-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
57 lines (42 loc) · 1.47 KB
/
main.py
File metadata and controls
57 lines (42 loc) · 1.47 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
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):
if not value.isdigit() or len(value) != 10:
raise ValueError("Invalid phone number format")
super().__init__(value)
class Record:
def __init__(self, name):
self.name = Name(name)
self.phones = []
def add_phone(self, phone):
self.phones.append(Phone(phone))
def remove_phone(self, phone):
self.phones = [p for p in self.phones if p.value != phone]
def edit_phone(self, old_phone, new_phone):
phone_found = False
for phone in self.phones:
if phone.value == old_phone:
phone.value = new_phone
phone_found = True
break
if not phone_found:
raise ValueError(f"Phone {old_phone} not found in the record")
def find_phone(self, phone):
return next((p for p in self.phones if p.value == phone), None)
def __str__(self):
return f"Contact name: {self.name.value}, phones: {'; '.join(str(p) for p in self.phones)}"
class AddressBook(UserDict):
def add_record(self, 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]