1
+ # This combines version 2 and 3
2
+
3
+ class ContactList :
4
+
5
+ contacts = {}
6
+
7
+ def __init__ (self ):
8
+
9
+ with open ('contacts2.csv' , 'r' ) as file : # open the file
10
+ lines = file .read ().split ('\n ' ) #when encounter a new line character, store into lines variable
11
+
12
+ header = lines [0 ].split (',' )
13
+
14
+ contacts_list = []
15
+
16
+ for line in lines :
17
+ contacts_dict = {}
18
+ elements = line .split (',' )
19
+ for i in range (len (header )):
20
+ contacts_dict .update ({header [i ]: elements [i ]})
21
+ contacts_list .append (contacts_dict )
22
+ contacts_list .pop (0 )
23
+ self .contacts = contacts_list
24
+
25
+ def create_record (self ):
26
+ name = input ("What is the name?" )
27
+ email = input ("What is the email?" )
28
+ phone = input ("what is the phone number?" )
29
+
30
+ newrecord = {"name" : name ,
31
+ "email" : email ,
32
+ "phone" : phone
33
+ }
34
+
35
+ self .contacts .append (newrecord )
36
+
37
+ with open ('contacts2.csv' , 'a' ) as file :
38
+ file .write (f'\n { name } ,{ email } ,{ phone } ' )
39
+
40
+ def print_list (self ):
41
+ print (self .contacts )
42
+
43
+
44
+ def retrieve_record (self ):
45
+ search_name = input ("What name are you searching for?" )
46
+ for i in self .contacts :
47
+ if i ['name' ].lower () == search_name .lower ():
48
+ print (i )
49
+
50
+ def update_record (self ):
51
+ search_name = input ("Which name do you want to update?" )
52
+ updated_attribute = input ("Which field do you want to update? Options are name, email, phone." )
53
+ change_attribute = input ("What do you want to update the field to be?" )
54
+ for i in self .contacts :
55
+ if i ['name' ].lower () == search_name .lower ():
56
+ i [updated_attribute .lower ()] = change_attribute
57
+
58
+ # print(self.contacts)
59
+
60
+ with open ('contacts2.csv' , 'w' ) as file :
61
+ file .write ("name,email,phone\n " )
62
+ for i in self .contacts :
63
+ name = i ["name" ]
64
+ email = i ["email" ]
65
+ phone = i ["phone" ]
66
+ file .write (f'{ name } ,{ email } ,{ phone } \n ' )
67
+
68
+ def delete_record (self ):
69
+ search_name = input ("Which contact do you want to delete? Enter their name" )
70
+ for i in self .contacts :
71
+ if i ['name' ].lower () == search_name .lower ():
72
+ self .contacts .remove (i )
73
+
74
+ with open ('contacts2.csv' , 'w' ) as file :
75
+ file .write ("name,email,phone\n " )
76
+ for i in self .contacts :
77
+ name = i ["name" ]
78
+ email = i ["email" ]
79
+ phone = i ["phone" ]
80
+ file .write (f'{ name } ,{ email } ,{ phone } \n ' )
81
+
82
+ contacts_test = ContactList ()
83
+ while True :
84
+ command = input ('Enter a command: ' )
85
+ if command == 'retrieve' :
86
+ contacts_test .retrieve_record ()
87
+ print ("Record retrieved" )
88
+ elif command == 'create' :
89
+ contacts_test .create_record ()
90
+ print ("Record created" )
91
+ elif command == 'update' :
92
+ contacts_test .update_record ()
93
+ print ("Record updated" )
94
+ elif command == 'delete' :
95
+ contacts_test .delete_record ()
96
+ print ("Record deleted" )
97
+ elif command == 'help' :
98
+ print ('Available commands:' )
99
+ print ('retrieve - retrieve a record' )
100
+ print ('create - create a record' )
101
+ print ('update - update a record' )
102
+ print ('delete - delete a record' )
103
+ print ('exit - exit the program' )
104
+ elif command == 'exit' :
105
+ break
106
+ else :
107
+ print ('Command not recognized' )
0 commit comments