Repository navigation
Expand file tree
/
Copy pathLibraryManagementSystem.py
More file actions
147 lines (127 loc) · 5.03 KB
/
Copy pathLibraryManagementSystem.py
File metadata and controls
147 lines (127 loc) · 5.03 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
import json
import os
DATA_FILE = "library_data.json"
# ------------------ Helper Functions ------------------
def load_data():
if os.path.exists(DATA_FILE):
with open(DATA_FILE, "r") as f:
return json.load(f)
else:
return {"books": [], "borrowed": []}
def save_data(data):
with open(DATA_FILE, "w") as f:
json.dump(data, f, indent=4)
def add_book():
data = load_data()
title = input("Enter book title: ").strip()
author = input("Enter author name: ").strip()
genre = input("Enter genre: ").strip()
for book in data["books"]:
if book["title"].lower() == title.lower() and book["author"].lower() == author.lower():
print("This book already exists in the library!")
return
data["books"].append({"title": title, "author": author, "genre": genre})
save_data(data)
print(f"✅ '{title}' by {author} added to the library.")
def remove_book():
data = load_data()
title = input("Enter book title to remove: ").strip()
author = input("Enter author name: ").strip()
for book in data["books"]:
if book["title"].lower() == title.lower() and book["author"].lower() == author.lower():
data["books"].remove(book)
save_data(data)
print(f"✅ '{title}' by {author} removed from the library.")
return
print("Book not found!")
def search_books():
data = load_data()
query = input("Enter title or author to search: ").strip().lower()
results = [book for book in data["books"] if query in book["title"].lower() or query in book["author"].lower()]
if results:
print("\nBooks found:")
for i, book in enumerate(results, start=1):
print(f"{i}. {book['title']} by {book['author']} ({book['genre']})")
else:
print("No books found.")
def borrow_book():
data = load_data()
title = input("Enter title of the book to borrow: ").strip()
author = input("Enter author of the book: ").strip()
borrower = input("Enter your name: ").strip()
for book in data["books"]:
if book["title"].lower() == title.lower() and book["author"].lower() == author.lower():
# Check if already borrowed
for b in data["borrowed"]:
if b["title"].lower() == title.lower() and b["author"].lower() == author.lower():
print("Sorry, this book is already borrowed.")
return
data["borrowed"].append({"title": title, "author": author, "borrower": borrower})
save_data(data)
print(f"✅ {borrower} borrowed '{title}' by {author}.")
return
print("Book not found!")
def return_book():
data = load_data()
title = input("Enter title of the book to return: ").strip()
author = input("Enter author of the book: ").strip()
borrower = input("Enter your name: ").strip()
for b in data["borrowed"]:
if b["title"].lower() == title.lower() and b["author"].lower() == author.lower() and b["borrower"].lower() == borrower.lower():
data["borrowed"].remove(b)
save_data(data)
print(f"✅ {borrower} returned '{title}' by {author}.")
return
print("No record found for this borrowed book.")
def view_books():
data = load_data()
if not data["books"]:
print("Library is empty!")
return
print("\nAll Books in Library:")
for i, book in enumerate(data["books"], start=1):
borrowed = any(b["title"].lower() == book["title"].lower() and b["author"].lower() == book["author"].lower() for b in data["borrowed"])
status = "Borrowed" if borrowed else "Available"
print(f"{i}. {book['title']} by {book['author']} ({book['genre']}) - {status}")
def view_borrowed():
data = load_data()
if not data["borrowed"]:
print("No books are currently borrowed.")
return
print("\nBorrowed Books:")
for i, b in enumerate(data["borrowed"], start=1):
print(f"{i}. '{b['title']}' by {b['author']} borrowed by {b['borrower']}")
# ------------------ Main Menu ------------------
def main():
while True:
print("\n=== Library Management System ===")
print("1. View all books")
print("2. Add a book")
print("3. Remove a book")
print("4. Search books")
print("5. Borrow a book")
print("6. Return a book")
print("7. View borrowed books")
print("8. Exit")
choice = input("Enter choice (1-8): ").strip()
if choice == '1':
view_books()
elif choice == '2':
add_book()
elif choice == '3':
remove_book()
elif choice == '4':
search_books()
elif choice == '5':
borrow_book()
elif choice == '6':
return_book()
elif choice == '7':
view_borrowed()
elif choice == '8':
print("Goodbye! 📚")
break
else:
print("Invalid input! Please choose a number between 1-8.")
if __name__ == '__main__':
main()