'''
Brian Crabtree
Program to simulate creating a new phone number. Demonstrates error handling
with custom exceptions.
'''

import random

#Class to handle when an invalid area code is entered
class badAreaCode(Exception):
    def __init__(self, code, minCode, maxCode):
        super().__init__("That is not a valid area code for this area!")

        self.code = code
        self.minCode = minCode
        self.maxCode = maxCode

    def valCodes(self):
        print("The area code you entered is not valid")
        print("Valid area codes are between %d and %d" %(self.minCode, self.maxCode))

#Class to handle when a phone number of the wrong length is entered
class invalidLength(Exception):
    pass

#Describe to the user what is happening
print("Today you are getting a new cell phone and get to pick out a new number for your phone.")

#Generate area codes and take in a phone number from the user.
try:
    minCode = random.randint(100, 999)
    maxCode = random.randint(minCode, 999)
    num = int(input("What would you like your new 10 digit number to be? (Area code must be between %d and %d) " %(minCode, maxCode)))

#Handle error where non-digits are entered
except ValueError:
    print("You cannot have non-digits in your phone number!")

#Handle other custom defined errors
else:
    #Check length of number
    if len(str(num)) != 10:
        raise invalidLength("That number is not 10 digits!")

    #Exctract the area code entered from the number
    code = int(str(num)[0:3])

    #Check the area code
    if code < minCode or code > maxCode:
        try:
            raise badAreaCode(code, minCode, maxCode)
        except badAreaCode as e:
            e.valCodes()

#Print a closing statement
finally:
    print("Enjoy your new phone!")

    
