-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreservations_and_fines.py
132 lines (113 loc) · 4.66 KB
/
reservations_and_fines.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
from datetime import datetime
def reserve_book(cursor, conn):
title = input("Enter the title of the book to reserve: ")
user_library_id = input("Enter your library ID: ")
try:
# Check if the book exists
cursor.execute("SELECT id, availability FROM books WHERE title = %s", (title,))
book_result = cursor.fetchone()
if not book_result:
print("Book not found.")
return
book_id, availability = book_result
if availability:
print("Book is currently available. You can borrow it instead.")
return
# Check if the user exists
cursor.execute("SELECT id FROM users WHERE library_id = %s", (user_library_id,))
user_id_result = cursor.fetchone()
if not user_id_result:
print("User not found.")
return
user_id = user_id_result[0]
# Check if the user already has a reservation for this book
cursor.execute(
"SELECT id FROM reservations WHERE user_id = %s AND book_id = %s",
(user_id, book_id)
)
existing_reservation = cursor.fetchone()
if existing_reservation:
print("You already have a reservation for this book.")
return
# Create a reservation
reservation_date = datetime.now().date()
cursor.execute(
"INSERT INTO reservations (user_id, book_id, reservation_date) VALUES (%s, %s, %s)",
(user_id, book_id, reservation_date)
)
conn.commit()
print("Book reserved successfully!")
except mysql.connector.Error as err:
print(f"Error reserving book: {err}")
conn.rollback()
def check_reservations_for_book(cursor, conn, book_id):
"""
Checks for pending reservations for the specified book and notifies the next user in line.
"""
try:
# Fetch the oldest reservation for the book
cursor.execute(
"SELECT r.id, r.user_id, b.title "
"FROM reservations r "
"JOIN books b ON r.book_id = b.id "
"WHERE r.book_id = %s "
"ORDER BY r.reservation_date ASC "
"LIMIT 1",
(book_id,)
)
reservation = cursor.fetchone()
if reservation:
reservation_id, user_id, book_title = reservation
# Fetch user's name
cursor.execute("SELECT name FROM users WHERE id = %s", (user_id,))
user_name = cursor.fetchone()[0]
print(f"Notification: Book '{book_title}' is now available for user {user_name}.")
# Delete the reservation
cursor.execute("DELETE FROM reservations WHERE id = %s", (reservation_id,))
conn.commit()
except mysql.connector.Error as err:
print(f"Error checking reservations: {err}")
conn.rollback()
def calculate_fine(cursor, user_library_id):
# Fetch all overdue books for the user
cursor.execute(
"SELECT bb.borrow_date "
"FROM borrowed_books bb "
"JOIN users u ON bb.user_id = u.id "
"WHERE u.library_id = %s AND bb.return_date IS NULL AND DATE_ADD(bb.borrow_date, INTERVAL 14 DAY) < CURDATE()",
(user_library_id,)
)
overdue_books = cursor.fetchall()
total_fine = 0
for borrow_date, in overdue_books:
days_overdue = (datetime.now().date() - borrow_date).days
fine = days_overdue * 1 # $1 fine per day
total_fine += fine
return total_fine
def pay_fine(cursor, conn, calculate_fine_func):
user_library_id = input("Enter your library ID: ")
fine = calculate_fine_func(cursor, user_library_id)
if fine > 0:
print(f"Total fine: ${fine}")
while True:
try:
payment = float(input("Enter payment amount: "))
if payment >= fine:
try:
cursor.execute(
"INSERT INTO fines (user_id, fine_amount, payment_status, payment_date) "
"VALUES ((SELECT id FROM users WHERE library_id = %s), %s, 'paid', CURDATE())",
(user_library_id, fine)
)
conn.commit()
print("Fine paid successfully!")
break
except mysql.connector.Error as err:
print(f"Error recording fine payment: {err}")
conn.rollback()
else:
print("Insufficient payment. Please pay the full amount.")
except ValueError:
print("Invalid payment amount. Please enter a number.")
else:
print("No fines to pay.")