Skip to content

Commit 4708eb2

Browse files
MyLinden-devMyLinden-dev
authored andcommitted
Тест меню читателей
1 parent 0d63ece commit 4708eb2

2 files changed

Lines changed: 84 additions & 99 deletions

File tree

Handler/LibraryHandler.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ def add_book(self, book):
4040
self.books.append(book)
4141

4242
def del_book(self, id_book) -> bool:
43-
book = get_item_by_id(self.__books, id_book)
44-
if book is None:
43+
index = get_index_by_id(self.__books, id_book)
44+
if index == -1:
4545
print('Не найдена книга с указанным идентификатором')
4646
return False
4747
else:
48-
self.books.remove(book)
48+
del self.books[index]
4949
return True
5050

5151
def is_book_exists(self, id_book) -> bool:
@@ -71,6 +71,14 @@ def edit_book(self, id_book, book) -> bool:
7171
# endregion
7272

7373
# region Readers
74+
def get_info_about_reader(self, id_reader) -> str:
75+
reader = get_item_by_id(self.__readers, id_reader)
76+
res = f"\n--- Читатель № {reader.id} ---\n"
77+
res += reader.__str__()
78+
res += f"Взято книг: {len(reader.books)}\n"
79+
res += "---\n"
80+
return res
81+
7482
def get_info_about_readers(self) -> str:
7583
res = "\n--- Читатели ---\n"
7684
for item in self.__readers:
@@ -83,22 +91,27 @@ def add_reader(self, reader):
8391
self.readers.append(reader)
8492

8593
def del_reader(self, id_reader):
86-
reader = get_item_by_id(self.readers, id_reader)
87-
self.readers.remove(reader)
94+
index = get_index_by_id(self.readers, str(id_reader))
95+
if index == -1:
96+
print('Не найдена книга с указанным идентификатором')
97+
return False
98+
else:
99+
del self.readers[index]
100+
return True
88101

89102
def is_reader_exists(self, id_reader) -> bool:
90-
index_reader = get_index_by_id(self.__readers, id_reader)
103+
index_reader = get_index_by_id(self.__readers, str(id_reader))
91104
if index_reader == -1:
92105
return False
93106
else:
94107
return True
95108

96109
def get_value_of_reader_by_id(self, id_reader):
97-
reader = get_item_by_id(self.__readers, id_reader)
110+
reader = get_item_by_id(self.__readers, str(id_reader))
98111
return reader.copy()
99112

100113
def edit_reader(self, id_reader, reader):
101-
index_reader = get_index_by_id(self.readers, id_reader)
114+
index_reader = get_index_by_id(self.readers, str(id_reader))
102115
self.readers[index_reader] = reader
103116

104117
# endregion
@@ -112,12 +125,13 @@ def get_info_about_books_of_reader(self, id_reader) -> str:
112125
:param id_reader: ид читателя
113126
:return: строка
114127
"""
115-
index = get_index_by_id(self.__books, id_reader)
116-
reader = self.__readers[index]
117-
res = f"\n--- Книги читателя №{reader.id} ({reader.fio}) ---\n"
118-
for id_book in reader.books:
119-
index = get_index_by_id(self.__books, id_book)
120-
res += self.books[index].__str__()
128+
reader = get_item_by_id(self.__readers, str(id_reader))
129+
if len(reader.books) == 0:
130+
res = f"--- Нет книг у читателя №{reader.id} ({reader.fio}) ---"
131+
else:
132+
res = f"\n--- Книги читателя №{reader.id} ({reader.fio}) ---\n"
133+
for book in reader.books:
134+
res += book.__str__()
121135
return res
122136

123137
def took_book(self, reader, book, days=14):

Service/MenuService.py

Lines changed: 56 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from Data.BorrowedBook import BorrowedBook
1010
from Handler.LibraryHandler import LibraryHandler
1111
from Service.JsonService import CustomEncoder, library_hook_for_read_json
12-
from Service.ListService import get_index_by_id, get_item_by_id
12+
from Service.ListService import get_item_by_id
1313
from Service.TextFileService import write_library_to_string, read_library_from_string, text_to_date
1414

1515

@@ -23,16 +23,16 @@ def main_menu():
2323
Главное меню программы
2424
"""
2525

