-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12.DesignLibraryManagementSystem.py
More file actions
107 lines (81 loc) · 3.06 KB
/
12.DesignLibraryManagementSystem.py
File metadata and controls
107 lines (81 loc) · 3.06 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
# Design a simple Library Management System that allows adding books, registering users,
# borrowing and returning books, and displaying available books — all in-memory without
# using any external database.
class Book:
def __init__(self, book_id, title, author):
self.book_id = book_id
self.title = title
self.author = author
self.is_borrowed = False
def __str__(self):
status = "Borrowed" if self.is_borrowed else "Available"
return f"{self.book_id} | {self.title} by {self.author} [{status}]"
class User:
def __init__(self, user_id, name):
self.user_id = user_id
self.name = name
self.borrowed_books = []
def __str__(self):
return f"{self.user_id} | {self.name} | Borrowed: {len(self.borrowed_books)}"
class Library:
def __init__(self):
self.books = {}
self.users = {}
def add_book(self, book_id, title, author):
if book_id not in self.books:
self.books[book_id] = Book(book_id, title, author)
print(f"Book '{title}' added successfully!")
else:
print("Book ID already exists.")
def register_user(self, user_id, name):
if user_id not in self.users:
self.users[user_id] = User(user_id, name)
print(f"User '{name}' registered successfully")
else:
print("User ID already exists.")
def borrow_book(self, user_id, book_id):
if user_id not in self.users:
print("Invalid User ID.")
return
if book_id not in self.books:
print("Invalid Book ID.")
return
book = self.books[book_id]
user = self.users[user_id]
if book.is_borrowed:
print(f"Sorry, '{book.title}' is already borrowed.")
else:
book.is_borrowed = True
user.borrowed_books.append(book)
print(f"'{book.title}' borrowed by {user.name}.")
def return_book(self, user_id, book_id):
if user_id not in self.users or book_id not in self.books:
print("Invalid ID's.")
return
user = self.users[user_id]
book = self.books[book_id]
if book in user.borrowed_books:
book.is_borrowed = False
user.borrowed_books.remove(book)
print(f"'{book.title}' returned by {user.name}.")
else:
print("Book not borrowed by the user.")
def show_books(self):
print("\n--- Library Books ---")
for b in self.books.values():
print(b)
if __name__ == "__main__":
library = Library()
# Add sample books
library.add_book(1, "Atomic Habits", "James Clear")
library.add_book(2, "Clean Code", "Robert Martin")
# Register users
library.register_user(101, "Alice")
library.register_user(102, "Bob")
# Borrow & return flow
library.borrow_book(101, 1)
library.show_books()
library.borrow_book(102, 1)
library.return_book(101, 1)
library.borrow_book(102, 1)
library.show_books()