-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
65 lines (57 loc) · 2.4 KB
/
test.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
class FileActions:
def __init__(self, name: str):
self.sheet = {}
self.file_name = name
with open(name, encoding='utf-8') as file:
for line in file:
key, value = line.split('-')
self.sheet[key.strip()] = float(value)
def add(self):
name = input('Введите название продукта:')
price = input('Введите цену продукта:')
self.sheet[name] = float(price)
self._commit()
print(f'Продукт добавлен: {name} - {price}')
def change(self):
name = self._get_existing_name()
if name:
price = input('Введите цену продукта:')
self.sheet[name] = float(price)
self._commit()
print(f'Продукт изменен: {name} - {price}')
def delete(self):
name = self._get_existing_name()
if name:
del self.sheet[name]
self._commit()
print(f'Продукт удален: {name}')
def total(self):
result = sum(list(self.sheet.values()))
print(f'Сумма = {result}')
def _commit(self):
with open(self.file_name, 'wt', encoding='utf-8') as file:
for key, value in self.sheet.items():
file.write(f'{key} - {value}\n')
def _get_existing_name(self):
while True:
name = input('Введите название продукта ("Enter" для отмены):')
if not name:
return ''
if name not in self.sheet:
print(f'Нет такого продукта! Имеются: {tuple(self.sheet.keys())}')
continue
return name
if __name__ == '__main__':
file_actions = FileActions('vegetables.txt')
actions = {'Добавить': file_actions.add, 'Изменить': file_actions.change, 'Удалить': file_actions.delete,
'Сумма': file_actions.total}
while True:
separator_length = len(str(file_actions.sheet)) + 8
print('=' * separator_length)
print(f'Товары: {file_actions.sheet}')
action = input(f'Введите действие {tuple(actions.keys())}, нажмитe "Enter" для выхода":')
if not action:
break
if action not in actions:
continue
actions[action]()