Skip to content

Commit a4eab01

Browse files
committed
adding contacts ex
1 parent 5e36d30 commit a4eab01

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
class PhoneBook:
2+
# attributes, characteristics
3+
def __init__(self):
4+
self.contacts = [] #list of contacts
5+
self.path = '' #path to csv
6+
self.headers = [] #stored headers
7+
8+
9+
def load(self, path):
10+
"""
11+
returns a list of dictionaries representating a contact list csv
12+
reads csv
13+
"""
14+
self.path = path #set path
15+
with open(self.path) as f:
16+
lines = f.read().split('\n')
17+
18+
# process csv into list of dictionaries
19+
self.headers = lines[0].split(',')
20+
for i in range(1, len(lines)):
21+
row = lines[i].split(',')
22+
contact = dict(zip(self.headers, row))
23+
self.contacts.append(contact)
24+
return len(self.contacts)
25+
26+
def count(self):
27+
"""return the length of self.contacts"""
28+
29+
return len(self.contacts)
30+
31+
def save(self):
32+
"""
33+
1) open 'self.file' with open 'w' for write
34+
2) convert the dictionaries into lines
35+
3) & write each line into to the file
36+
"""
37+
lines = []
38+
for contact in self.contacts:
39+
lines.append( ",".join(list(contact.values())))
40+
41+
with open(self.path, 'w') as phone_book_file:
42+
phone_book_file.write('\n'.join(lines))
43+
44+
45+
def print(self):
46+
"""
47+
loop over self.contacts
48+
print the information for each contact
49+
"""
50+
for contact in self.contacts:
51+
print(contact)
52+
53+
def add(self, name, phone_number, state):
54+
"""
55+
create a new dictionary
56+
add the new dictionary to self.contacts
57+
"""
58+
59+
new_contact = {
60+
"name": f"{name}",
61+
"phone_number": f"{phone_number}",
62+
"state": f"{state}"
63+
}
64+
65+
self.contacts.append(new_contact)
66+
67+
def remove(self, name):
68+
"""
69+
find the contact in self-contacts with the given name
70+
remove the element at that index
71+
"""
72+
for i in self.contacts:
73+
if i["name"] == name:
74+
self.contacts.remove(i)
75+
76+
def update(self, old_name, new_name, new_phone_number, new_state):
77+
"""
78+
find the contact in self.contacts with the given old_name
79+
set that contacts' name, phone number, etc to the given values
80+
"""
81+
for i in self.contacts:
82+
if i["name"] == old_name:
83+
self.add(new_name, new_phone_number, new_state)
84+
self.remove(old_name)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import PhoneBook
2+
3+
def main():
4+
phone_book = PhoneBook.PhoneBook() # create an instance of our class
5+
print('Welcome to the Phone Book')
6+
while True:
7+
command = input('Enter a command: ')
8+
if command == 'load':
9+
path = input('Enter the path to your CSV: ')
10+
phone_book.load(path)
11+
print(f'Loaded {phone_book.count()} contacts.')
12+
elif command == 'save':
13+
phone_book.save()
14+
print(f'Saved {phone_book.count()} contacts.')
15+
elif command == 'print':
16+
phone_book.print()
17+
elif command == 'add':
18+
print('Enter info of contact to add:')
19+
name = input('Name: ')
20+
phone_number = input('Phone Number: ')
21+
state = input('state: ')
22+
phone_book.add(name, phone_number, state)
23+
elif command == 'remove':
24+
name = input('Name of contact to remove: ')
25+
phone_book.remove(name)
26+
elif command == 'update':
27+
print('Enter info of contact to add:')
28+
old_name = input('Name of contact to update: ')
29+
new_name = input('New Name: ')
30+
new_phone_number = input('New Phone Number: ')
31+
new_state = input('New state: ')
32+
phone_book.update(old_name, new_name, new_phone_number,
33+
new_state)
34+
elif command == 'help':
35+
print('Available commands:')
36+
print('load - load all contacts from the file')
37+
print('save - save contacts to a file')
38+
print('print - print all contacts')
39+
print('add - add a new contact')
40+
print('remove - remove a contact')
41+
print('update - update a contact')
42+
print('q - quit the program')
43+
elif command == 'q':
44+
break
45+
else:
46+
print('Command not recognized')
47+
48+
49+
if __name__ == "__main__":
50+
main()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Sarah Beth,123432112,VA
2+
3+
Frog,123456,florida
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name,phone number,state,title,office
2+
Sarah Beth,123432112,VA,teacher,home
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import PhoneBook
2+
3+
book = PhoneBook.PhoneBook()
4+
book.load('work_contacts.csv')
5+
book.print()

0 commit comments

Comments
 (0)