26-
file_type = choice_file_type()
26+
file_type = choice_file_type_menu()
2727
if file_type is None:
2828
return
2929
data = load_data(file_type)
30-
choice_category(data)
30+
choice_category_menu(data)
3131
save_data(file_type, data)
3232

3333

3434
# region Work with file
35-
def choice_file_type() -> FileType | None:
35+
def choice_file_type_menu() -> FileType | None:
3636
file_type = None
3737
is_processed = False
3838
while is_processed is False:
@@ -116,6 +116,7 @@ def __check_date(value) -> date | bool:
116116

117117

118118
def __input_int() -> int:
119+
option = ""
119120
is_processed = False
120121
while is_processed is False:
121122
option = input(">> ")
@@ -126,6 +127,7 @@ def __input_int() -> int:
126127

127128

128129
def __input_date() -> date:
130+
option = ""
129131
is_processed = False
130132
while is_processed is False:
131133
option = input(">> ")
@@ -138,7 +140,7 @@ def __input_date() -> date:
138140
# endregion
139141

140142

141-
def books(data: LibraryHandler):
143+
def books_menu(data: LibraryHandler):
142144
is_processed = False
143145
while is_processed is False:
144146
print("--- Книги ---")
@@ -243,20 +245,10 @@ def books(data: LibraryHandler):
243245
case "4":
244246
while is_processed is False:
245247
print("Введите идентификатор книги")
246-
print("0 - Назад")
247-
option = input(">> ")
248-
match option:
249-
case "0":
250-
print()
251-
case _:
252-
try:
253-
option = int(option)
254-
if data.del_book(option) is True:
255-
print('Книга удалена')
256-
is_processed = True
257-
except ValueError:
258-
print('Введите число!')
259-
is_processed = False
248+
option = __input_int()
249+
if data.del_book(option) is True:
250+
print('Книга удалена')
251+
is_processed = True
260252

261253
case "0":
262254
is_processed = True
@@ -267,7 +259,7 @@ def books(data: LibraryHandler):
267259
print()
268260

269261

270-
def readers(data):
262+
def readers_menu(data):
271263
is_processed = False
272264
while is_processed is False:
273265
print("--- Читатели ---")
@@ -284,21 +276,25 @@ def readers(data):
284276
print(data.get_info_about_readers())
285277

286278
case "2":
287-
print(get_item_by_id(id_reader))
288-
print(data.get_info_about_books_of_reader(id_reader))
279+
option = input(">> ")
280+
if get_item_by_id(data.readers, option) is None:
281+
print("Читатель не найден")
282+
else:
283+
print(data.get_info_about_reader(option))
284+
print(data.get_info_about_books_of_reader(option))
289285

290286
case "3":
291287
print("--- Новый читатель ---")
292288
print("Введите номер читательского билета")
293289
option = __input_int()
294-
reader = Book(option)
290+
reader = Reader(option)
295291

296292
print("Введите ФИО")
297293
option = input(">> ")
298294
reader.fio = option
299295

300-
print("Введите дату рождения")
301-
option = __input_date(">> ")
296+
print("Введите дату рождения в формате год-месяц-день")
297+
option = __input_date()
302298
reader.birthdate = option
303299

304300
print("Введите телефон")
@@ -311,7 +307,7 @@ def readers(data):
311307
is_processed = False
312308
case "4":
313309
print("--- Редактирование информации о читателе ---")
314-
print("Введите читательского билета")
310+
print("Введите номер читательского билета")
315311
option = __input_int()
316312
if data.is_reader_exists(option) is False:
317313
print('Не найден читатель с указанным читательским билетом')
@@ -322,95 +318,70 @@ def readers(data):
322318

