This repository was archived by the owner on Jun 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
70 lines (51 loc) · 1.88 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
from collections import UserDict
from dataclasses import dataclass
@dataclass
class Field:
value: str
def __str__(self) -> str:
return str(self.value)
class Name(Field):
...
class Phone(Field):
def __init__(self, value: str):
if len(value) != 10 or not value.isdigit():
raise ValueError('The number is not valid!')
super().__init__(value)
class Record:
def __init__(self, name: str) -> None:
self.name = Name(name)
self.phones: list[Phone] = []
def __find(self,
phone: str,
get_instance: bool = False
) -> int | Phone | None:
for id, instance in enumerate(self.phones):
if instance.value == phone:
return instance if get_instance else id
return None
def add_phone(self, phone: str) -> None:
self.phones.append(Phone(phone))
def remove_phone(self, phone: str) -> None:
if (id := self.__find(phone)) is not None:
del self.phones[id]
def edit_phone(self, current: str, new: str) -> None:
if (id := self.__find(current)) is None:
raise ValueError(f'Phone {current} not found!')
else:
self.phones[id] = Phone(new)
def find_phone(self, phone: str) -> Phone | None:
return self.__find(phone, True)
def __str__(self) -> str:
phones = '; '.join(map(lambda phone: phone.value, self.phones))
return f'Contact name: {self.name.value}, phones: {phones}'
class AddressBook(UserDict):
def add_record(self, record: Record) -> None:
self.data[record.name.value] = record
def find(self, name: str) -> Record | None:
return self.data.get(name)
def delete(self, name: str) -> None:
for id, record in self.data.items():
if record.name.value == name:
del self.data[id]
break