-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
138 lines (133 loc) · 6.55 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
'''
@Author: shubham shirke
@Date: 2023-06-12 10:30:30
@Last Modified by: shubham shirke
@Last Modified time: 2022-06-16 23:30:30
@Title : Read and write json file in address book system.
'''
from logger import logger
from AddressBook_Service import *
def main():
logger.info('Starting the program')
"""
Description :
This function is where the code execution starts. It provides user a interface to work with address book system.
Parameters:
none
Returns :
none
displays menu driven interface.
"""
try:
address_book = AddressBook() # creating instance of a class
print("--Welcome to Address Book System--")
print("How would you like to read and write file in address book system?")
print("1.Text file\n2.CSV File\n3.JSON File\n4.Exit")
file_option =int(input("Enter option: "))
if file_option == 1:
file_name = input("Enter Text file name: ")
address_book.read_address_books_from_file(file_name)
elif file_option == 2:
file_name = input("Enter CSV file name: ")
address_book.read_address_books_from_csv(file_name)
elif file_option == 3:
file_name = input("Enter JSON file name: ")
address_book.read_address_books_from_json(file_name)
elif file_option == 4:
logger.error("File not found")
while True:
print()
print("--AddressBook System--")
print("----------------------")
print("1.Add AddressBook")
print("2.Remove AddressBook")
print("3.Edit AddressBook")
print("4.Search AddressBook")
print("5.Sort AddressBook")
print("6.Print AddressBook")
print("7.Exit")
print("-------------------")
user_choice = int(input("Enter choice: "))
if user_choice == 1:
address_book_name = input("Enter Address Book name: ")
address_book.add_address_books(address_book_name)
elif user_choice == 2:
address_book_name = input("Enter Address Book name to remove: ")
address_book.remove_address_book(address_book_name)
elif user_choice == 3:
address_book_name = input("Enter Address book name to update: ")
address_book.update_address_book(address_book_name)
elif user_choice == 4:
while True:
print()
print("--Search contacts in AddressBook--")
print("1. Search by state")
print("2. Search by City")
print("3. Exit")
user_search_choice =int(input("Enter choice: "))
#search by state
if user_search_choice == 1:
state_name = input("Enter state name to search: ")
print("Enter choice: \n 1:Display contact names \n 2:Display full contacts")
user_state_choice = int(input("Enter choice: "))
if user_state_choice == 1:
state_contacts,state_name = address_book.search_address_book_by_state(state_name,user_state_choice)
address_book.print_search_contacts(state_contacts,state_name)
elif user_state_choice == 2:
state_contacts,state_name = address_book.search_address_book_by_state(state_name,user_state_choice)
address_book.print_search_contacts(state_contacts,state_name)
elif user_state_choice == 3:
break
else:
logger.warning("Invalid option: enter correct option")
# search by city
elif user_search_choice == 2:
city_name = input("Enter city name to search: ")
print("Enter choice: \n 1.Display contact names \n 2.Display full contacts \n 3.Exit")
user_city_choice = int(input("Enter choice: "))
if user_city_choice == 1:
city_contacts,city_name = address_book.search_address_book_by_city(city_name,user_city_choice)
address_book.print_search_contacts(city_contacts,city_name)
elif user_city_choice == 2:
city_contacts,city_name = address_book.search_address_book_by_city(city_name,user_city_choice)
address_book.print_search_contacts(city_contacts,city_name)
elif user_state_choice == 3:
break
else:
logger.warning("Invalid option: enter correct option")
elif user_search_choice == 3:
break
else:
logger.warning("Please enter correct choice")
elif user_choice == 5:
print("--sort contacts--")
print("1.Sort by city \n2.Sort by state \n3.Exit")
user_sort_choice = int(input("Enter choice: "))
if user_sort_choice == 1:
sorted_contacts = address_book.sort_address_book(user_sort_choice)
address_book.print_sorted_contacts(sorted_contacts)
elif user_sort_choice == 2:
sorted_contacts = address_book.sort_address_book(user_sort_choice)
address_book.print_sorted_contacts(sorted_contacts)
elif user_sort_choice == 3:
break
else:
logger.warning("Invalid option: enter correct option")
elif user_choice == 6:
address_book.display_address_books()
elif user_choice == 7:
logger.info("Thank you for using Address Book system ")
if file_option == 1:
address_book.write_address_books_to_file(file_name)
elif file_option == 2:
address_book.write_address_books_to_csv(file_name)
elif file_option == 3:
address_book.write_address_books_to_json(file_name)
break
else :
logger.error("[error] : enter valid option")
except Exception as e:
logger.error('An error occurred: %s', str(e))
logger.info('Program finished')
if __name__ == "__main__":
main()