-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrary.py
More file actions
95 lines (83 loc) · 2.91 KB
/
library.py
File metadata and controls
95 lines (83 loc) · 2.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
import json
# ---------------- Variables & Basic Syntax ----------------
library_name = "City Central Library"
total_books = 0
# ---------------- Nested Dictionary ----------------
library = {
"Fiction": {
"Harry Potter": {"author": "J.K. Rowling", "copies": 3},
"The Hobbit": {"author": "J.R.R. Tolkien", "copies": 2}
},
"Science": {
"A Brief History of Time": {"author": "Stephen Hawking", "copies": 4},
"The Selfish Gene": {"author": "Richard Dawkins", "copies": 1}
}
}
# ---------------- Functions ----------------
def display_books():
print(f"\n📚 {library_name} - Book List")
for category, books in library.items():
print(f"\nCategory: {category}")
for title, info in books.items():
print(f" {title} by {info['author']} - {info['copies']} copies")
def add_book(category, title, author, copies):
if category not in library:
library[category] = {}
library[category][title] = {"author": author, "copies": copies}
print(f"✅ '{title}' added under {category}.")
def borrow_book(title):
for category, books in library.items():
if title in books:
if books[title]["copies"] > 0:
books[title]["copies"] -= 1
print(f"📖 You borrowed '{title}'. Copies left: {books[title]['copies']}")
return
else:
print("❌ No copies left!")
return
print("❌ Book not found.")
# ---------------- JSON Save & Load ----------------
def save_data():
with open("library_data.json", "w") as f:
json.dump(library, f)
print("💾 Data saved.")
def load_data():
global library
try:
with open("library_data.json", "r") as f:
library = json.load(f)
print("📂 Data loaded.")
except FileNotFoundError:
print("⚠ No saved data found.")
# ---------------- Aggregation ----------------
def total_books_count():
total = sum(info["copies"] for books in library.values() for info in books.values())
print(f"📊 Total books in library: {total}")
# ---------------- Program Flow ----------------
load_data()
while True:
print("\n--- Library Menu ---")
print("1. Display Books")
print("2. Add Book")
print("3. Borrow Book")
print("4. Total Books Count")
print("5. Save & Exit")
choice = input("Enter choice: ")
if choice == "1":
display_books()
elif choice == "2":
cat = input("Enter category: ")
title = input("Enter title: ")
author = input("Enter author: ")
copies = int(input("Enter number of copies: "))
add_book(cat, title, author, copies)
elif choice == "3":
title = input("Enter title to borrow: ")
borrow_book(title)
elif choice == "4":
total_books_count()
elif choice == "5":
save_data()
break
else:
print("❌ Invalid choice. Try again.")