def show(phoneBook): cls() print(' WHOLE PHONEBOOK:\n') i = 0 for person in phoneBook.values(): person.print() print('Total number of contacts: ', len(phoneBook)) print('\n-----------------------------\n')
def showSearch(searchBook): cls() print(' PHONEBOOK AFTER CURRENT SEARCH:\n') i = 0 for person in searchBook.values(): person.print() print('Number of contacts that were found: ', len(searchBook)) print('\n-----------------------------\n')
def showBirthday(searchBook): cls() print('\tLIST OF CONTACTS WHO WILL CELEBRATE BIRTHDAY SOON:\n') i = 0 for person in searchBook.values(): i += 1 person.print() print('\t\tNumber of contacts that were found: ', len(searchBook)) print('\n\t-----------------------------\n')
def currentAge(phoneBook): cls() print( '\tTo find information about age of person from phoneBook enter name and surname in format(Name Surname) \n\t\tOr ' 'enter \'quit\' to quit') menu = input('\n\tEnter: ') if menu != 'quit': try: nameSurname = menu ns = nameSurname.split(' ') searchKey = None ns[0] = ns[0].title() ns[1] = ns[1].title() for key in list(phoneBook): if phoneBook[key].name == ns[0] and phoneBook[ key].surname == ns[1]: searchKey = key if searchKey is not None: print('\n\tContact was found:\n') phoneBook[searchKey].print() agePerson = phoneBook[searchKey] if agePerson.birthday is not None: now = datetime.now().date() delta = now - agePerson.birthday print('\tNow ' + agePerson.name + ' ' + agePerson.surname + ' is ' + str(int(delta.days / 365)) + ' years old\n') else: print( "\tThere is no information about the date of birthday\n\n" ) else: print('\tThere is no person with such name and surname\n') except BaseException: print('\n\tWrong name and surname format\n\n') else: cls()
def birthdayList(phoneBook): cls() searchBook = phoneBook.copy() for key in list(searchBook): if searchBook[key].birthday is None: del searchBook[key] else: today = datetime.now().date() soon = datetime.now() + timedelta(days=31) soon = soon.date() day = searchBook[key].birthday.day month = searchBook[key].birthday.month year = today.year birthday1 = date(year, month, day) birthday2 = date(year + 1, month, day) if not (today < birthday1 and birthday1 < soon) and not ( today < birthday2 and birthday2 < soon): del searchBook[key] showBirthday(searchBook) print('\tDon\'t forget to congratulate them!!!\n')
def search(phoneBook): cls() searchBook = phoneBook.copy() print('\tTo find a person in phone book choose a field for search:') print('\t(You can search till the command quit narrowing down you search)') print('\t\t1.name\n\t\t2.surname\n\t\t3.name and surname\n\t\t4.phone\n\t\t5.date of birthday\n\t\t\'quit\' to quit\n') menu = input('Enter: ') while menu != 'quit': try: menu = int(menu) except BaseException: menu = 6 if menu == 1: try: name = input('\tEnter a name:') name = name.replace(' ', '') name = name.title() for key in list(searchBook): if searchBook[key].name != name: del searchBook[key] showSearch(searchBook) except BaseException: print('Wrong name format, try one more time\n') elif menu == 2: try: surname = input('\tEnter a surname:') surname = surname.replace(' ', '') surname = surname.title() for key in list(searchBook): if searchBook[key].surname != surname: del searchBook[key] showSearch(searchBook) except BaseException: print('Wrong surname format, try one more time\n') elif menu == 3: try: nameSurname = input('\tEnter name and surname in format (Name Surname):') ns = nameSurname.split(' ') ns[0] = ns[0].title() ns[1] = ns[1].title() for key in list(searchBook): if searchBook[key].name != ns[0] and searchBook[key].surname != ns[1]: del searchBook[key] showSearch(searchBook) except BaseException: print('wrong name or surname format, try one more time\n') elif menu == 4: try: phone = input('\tEnter phone number:') phone = phone.replace(' ', '').replace('+7', '8') for key in list(searchBook): if searchBook[key].phones['mobile'] != phone and searchBook[key].phones['home'] != phone and \ searchBook[key].phones['work'] != phone: del searchBook[key] showSearch(searchBook) except BaseException: print('wrong phone format, try one more time\n') elif menu == 5: try: birthday = input('\tEnter date of birthday in format (dd.mm):') birthday = birthday.replace(' ', '') DM = birthday.split('.') day = DM[0] month = DM[1] for key in list(searchBook): if searchBook[key].birthday is None or not (searchBook[key].birthday.day == int(day) and searchBook[key].birthday.month == int(month)): del searchBook[key] showSearch(searchBook) except BaseException: print('wrong date format, try one more time\n') # wrong command print( '\tChoose a field for search:\n\t\t1.name\n\t\t2.surname\n\t\t3.name and surname\n\t\t4.phone\n\t\t5.date of birthday\n\t\t\'quit\' to quit\n') menu = input('\tEnter: ') cls()
def add(phoneBook): cls() print( '\tTo add new person in phone book enter information in the following format:' ) print( '\t\tName;Surname;mobile:8xxxxxxxx,home:xxxxxxx,work:xxxxxxxx;dd.mm.yyyy' ) print( '\tIf some fields are missing, just dont enter it (at least one phone number is needed)' ) print( '\t\texample: Ivan; Ivanov; mobile:88005553535, home:2523772; 01.01.2000' ) print('\t\texample: Andrey; Andreev; mobile:88005553535') print('\t\tOr enter \'quit\' to quit') string = input('\n\tEnter: ') string = string.replace(' ', '') if string != 'quit': newOne = parse(string) # Enter a string with the information until it has a right format while newOne == None: string = input('Enter: ') newOne = parse(string) # Check if there is a person with such Name and Surname f = (newOne.name + ' ' + newOne.surname) in phoneBook if not f: phoneBook[newOne.name + ' ' + newOne.surname] = newOne newOne = None # while f: print( '\n\tThere is a contact with such Name and Surname or wrong command\n\tChoose what to do:\n' ) print('\t\t1.replace\n\t\t2.reenter information\n\t\t3.quit') try: menu = int(input('\n\tEnter: ')) except BaseException: menu = 4 # Replace the information if menu == 1: del (phoneBook[newOne.name + ' ' + newOne.surname]) phoneBook[newOne.name + ' ' + newOne.surname] = newOne f = False # Reenter a string elif menu == 2: print( '\tTo add new person in phone book enter information in the following format:' ) print( '\t\tName;Surname;mobile:8xxxxxxxx,home:xxxxxxx,work:xxxxxxxx;dd.mm.yyyy' ) print( '\tIf some fields are missing, just dont enter it (at least one phone number is needed)' ) print( '\t\texample: Ivan; Ivanov; mobile:88005553535, home:2523772; 01.01.2000' ) print('\t\texample: Andrey; Andreev; mobile:88005553535') while newOne == None: string = input('\n\tEnter: ') newOne = parse(string) f = (newOne.name + ' ' + newOne.surname) in phoneBook if not f: phoneBook[newOne.name + ' ' + newOne.surname] = newOne else: newOne = None # quit elif menu == 3: f = False # wrong command else: print('\n\t\tWRONG COMMAND') f = True cls()
def change(phoneBook): cls() print( '\tTo change information about a person from phoneBook enter name and surname in format(Name Surname) \n\t\tOr ' 'enter \'quit\' to quit') menu = input('\n\tEnter: ') if menu != 'quit': try: nameSurname = menu ns = nameSurname.split(' ') searchKey = None ns[0] = ns[0].title() ns[1] = ns[1].title() for key in list(phoneBook): if phoneBook[key].name == ns[0] and phoneBook[ key].surname == ns[1]: searchKey = key if searchKey is not None: print('\n\tContact was found:\n') phoneBook[searchKey].print() changePerson = phoneBook[searchKey] print( '\n\tChoose a field to change:\n\t\t1.name\n\t\t2.surname\n\t\t3.mobile number\n\t\t4.home number\n\t\t5.work ' 'number\n\t\t6.date of birhday\n\t\t\'quit\' to quit') menu2 = input('\n\tEnter: ') while menu2 != 'quit': try: menu2 = int(menu2) except BaseException: menu2 = 7 if menu2 == 1: try: name = input('\tEnter new name: ') name = name.replace(' ', '') name = name.title() if (name + ' ' + changePerson.surname) in phoneBook: print('There is such contact ' + name + ' ' + changePerson.surname + ' in phoneBook, try one more time') else: phoneBook[name + ' ' + changePerson. surname] = phoneBook.pop(searchKey) phoneBook[name + ' ' + changePerson.surname].name = name searchKey = name + ' ' + changePerson.surname except BaseException: print('Wrong name format, try one more time\n') elif menu2 == 2: try: surname = input('\tEnter new surname: ') surname = surname.replace(' ', '') surname = surname.title() if (changePerson.name + ' ' + surname) in phoneBook: print('There is such contact ' + changePerson.name + ' ' + surname + ' in phoneBook, try one more time') else: phoneBook[changePerson.name + ' ' + surname] = phoneBook.pop(searchKey) phoneBook[changePerson.name + ' ' + surname].surname = surname searchKey = changePerson.name + ' ' + surname except BaseException: print('Wrong surname format, try one more time\n') elif menu2 == 3: try: phone = input( '\tEnter new mobile number or \'del\' to delete: ' ) phone = phone.replace(' ', '').replace('+7', '8') if phone == 'del': if (phoneBook[searchKey].phones['home'] is None and phoneBook[searchKey].phones['work'] is None): raise Exception() phoneBook[searchKey].phones['mobile'] = None else: if not phone.isdigit(): raise Exception() phoneBook[searchKey].phones['mobile'] = phone except BaseException: print( '\nWrong phone number format or you try to delete only (last) number, try one more time\n' ) elif menu2 == 4: try: phone = input( '\tEnter new home number \'del\' to delete: ') phone = phone.replace(' ', '').replace('+7', '8') if phone == 'del': if (phoneBook[searchKey].phones['mobile'] is None and phoneBook[searchKey].phones['work'] is None): raise Exception() phoneBook[searchKey].phones['home'] = None else: if not phone.isdigit(): raise Exception() phoneBook[searchKey].phones['home'] = phone except BaseException: print( '\nWrong phone number format or you try to delete only (last) number, try one more time\n' ) elif menu2 == 5: try: phone = input( '\tEnter new work number or \'del\' to delete: ' ) phone = phone.replace(' ', '').replace('+7', '8') if phone == 'del': if (phoneBook[searchKey].phones['mobile'] is None and phoneBook[searchKey].phones['home'] is None): raise Exception() phoneBook[searchKey].phones['work'] = None else: if not phone.isdigit(): raise Exception() phoneBook[searchKey].phones['work'] = phone except BaseException: print( '\nWrong phone number format or you try to delete only (last) number, try one more time\n' ) elif menu2 == 6: try: newDate = input( '\tEnter new date of birthday in format (dd.mm.yyyy) or \'del\' to delete: ' ) if newDate == 'del': phoneBook[searchKey].birthday = None else: newDate = newDate.replace(' ', '') newDate = newDate.replace('/', '.').replace( '-', '.') phoneBook[ searchKey].birthday = datetime.strptime( newDate, '%d.%m.%Y').date() except BaseException: print('Wrong date format, try one more time\n') print('\nChanges were saved:\n') phoneBook[searchKey].print() print( '\n\n\tChoose one more field to change:\n\t\t1.name\n\t\t2.surname\n\t\t3.mobile number\n\t\t4.home number\n\t\t5.work ' 'number\n\t\t6.date of birhday\n\t\t\'quit\' to quit') menu2 = input('\n\tEnter: ') cls() else: print('\t\tThere is no person with such name and surname\n\n') except BaseException: print('Wrong name and surname format') else: cls()
def delete(phoneBook): cls() print( '\tTo delete a person from phoneBook choose a field for deletion search:' ) print( '\t\t1.name and surname\n\t\t2.name\n\t\t3.surname\n\t\t4.phone\n\t\t\'quit\' to quit\n' ) menu = input('\tEnter: ') if menu != 'quit': try: menu = int(menu) except BaseException: menu = 6 listDelete = [] f = True if menu == 1: try: nameSurname = input( '\n\tEnter name and surname in format (Name Surname):') ns = nameSurname.split(' ') ns[0] = ns[0].title() ns[1] = ns[1].title() for key in list(phoneBook): if phoneBook[key].name == ns[0] and phoneBook[ key].surname == ns[1]: listDelete.append(key) except BaseException: print('\n\tWrong name or surname format, try one more time') f = False elif menu == 2: try: name = input('\n\tEnter a name:') name = name.replace(' ', '') name = name.title() for key in list(phoneBook): if phoneBook[key].name == name: listDelete.append(key) except BaseException: print('\n\tWrong name format, try one more time') f = False elif menu == 3: try: surname = input('\n\tEnter a surname:') surname = surname.replace(' ', '') surname = surname.title() for key in list(phoneBook): if phoneBook[key].surname == surname: listDelete.append(key) except BaseException: print('\n\tWrong surname format, try one more time') f = False elif menu == 4: try: phone = input('\n\tEnter phone number:') phone = phone.replace(' ', '').replace('+7', '8') if not phone.isdigit(): raise Exception() for key in list(phoneBook): if phoneBook[key].phones['mobile'] == phone or phoneBook[key].phones['home'] == phone or \ phoneBook[key].phones['work'] == phone: listDelete.append(key) except BaseException: print('\n\tWrong phone format, try one more time') f = False else: f = False if f == True: print('\tList of contacts for deletion:\n') for element in listDelete: phoneBook[element].print() print( '\n\t\tDo you want to delete all of them? Choose:\n\t1.Yes\n\t2.Cancel\n' ) menu2 = input('Enter: ') try: menu2 = int(menu2) except BaseException: menu2 = 2 if menu2 == 1: for key in listDelete: del phoneBook[key] cls() else: print('\n\tWrong command\n')