5. 파이썬
[파이썬 객체지향] 주소록 예제
패스트코드블로그
2020. 5. 9. 14:12
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 | class Entity: def __init__(self): self._name = '' self._phone = '' self._email = '' self._addr = '' @property def name(self) -> str: return self._name @name.setter def name(self, name): self._name = name @property def phone(self) -> str: return self._phone @phone.setter def phone(self, phone): self._phone = phone @property def email(self) -> str: return self._email @email.setter def email(self, email): self._email = email @property def addr(self) -> str: return self._addr @addr.setter def addr(self, addr): self._addr = addr def to_string(self): return f'이름 {self._name}, 전화번호 {self._phone}, 이메일 {self._email}, 주소 {self._addr}' class Service: def __init__(self): self._contacts = [] def add_contact(self, payload): self._contacts.append(payload) def get_contacts(self): contacts = [] for i in self._contacts: contacts.append(i.to_string()) return ' '.join(contacts) def del_contact(self, name): for i, t in enumerate(self._contacts): if t.name == name: del self._contacts[i] class Controller: def __init__(self): self.service = Service() def register(self,name, phone, email, addr ): entity = Entity() entity.name = name entity.phone = phone entity.email = email entity.addr = addr self.service.add_contact(entity) def list(self): return self.service.get_contacts() def remove(self, name): self.service.del_contact(name) if __name__ == '__main__': def print_menu(): print('0. Exit') print('1. 등록') print('2. 목록') print('3. 삭제') return input('메뉴 선택 \n') app = Controller() while 1: menu = print_menu() if menu == '0': break if menu == '1': app.register(input('이름\n'),input('전화번호\n'),input('이메일\n'),input('주소\n')) if menu == '2': print(app.list()) if menu == '3': app.remove(input('이름\n')) elif menu == '0': break | cs |