-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
285 lines (241 loc) · 10.7 KB
/
Copy pathcli.py
File metadata and controls
285 lines (241 loc) · 10.7 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
"""
cli.py - Command Line Interface for Library Management System
Works perfectly with the encapsulated Library class in src/library.py
"""
import os
from datetime import date
from src.library import Library
from src.models import SimpleFinePolicy, FinePolicy
# ===== CUSTOM POLICIES FOR THE MENU =====
class ProgressiveFinePolicy(FinePolicy):
"""Charges $5/day for first 5 days, then $10/day after that."""
def calculate(self, days_late: int) -> float:
if days_late <= 0: return 0.0
if days_late <= 5: return float(days_late * 5.0)
return float((5 * 5.0) + ((days_late - 5) * 10.0))
class NoFinePolicy(FinePolicy):
"""Never charges a fine."""
def calculate(self, days_late: int) -> float:
return 0.0
# ===== CLI HELPERS =====
def clear_screen():
"""Clear terminal screen"""
os.system('cls' if os.name == 'nt' else 'clear')
def print_header(title):
"""Print formatted header"""
print("\n" + "=" * 50)
print(f" {title}")
print("=" * 50)
def main():
"""Main CLI function"""
# ===== SETUP =====
clear_screen()
print_header("LIBRARY MANAGEMENT SYSTEM SETUP")
print("\nChoose Fine Policy:")
print("1. Simple Fine ($5 per day)")
print("2. Progressive Fine ($5 initially, then $10)")
print("3. No Fine")
choice = input("\nEnter choice (1-3): ").strip()
if choice == "2":
library = Library(ProgressiveFinePolicy())
print("Progressive Fine Policy selected")
elif choice == "3":
library = Library(NoFinePolicy())
print("No Fine Policy selected")
else:
library = Library(SimpleFinePolicy())
print("Simple Fine Policy selected")
input("\nPress Enter to continue...")
# ===== MAIN LOOP =====
current_member = None
while True:
clear_screen()
print_header("LIBRARY MANAGEMENT SYSTEM")
# Safe encapsulated access to the current member
if current_member:
try:
member = library.get_member(current_member)
print(f"\n Logged in: {member.name} ({current_member})")
print(f" Books borrowed: {len(member.borrowed_books)}/3")
except KeyError:
current_member = None
else:
print("\nNot logged in")
print("\n=== MAIN MENU ===")
print("1. Book Operations")
print("2. Member Operations")
print("3. Borrow/Return")
print("4. Login/Logout")
print("0. Exit")
cmd = input("\nChoice: ").strip()
# ===== BOOK OPERATIONS =====
if cmd == "1":
while True:
clear_screen()
print_header("BOOK OPERATIONS")
print("1. Add Book")
print("2. List All Books")
print("3. List Available Books")
print("4. Search Book")
print("0. Back")
sub = input("\nChoice: ").strip()
if sub == "1": # Add Book
book_id = input("Book ID: ")
title = input("Title: ")
author = input("Author: ")
try:
library.add_book(book_id, title, author)
print(f"Book '{title}' added")
except ValueError as e:
print(f" {e}")
elif sub == "2": # List All Books
print("\n--- ALL BOOKS ---")
# Use public method list_all_books() instead of private dictionary
for book in library.list_all_books():
# is_available is a property, no () needed
status = " Available" if book.is_available else "✗ Borrowed"
print(f"{book.book_id}: {book.title} by {book.author} [{status}]")
elif sub == "3": # List Available Books
print("\n--- AVAILABLE BOOKS ---")
available = library.list_available_books()
if available:
for book in available:
print(f"{book.book_id}: {book.title} by {book.author}")
else:
print("No books available")
elif sub == "4": # Search Book
book_id = input("Enter Book ID: ")
try:
book = library.get_book(book_id)
print(f"\nTitle: {book.title}")
print(f"Author: {book.author}")
print(f"Status: {'Available' if book.is_available else 'Borrowed'}")
except KeyError:
print(" Book not found")
elif sub == "0":
break
input("\nPress Enter...")
# ===== MEMBER OPERATIONS =====
elif cmd == "2":
while True:
clear_screen()
print_header("MEMBER OPERATIONS")
print("1. Add Member")
print("2. List All Members")
print("3. Search Member")
print("0. Back")
sub = input("\nChoice: ").strip()
if sub == "1": # Add Member
member_id = input("Member ID: ")
name = input("Name: ")
try:
library.add_member(member_id, name)
print(f" Member '{name}' added")
except ValueError as e:
print(f" {e}")
elif sub == "2": # List All Members
print("\n--- ALL MEMBERS ---")
for member in library.list_all_members():
print(f"{member.member_id}: {member.name} [{len(member.borrowed_books)}/3 books]")
elif sub == "3": # Search Member
member_id = input("Enter Member ID: ")
try:
member = library.get_member(member_id)
print(f"\nName: {member.name}")
print(f"Books borrowed: {len(member.borrowed_books)}/3")
if member.borrowed_books:
print("\nCurrently reading:")
for bid in member.borrowed_books:
try:
book = library.get_book(bid)
print(f" • {book.title}")
except KeyError:
pass
except KeyError:
print(" Member not found")
elif sub == "0":
break
input("\nPress Enter...")
# ===== BORROW/RETURN =====
elif cmd == "3":
if not current_member:
print("Please login first!")
input("\nPress Enter...")
continue
while True:
clear_screen()
print_header("BORROW/RETURN")
print("1. Borrow Book")
print("2. Return Book")
print("3. My Borrowed Books")
print("0. Back")
sub = input("\nChoice: ").strip()
if sub == "1": # Borrow
available = library.list_available_books()
if available:
print("\nAvailable books:")
for book in available:
print(f" {book.book_id}: {book.title}")
book_id = input("\nBook ID to borrow: ")
try:
library.borrow_book(current_member, book_id, date.today())
print("Book borrowed successfully!")
except (KeyError, ValueError) as e:
print(f"{e}")
elif sub == "2": # Return
member = library.get_member(current_member)
if member.borrowed_books:
print("\nYour books:")
for bid in member.borrowed_books:
try:
book = library.get_book(bid)
print(f" {bid}: {book.title}")
except KeyError:
pass
book_id = input("\nBook ID to return: ")
try:
fine = library.return_book(current_member, book_id, date.today())
if fine > 0:
print(f" Book returned. Fine: ₹{fine:.2f}")
else:
print(" Book returned on time!")
except (KeyError, ValueError) as e:
print(f"{e}")
elif sub == "3": # My Books
member = library.get_member(current_member)
if member.borrowed_books:
print("\nBooks you're reading:")
for bid in member.borrowed_books:
try:
book = library.get_book(bid)
print(f" • {book.title} by {book.author}")
except KeyError:
pass
else:
print("You haven't borrowed any books")
elif sub == "0":
break
input("\nPress Enter...")
# ===== LOGIN/LOGOUT =====
elif cmd == "4":
if current_member:
print(f"Logged out from {current_member}")
current_member = None
else:
member_id = input("Enter Member ID: ")
try:
member = library.get_member(member_id)
current_member = member_id
print(f"Logged in as {member.name}")
except KeyError:
print("Member not found")
input("\nPress Enter...")
# ===== EXIT =====
elif cmd == "0":
print("\nThanks for using the Library System!")
break
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n\nGoodbye!")