-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbooks.py
More file actions
86 lines (70 loc) · 2.39 KB
/
books.py
File metadata and controls
86 lines (70 loc) · 2.39 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
# -*- coding: utf-8 -*-
"""
@author: admin
"""
# books.py
import uuid
from datetime import datetime
from models import Book
from storage import load_books, save_books
BOOKS_FILE = 'books.json'
def list_books():
books = load_books()
if not books:
print("No books available in the library.")
return
print("\n📚 Available Books:")
for book in books.values():
print(f"ID: {book.id} | Title: {book.title} | Author: {book.author} | Available: {book.available_copies}/{book.total_copies}")
def add_book():
title = input("Book Title: ")
author = input("Author: ")
publication_year = input("Publication Year: ")
try:
total_copies = int(input("Total Copies: "))
if total_copies < 0:
print("❌ Total copies cannot be negative.")
return
except ValueError:
print("❌ Invalid input for total copies. Book not added.")
return
book = Book(
id=str(uuid.uuid4()),
title=title,
author=author,
publication_year=publication_year,
total_copies=total_copies,
available_copies=total_copies
)
books = load_books()
books[book.id] = book
save_books(books)
print(f"✅ Book '{title}' added successfully!")
def view_books():
books = load_data(BOOKS_FILE)
if not books:
print("📚 No books available.")
return
print("\nAvailable Books:")
for book in books:
status = "Available ✅" if book['is_available'] else "Borrowed ❌"
print(f"📘 {book['title']} by {book['author']} ({book['publication_year']}) - {status}")
def delete_book():
books = load_books()
list_books()
book_id = input("Enter the ID of the book to delete: ")
if book_id not in books:
print("❌ Book ID not found.")
return
del books[book_id]
save_books(books)
print("✅ Book deleted successfully!")
def search_books():
books = load_books()
keyword = input("Enter title or author keyword: ").lower()
results = [book for book in books.values() if keyword in book.title.lower() or keyword in book.author.lower()]
if not results:
print("No matching books found.")
return
for book in results:
print(f"{book.title} by {book.author} (Available: {book.available_copies})")