-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary Managment System (project).py
More file actions
169 lines (151 loc) · 5.91 KB
/
Library Managment System (project).py
File metadata and controls
169 lines (151 loc) · 5.91 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
# Full updated code with Admin Panel feature
import math
import datetime
import random
import os
import pyfiglet
class User:
def __init__(self, username, password):
self.username = username
self.password = password
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
self.is_borrowed = False
class Library:
def __init__(self):
self.books = [
Book("1984", "George Orwell"),
Book("Python Programming", "John Zelle"),
Book("A Brief History of Time", "Stephen Hawking")
]
self.records_file = "library_records.txt"
if not os.path.exists(self.records_file):
open(self.records_file, 'w').close()
def display_books(self):
print("\nAvailable Books:")
for book in self.books:
status = "Borrowed" if book.is_borrowed else "Available"
print(f"- {book.title} by {book.author} [{status}]")
def search_books(self, keyword):
print(f"\nSearch Results for '{keyword}':")
found = False
for book in self.books:
if keyword.lower() in book.title.lower() or keyword.lower() in book.author.lower():
status = "Borrowed" if book.is_borrowed else "Available"
print(f"- {book.title} by {book.author} [{status}]")
found = True
if not found:
print("No books found.")
def borrow_book(self, title, username):
for book in self.books:
if book.title.lower() == title.lower() and not book.is_borrowed:
book.is_borrowed = True
date = datetime.datetime.now()
fine = round(random.uniform(0, 10), 2)
self.save_record(f"{username} borrowed '{book.title}' on {date.strftime('%Y-%m-%d %H:%M:%S')} | Fine Estimation: ${fine}\n")
print(f"\nYou borrowed '{book.title}'. Estimated fine if late: ${fine}")
return
print("Book is not available or doesn't exist.")
def return_book(self, title, username):
for book in self.books:
if book.title.lower() == title.lower() and book.is_borrowed:
book.is_borrowed = False
date = datetime.datetime.now()
late_days = random.randint(0, 10)
fine = math.ceil(late_days * 1.5)
self.save_record(f"{username} returned '{book.title}' on {date.strftime('%Y-%m-%d %H:%M:%S')} | Late: {late_days} days | Fine: ${fine}\n")
print(f"\nYou returned '{book.title}'. Late by {late_days} days. Fine: ${fine}")
return
print("Book not found or not borrowed.")
def save_record(self, record):
with open(self.records_file, "a") as file:
file.write(record)
def admin_panel(self):
print("\n===== Admin Panel =====")
with open(self.records_file, 'r') as file:
data = file.read()
if data:
print(data)
else:
print("No records found.")
class AuthSystem:
def __init__(self):
self.users_file = "users.txt"
self.users = self.load_users()
def load_users(self):
users = {}
if os.path.exists(self.users_file):
with open(self.users_file, 'r') as file:
for line in file:
username, password = line.strip().split(',')
users[username] = password
return users
def signup(self):
print("\n--- Sign Up ---")
username = input("Enter username: ")
if username in self.users:
print("Username already exists.")
return None
password = input("Enter password: ")
with open(self.users_file, 'a') as file:
file.write(f"{username},{password}\n")
self.users[username] = password
print("Signup successful. Please login.")
return None
def login(self):
print("\n--- Login ---")
username = input("Enter username: ")
password = input("Enter password: ")
if self.users.get(username) == password:
print("Login successful.")
return username
else:
print("Invalid credentials.")
return None
def print_ascii(text):
print(pyfiglet.figlet_format(text))
def main():
print_ascii("Library System")
library = Library()
auth = AuthSystem()
current_user = None
while True:
if not current_user:
print("\n1. Login\n2. Sign Up\n3. Exit")
choice = input("Choose an option: ")
if choice == '1':
current_user = auth.login()
elif choice == '2':
auth.signup()
elif choice == '3':
break
else:
print("Invalid choice.")
else:
print_ascii(f"Welcome {current_user}")
print("1. View Books\n2. Search Book\n3. Borrow Book\n4. Return Book\n5. Admin Panel\n6. Logout")
option = input("Choose an option: ")
if option == '1':
library.display_books()
elif option == '2':
keyword = input("Enter keyword to search: ")
library.search_books(keyword)
elif option == '3':
title = input("Enter book title to borrow: ")
library.borrow_book(title, current_user)
elif option == '4':
title = input("Enter book title to return: ")
library.return_book(title, current_user)
elif option == '5':
if current_user.lower() == "admin":
library.admin_panel()
else:
print("Access denied. Only admin can view records.")
elif option == '6':
current_user = None
else:
print("Invalid option.")
if __name__ == "__main__":
main()