-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12.DesignLibraryManagementSystem.java
More file actions
178 lines (149 loc) · 5.63 KB
/
12.DesignLibraryManagementSystem.java
File metadata and controls
178 lines (149 loc) · 5.63 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
170
171
172
173
174
175
176
177
178
/* ---------------------------------------------------------------------------- */
/* ( The Authentic JS/JAVA CodeBuff )
___ _ _ _
| _ ) |_ __ _ _ _ __ _ __| |_ __ ____ _ (_)
| _ \ ' \/ _` | '_/ _` / _` \ V V / _` || |
|___/_||_\__,_|_| \__,_\__,_|\_/\_/\__,_|/ |
|__/
*/
/* -------------------------------------------------------------------------- */
/* Youtube: https://youtube.com/@code-with-Bharadwaj */
/* Github : https://github.com/Manu577228 */
/* Portfolio : https://manu-bharadwaj-portfolio.vercel.app/portfolio */
/* ----------------------------------------------------------------------- */
import java.io.*;
import java.util.*;
/* ---------------------- Library Management System ---------------------- */
/* 1) Book, User, Library classes designed in-memory with HashMaps. */
/* 2) Demonstrates add, borrow, return, and list operations concisely. */
class Book {
int bookId;
String title;
String author;
boolean isBorrowed;
Book(int bookId, String title, String author) {
this.bookId = bookId;
this.title = title;
this.author = author;
this.isBorrowed = false;
}
@Override
public String toString() {
return bookId + " | " + title + " by " + author + " [" + (isBorrowed ? "Borrowed" : "Available") + "]";
}
}
class User {
int userId;
String name;
List<Book> borrowedBooks;
User(int userId, String name) {
this.userId = userId;
this.name = name;
this.borrowedBooks = new ArrayList<>();
}
@Override
public String toString() {
return userId + " | " + name + " | Borrowed: " + borrowedBooks.size();
}
}
class Library {
Map<Integer, Book> books;
Map<Integer, User> users;
Library() {
books = new HashMap<>();
users = new HashMap<>();
}
// Add new book
void addBook(int bookId, String title, String author) {
if (!books.containsKey(bookId)) {
books.put(bookId, new Book(bookId, title, author));
System.out.println("Book '" + title + "' added successfully!");
} else System.out.println("Book ID already exists.");
}
// Register user
void registerUser(int userId, String name) {
if (!users.containsKey(userId)) {
users.put(userId, new User(userId, name));
System.out.println("User '" + name + "' registered successfully!");
} else System.out.println("User ID already exists.");
}
// Borrow book
void borrowBook(int userId, int bookId) {
if (!users.containsKey(userId)) {
System.out.println("Invalid User ID.");
return;
}
if (!books.containsKey(bookId)) {
System.out.println("Invalid Book ID.");
return;
}
Book book = books.get(bookId);
User user = users.get(userId);
if (book.isBorrowed)
System.out.println("Sorry, '" + book.title + "' is already borrowed.");
else {
book.isBorrowed = true;
user.borrowedBooks.add(book);
System.out.println("'" + book.title + "' borrowed by " + user.name + ".");
}
}
// Return book
void returnBook(int userId, int bookId) {
if (!users.containsKey(userId) || !books.containsKey(bookId)) {
System.out.println("Invalid IDs.");
return;
}
User user = users.get(userId);
Book book = books.get(bookId);
if (user.borrowedBooks.contains(book)) {
book.isBorrowed = false;
user.borrowedBooks.remove(book);
System.out.println("'" + book.title + "' returned by " + user.name + ".");
} else System.out.println("Book not borrowed by user.");
}
// Display all books
void showBooks() {
System.out.println("\n--- Library Books ---");
for (Book b : books.values()) System.out.println(b);
}
}
/* ---------------------- DEMO EXECUTION ---------------------- */
public class LibrarySystemDemo {
public static void main(String[] args) throws Exception {
Library library = new Library();
// Add sample books
library.addBook(1, "Atomic Habits", "James Clear");
library.addBook(2, "Clean Code", "Robert Martin");
// Register users
library.registerUser(101, "Alice");
library.registerUser(102, "Bob");
// Borrow & Return flow
library.borrowBook(101, 1);
library.showBooks();
library.borrowBook(102, 1);
library.returnBook(101, 1);
library.borrowBook(102, 1);
library.showBooks();
}
}
/* ---------------------- OUTPUT EXPLANATION ----------------------
Book 'Atomic Habits' added successfully!
Book 'Clean Code' added successfully!
User 'Alice' registered successfully!
User 'Bob' registered successfully!
'Atomic Habits' borrowed by Alice.
--- Library Books ---
1 | Atomic Habits by James Clear [Borrowed]
2 | Clean Code by Robert Martin [Available]
Sorry, 'Atomic Habits' is already borrowed.
'Atomic Habits' returned by Alice.
'Atomic Habits' borrowed by Bob.
--- Library Books ---
1 | Atomic Habits by James Clear [Borrowed]
2 | Clean Code by Robert Martin [Available]
----------------------------------------------------------------- */
/* ---------------------- EXPLANATION ----------------------
1) Each Book/User stored in HashMap for O(1) access.
2) Borrow/Return just flips a boolean and updates user list.
3) Library runs fully in-memory with clear readable logic.
----------------------------------------------------------------- */