-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfamily.py
138 lines (122 loc) · 4.43 KB
/
family.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
133
134
135
136
137
138
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Family application class."""
from src.json_database import JsonDatabase
from src.person import Person
from pprint import pprint as pp
class FamilyApplication:
APPLICATION_NAME = 'Application: FAMILY'
LINE_LENGTH = 50
DATABASE_NAME = 'family'
DATABASE_DIR = 'databases/'
def run(self):
run_app = True
"""Run application."""
self.display_app_name()
self.display_app_menu()
while run_app:
option = self.get_menu_option_from_strem()
if option == 1:
self.get_all_family_persons()
self.display_app_menu()
elif option == 2:
self.get_family_person()
self.display_app_menu()
elif option == 3:
self.add_new_person_to_family()
self.display_app_menu()
elif option == 4:
self.remove_family_person()
self.display_app_menu()
elif option == 5:
self.close_app()
run_app = False
def display_app_name(self):
"""Display application name."""
self.print_line()
print(self.APPLICATION_NAME)
self.print_line()
def display_app_menu(self):
"""Display application menu."""
print('MENU:')
print('1. Show all family persons')
print('2. Show family person')
print('3. Add person')
print('4. Remove person')
print('5. Close application')
self.print_line()
def get_menu_option_from_strem(self):
"""Get option from stream."""
is_incorrect = True
while is_incorrect:
option_number = input('Select option [1-5]: ')
if option_number.isdigit():
option_number = int(option_number)
if option_number in [1, 2, 3, 4, 5]:
is_incorrect = False
else:
print('Option number must by from 1 to 5.')
self.print_line()
else:
print('Option number must by an integer number.')
self.print_line()
self.print_line()
return option_number
def get_all_family_persons(self):
"""Display all persons in family."""
print('ALL PERSONS IN FAMILY')
family_db = JsonDatabase(self.DATABASE_NAME, self.DATABASE_DIR)
pp(family_db.fetch_all())
self.print_line()
def get_family_person(self):
"""Display person in family."""
print('PERSON IN FAMILY')
is_incorrect = True
while is_incorrect:
person_id = input('Type person id: ')
if person_id.isdigit():
is_incorrect = False
else:
print('Person id must be an integer number')
family_db = JsonDatabase(self.DATABASE_NAME, self.DATABASE_DIR)
pp(family_db.fetch(int(person_id)))
self.print_line()
def add_new_person_to_family(self):
"""Add new person to family"""
print('ADD NEW PERSON TO FAMILY')
family_db = JsonDatabase(self.DATABASE_NAME, self.DATABASE_DIR)
new_person = Person()
new_person.set_firstname(input('Type firstname: '))
new_person.set_lastname(input('Type lastname: '))
new_person.set_age(input('Type age: '))
new_person.set_height(input('Type height: '))
new_person.set_weight(input('Type weight: '))
new_person_json = new_person.get_person_like_json()
family_db.add(new_person_json)
family_db.save()
print('Person added')
self.print_line()
def remove_family_person(self):
"""Remove person from family."""
print('REMOVE PERSON')
is_incorrect = True
while is_incorrect:
person_id = input('Type person id: ')
if person_id.isdigit():
is_incorrect = False
else:
print('Person id must be an integer number')
family_db = JsonDatabase(self.DATABASE_NAME, self.DATABASE_DIR)
if family_db.delete(int(person_id)):
family_db.save()
print('Person removed')
self.print_line()
def close_app(self):
"""Close application."""
print('CLOSE APPLICATION')
self.print_line()
def print_line(self):
"""Print line."""
print('-' * self.LINE_LENGTH)
application = FamilyApplication()
application.run()