323319
while is_processed is False:
324320
print("Выберите поле для редактирования")
325-
print("1 - Название")
326-
print("2 - ФИО")
327-
print("3 - Дата рождения")
328-
print("4 - Номер телефона")
329-
print("5 - Добавить книгу")
330-
print("6 - Удалить книгу")
331-
print("7 - Сохранить изменения")
321+
print("1 - ФИО")
322+
print("2 - Дата рождения")
323+
print("3 - Номер телефона")
324+
print("4 - Добавить книгу")
325+
print("5 - Удалить книгу")
326+
print("6 - Сохранить изменения")
332327
print("0 - Назад")
333328
option = input(">> ")
334329

335330
match option:
336331
case "1":
337-
print("Введите название")
338-
option = input(">> ")
339-
reader.title = option
340-
341-
case "2":
342332
print("Введите ФИО")
343333
option = input(">> ")
344334
reader.fio = option
345335

346-
case "3":
336+
case "2":
347337
print("Введите дату рождения в формате год-месяц-день")
348-
option = __input_date(">> ")
338+
option = __input_date()
349339
reader.birthdate = option
350340

351-
case "4":
341+
case "3":
352342
print("Введите номер телефона")
353343
option = input(">> ")
354344
reader.telephone = option
355345

356-
case "5":
346+
case "4":
357347
print("Введите ид книги")
358348
option = __input_int()
359349
if data.is_book_exists(option) is False:
360350
print("Книги с указанным ид не найдена в библиотеке")
361-
elif data.readers.is_book_exists(option) is True:
362-
print("Книга с указанным ид уже взята читателем")
351+
elif reader.is_book_exists(option) is True:
352+
print("Такая книга уже взята данным читателем")
363353
else:
364354
borrowed_book = BorrowedBook(option)
365355
print("Книга добавлена")
366356

367-
print("Введите дату взятия книги в формате год-месяц-день")
368-
option = __input_date(">> ")
369-
borrowed_book.date_borrowed = option
357+
print("Введите дату взятия книги в формате год-месяц-день")
358+
option = __input_date()
359+
borrowed_book.date_borrowed = option
370360

371-
reader.add_book(borrowed_book)
361+
reader.add_book(borrowed_book)
372362

373-
case "6":
363+
case "5":
374364
print("Введите ид книги")
375-
print("0 - Назад")
376-
option = input(">> ")
377-
match option:
378-
case "0":
379-
print()
380-
case _:
381-
try:
382-
option = int(option)
383-
if reader.del_book(option) is True:
384-
print('Книга удалена')
385-
is_processed = True
386-
except ValueError:
387-
print('Введите число!')
388-
389-
case "7":
390-
data.edit_book(id_reader, reader)
365+
option = __input_int()
366+
if reader.del_book(option) is True:
367+
print('Книга удалена')
368+
369+
case "6":
370+
data.edit_reader(id_reader, reader)
391371
print('Информация о читателе отредактирована')
392372
is_processed = True
373+
393374
case "0":
394375
is_processed = True
395376
is_processed = False
396377
case "5":
397-
while is_processed is False:
398-
print("Введите идентификатор читателя")
399-
print("0 - Назад")
400-
option = input(">> ")
401-
match option:
402-
case "0":
403-
print()
404-
case _:
405-
try:
406-
option = int(option)
407-
if data.del_reader(option) is True:
408-
print('Читатель удален')
409-
is_processed = True
410-
except ValueError:
411-
print('Введите число!')
412-
is_processed = False
413-
378+
print("Введите идентификатор читателя")
379+
option = __input_int()
380+
if data.is_reader_exists(option) is False:
381+
print("Не найден читатель с указанным читательским билетом")
382+
else:
383+
data.del_reader(option)
384+
print('Читатель удален')
414385
case "0":
415386
is_processed = True
416387

@@ -420,7 +391,7 @@ def readers(data):
420391
print()
421392

422393

423-
def choice_category(data):
394+
def choice_category_menu(data):
424395
is_processed = False
425396
while is_processed is False:
426397
print("--- Выберите категорию ---")
@@ -430,9 +401,9 @@ def choice_category(data):
430401
option = input(">> ")
431402
match option:
432403
case "1":
433-
books(data)
404+
books_menu(data)
434405
case "2":
435-
readers(data)
406+
readers_menu(data)
436407
case "0":
437408
is_processed = True
438409
case _:

0 commit comments

Comments
 (0)