-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
70 lines (51 loc) · 1.8 KB
/
main.py
File metadata and controls
70 lines (51 loc) · 1.8 KB
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
from collections import UserDict
class Field:
def __init__(self, value):
self.value = value
def __str__(self):
return str(self.value)
class Name(Field):
pass
class Phone(Field):
def __init__(self, value):
self.validate(value)
super().__init__(value)
def validate(self, value):
if len(value) != 10 or not value.isdigit():
raise ValueError('Phone should be 10 digits')
class Record:
def __init__(self, name):
self.name = Name(name)
self.phones = []
def __str__(self,):
return f"Contact name: {self.name.value}, phones: {'; '.join(p.value for p in self.phones)}"
def add_phone(self, phone_number: str):
phone = Phone(phone_number)
phone.validate(phone_number)
if phone not in self.phones:
self.phones.append(phone)
def find_phone(self, phone_number: str):
for phone in self.phones:
if phone.value == phone_number:
return phone
return None
def edit_phone(self, old_phone, new_phone):
for phone in self.phones:
if phone.value == old_phone:
phone.validate(new_phone)
phone.value = new_phone
return
raise ValueError(f"Phone {old_phone} not found")
def remove_phone(self, phone_number):
self.phones = [phone for phone in self.phones if phone.value != phone_number]
class AddressBook(UserDict):
def add_record(self, record: Record):
self.data[record.name.value] = record
def find(self,name):
return self.data.get(name)
def delete(self,name):
if name in self.data:
del self.data[name]
return f"Contact {name} deleted"
else:
return f"Contact with name {name} not found"