""" BirthdayBook.py ***An introductory OOP Python activity demonstrating classes, attributes and methods*** """ ###CLASSES ##Uses three classes:BirthdayBook, BirthdayEntry, Engine class BirthdayBook(): #Note: Methods for classes appear as functions # creates instance of the class called 'friends' as a list def __init__(self): self.friends = [] def add_newentry(self, new_entry): # adds entry to BirthdayBook list self.friends.append(new_entry) ## def display(self): ## for friend in self.friends: ## friend.display() class BirthdayEntry(): # multiple instances of class hold friend details # initialise attributes # class requires its methods to be initialised with the argument 'self' def __init__(self, given_name = None, family_name = None, email_address = None, DOB = None): self.given_name = given_name self.family_name = family_name self.email_address = email_address self.DOB = DOB # syntax for formatting needs explanation for students def display(self): print('{} {}: {} {}'.format(self.given_name, self.family_name, self.email_address, self.DOB)) class UserInterface(): def __init__(self, book): self.birthday_book = book self.choices() def confirm_quit(self): outofhere=input("""If you respond y, all data will be lost as no module exists for saving to a file. Do you really want to quit? y/n?""") if outofhere=='n': return False else: return True def add_entry(self): print ("Adding a new person") print("What is persons's: ") given_name = input("Given Name? ") if given_name =="q": print("Not adding") return family_name = input("Family Name? ") if family_name =="q": print("Not adding") return email_address =input("Email address? ") while "@" not in email_address: print("Invalid email") email_address = input("Email address? ") if email_address =="q": print("Not adding") return DOB_PROMPT = "Date of Birth in form DD MM YY? " DOB = input(DOB_PROMPT) if DOB=="q": print("Not adding") return birthday_entry = BirthdayEntry(given_name, family_name, email_address, DOB) self.birthday_book.add_newentry(birthday_entry) print("Added Birthday entry for {} {}".format(given_name,family_name)) def display_summaries(self): print() print("**************************************") print(" Displaying my Address Book ") print("**************************************") ##this will be used to display the birthday book as it now exits with its set entries self.birthday_book.display() print("**************************************") def choices(self): # define Help as string constant as it is used more than once Help ="""An OOP Birthday Book Python program Use: a to add an entry d to display all save entries in Address Book i to see the instructions q to quit """ while True: print() print(Help) choice = input("Make a selection from the above ") if choice=="a": self.add_entry() elif choice=="q": if self.confirm_quit()==True: print("Exiting the Birthday book.") break elif choice=="i": print(Help) elif choice=="d": self.display_summaries() else: instruction_message = "This was not one of my choices: {}" print(instruction_message.format(choice)) # the main program starts here # program is limited to entries created while running in addition to # the following 'hard-wired'entries until students learn to save and read external files book = BirthdayBook() book.add_newentry(BirthdayEntry('Bob', 'Smith', 'bsmith@python.com', '11111')) book.add_newentry(BirthdayEntry('Mary', 'Smith', 'msmith@python.com', '22222')) UserInterface(book)