-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser.py
48 lines (39 loc) · 1.56 KB
/
user.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
import re
from datetime import datetime
class User:
def __init__(self, name, library_id):
self.__name = name
self.__library_id = library_id
self.__borrowed_books = []
def get_name(self):
return self.__name
def get_library_id(self):
return self.__library_id
def get_borrowed_books(self):
return [book.get_book_title() for book in self.__borrowed_books]
def borrow_book(self, borrowed_book):
self.__borrowed_books.append(borrowed_book)
def return_book(self, book_title):
for borrowed_book in self.__borrowed_books:
if borrowed_book.get_book_title() == book_title:
self.__borrowed_books.remove(borrowed_book)
return
print("Book not found in borrowed books.")
def clear_fines(self):
# Add a method to clear fines
self.__borrowed_books = [book for book in self.__borrowed_books if datetime.now() <= book.get_due_date()]
@staticmethod
def calculate_fine(user):
total_fine = 0
for borrowed_book in user.__borrowed_books:
due_date = borrowed_book.get_due_date()
if datetime.now() > due_date:
days_overdue = (datetime.now() - due_date).days
fine = days_overdue * 1 # $1 fine per day
total_fine += fine
return total_fine
@staticmethod
def validate_library_id(library_id):
if not re.match(r"^\d{5}$", library_id):
raise ValueError("Invalid library ID format. Should be 5 digits.")
return library_id