""" BirthdayBook.py ***An introductory OOP Python activity demonstrating classes, attributes and methods inclusing saves to external text file*** """ ###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) 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 class UserInterface(): def __init__(self): self.birthday_book = BirthdayBook() self.choices() def confirm_quit(self): outofhere = input("Do you really want to quit? y/n?") if outofhere.lower()=='y' or outofhere.lower()=='yes': return True else: return False def add_entry(self): print ("Adding a new person") print("What is persons's: ") ## allow user to escape entry while editing given_name = input("Given Name? ") if given_name.lower()== "q": print("Not adding") return family_name = input("Family Name? ") if family_name.lower()=="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.lower()=="q": print("Not adding") return DOB_PROMPT="Date of Birth in form DD MM YY? " DOB = input(DOB_PROMPT) if DOB.lower()=="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)) f = open("workfile.txt","a+") e = birthday_entry saveline = ("{} {} DOB:{} email:{}".format(e.given_name, e.family_name, e.DOB, e.email_address)) f.write(saveline+"\n") f.close() def display_summaries(self): print() print("**************************************") print(" Displaying my saved Address Book ") print("**************************************") f=open("workfile.txt","r+") for line in f: print(line) 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.lower()=="a": self.add_entry() elif choice.lower()=="q": if self.confirm_quit()==True: print("Exiting the Birthday book.") break elif choice.lower()=="i": print(Help) elif choice.lower() =="d": self.display_summaries() else: instruction_message = "This was not one of my choices: {}" print() print(instruction_message.format(choice)) UserInterface()