-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
132 lines (107 loc) · 4.65 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
'''
Create a sistem to store contacts in the address book.
'''
from collections import UserDict
class Field:
'''
Base class for all fields.
'''
def __init__(self, value): # Initialize the field with a value.
self.value = value # Set the value of the field.
def __str__(self): # Return the string representation of the field.
return str(self.value) # Return the value of the field.
class Name(Field):
# Initialize the Class with a value.
pass
class Phone(Field):
# Initialize the Class with a value.
pass
class Record:
'''
Class to store a contact record.
'''
def __init__(self, name): # Initialize the record with a name.
self.name = Name(name) # Set the name of the record.
self.phones = [] # Initialize the list of phones.
def add_phone(self, phone):
'''
Add a phone to the record.
'''
if self._is_valid_phone_number(phone): # Check if the phone number is valid.
self.phones.append(Phone(phone)) # Add the phone to the list of phones.
else: # If the phone number is not valid.
print("Phone number should contain 10 digits.") # Print an error message.
def remove_phone(self, phone):
'''
Remove a phone from the record.
'''
for p in self.phones: # Iterate over the list of phones.
if p.value == phone: # If the phone number is found.
self.phones.remove(p) # Remove the phone number.
def edit_phone(self, old_phone, new_phone):
'''
Edit a phone in the record.
'''
if not self._is_valid_phone_number(new_phone): # Check if the new phone number is valid.
print("Phone number should contain 10 digits.") # Print an error message.
else: # If the new phone number is valid.
for phone in self.phones: # Iterate over the list of phones.
if phone.value == old_phone: # If the phone number is found.
phone.value = new_phone # Update the phone number.
def find_phone(self, phone):
'''
Find a phone in the record.
'''
for p in self.phones: # Iterate over the list of phones.
if p.value == phone: # If the phone number is found.
return p.value # Return the phone number.
return None # Return None if the phone number is not found.
def _is_valid_phone_number(self, phone):
'''
Check if the phone number is valid.
'''
return isinstance(phone, str) and phone.isdigit() and len(phone) == 10
# Return True if the phone number is valid.
def __str__(self):
'''
Return the string representation of the record.
'''
return f"Contact name: {self.name.value}, phones: {'; '.join(str(p) for p in self.phones)}" # Return the name and phones of the record.
class AddressBook(UserDict):
'''
Class to store the address book.
'''
def add_record(self, record):
'''
Add a record to the address book.
'''
self.data[record.name.value] = record # Add the record to the data dictionary.
def delete(self, name):
'''
Delete a record from the address book.
'''
return self.data.pop(name) # Remove the record from the data dictionary.
def find(self, name):
'''
Find a record in the address book.
'''
return self.data.get(name) # Return the record from the data dictionary.
book = AddressBook() # Create an instance of the AddressBook class.
john_record = Record("John") # Create a record for John.
john_record.add_phone("1234567890") # Add a phone to the record.
john_record.add_phone("5555555555") # Add another phone to the record.
john_record.add_phone("9999999999") # Add another phone to the record.
john_record.add_phone("1112223333") # Add another phone to the record.
john_record.remove_phone("9999999999") # Remove a phone from the record.
book.add_record(john_record) # Add the record to the address book.
jane_record = Record("Jane") # Create a record for Jane.
jane_record.add_phone("9876543210") # Add a phone to the record.
book.add_record(jane_record) # Add the record to the address book.
for name, record in book.data.items(): # Iterate over the records in the address book.
print(record) # Print the record.
john = book.find("John") # Find the record for John.
john.edit_phone("1234567890", "1112223333") # Edit the phone number for John.
print(john) # Print the record for John.
found_phone = john.find_phone("5555555555") # Find a phone number for John.
print(f"{john.name}: {found_phone}") # Print the phone number.
book.delete("Jane") # Delete the record for Jane.