-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary.java
More file actions
212 lines (186 loc) · 7.31 KB
/
Library.java
File metadata and controls
212 lines (186 loc) · 7.31 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.dja.project;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.*;
/**
*
* @author nianc
*/
public class Library {
HashMap<String, Book> books = new HashMap<>();
HashMap<String, Users> users = new HashMap<>();
// Add a new book to the library
public void addBook(String title, int copies) {
if (books.containsKey(title)) {
System.out.println("Book already exists: " + title);
} else {
books.put(title, new Book(title, copies));
System.out.println("Book added: " + title + " (" + copies + " copies)");
}
}
// Register a new user
public void addUser(String userId) {
if (users.containsKey(userId)) {
System.out.println("User already exists: " + userId);
} else {
users.put(userId, new Users(userId));
System.out.println("User added: " + userId);
}
}
public List<String> borrowedBooks(String userId) {
Users user = users.get(userId);
List<String> borrowedList = new ArrayList<>();
if (user == null) {
System.out.println("User not found: " + userId);
return borrowedList;
}
for (Book b : user.borrowedBooks) {
borrowedList.add(b.title);
}
return borrowedList;
}
// Borrow a book
public void borrowBook(String userId, String title) {
Users user = users.get(userId);
Book book = books.get(title);
if (user == null) {
System.out.println("User not found: " + userId);
return;
}
if (book == null) {
System.out.println("Book not found: " + title);
return;
}
// Check if user already borrowed the book
if (user.borrowedBooks.contains(book)) {
System.out.println(userId + " already borrowed " + title);
return;
}
// If copies are available, borrow immediately
if (book.available > 0) {
book.available--;
user.borrowedBooks.add(book);
System.out.println(userId + " borrowed " + title);
} else {
// Otherwise, join the waitlist if not already there
if (book.waitlist.contains(user)) {
System.out.println(userId + " is already on the waitlist for " + title);
} else {
book.waitlist.add(user);
System.out.println(title + " unavailable. " + userId + " added to waitlist.");
}
}
}
// Return a book
public void returnBook(String userId, String title) {
Users user = users.get(userId);
Book book = books.get(title);
if (user == null) {
System.out.println("User not found: " + userId);
return;
}
if (book == null) {
System.out.println("Book not found: " + title);
return;
}
// Check if user actually borrowed the book
if (!user.borrowedBooks.contains(book)) {
System.out.println(userId + " did not borrow " + title);
return;
}
// Remove book from user's borrowed list
user.borrowedBooks.remove(book);
// Handle waitlist or increase available copies
if (book.waitlist.isEmpty()) {
book.available++;
System.out.println(userId + " returned " + title + ". Copy added back to library.");
} else {
Users nextUser = book.waitlist.poll(); // Dequeue first waiting user
nextUser.borrowedBooks.add(book);
System.out.println(userId + " returned " + title + ". It has been assigned to " + nextUser.userId + ".");
}
}
// Display borrowed books of a user
public void viewBorrowedBooks(String userId) {
Users user = users.get(userId);
if (user == null) {
System.out.println("User not found: " + userId);
return;
}
System.out.println("\nBooks borrowed by " + userId + ":");
if (user.borrowedBooks.isEmpty()) {
System.out.println("No books currently borrowed.");
} else {
for (Book b : user.borrowedBooks) {
System.out.println("- " + b.title);
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Library library = new Library();
boolean running = true;
System.out.println(" Welcome to the Library System!");
while (running) {
System.out.println("\n---- MENU ----");
System.out.println("1. Add Book");
System.out.println("2. Add User");
System.out.println("3. Borrow Book");
System.out.println("4. Return Book");
System.out.println("5. Check Book Availability");
System.out.println("6. View Borrowed Books");
System.out.println("7. Exit");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
sc.nextLine(); // consume newline
switch (choice) {
case 1 -> {
System.out.print("Enter book title: ");
String title = sc.nextLine();
System.out.print("Enter number of copies: ");
int copies = sc.nextInt();
library.addBook(title, copies);
}
case 2 -> {
System.out.print("Enter user ID: ");
String userId = sc.nextLine();
library.addUser(userId);
}
case 3 -> {
System.out.print("Enter user ID: ");
String userId = sc.nextLine();
System.out.print("Enter book title: ");
String title = sc.nextLine();
library.borrowBook(userId, title);
}
case 4 -> {
System.out.print("Enter user ID: ");
String userId = sc.nextLine();
System.out.print("Enter book title: ");
String title = sc.nextLine();
library.returnBook(userId, title);
}
case 5 -> {
System.out.print("Enter book title to check: ");
String title = sc.nextLine();
library.checkAvailability(title);
}
case 6 -> {
System.out.print("Enter user ID: ");
String userId = sc.nextLine();
library.viewBorrowedBooks(userId);
}
case 7 -> {
System.out.println("Exiting Library System. Goodbye!");
running = false;
}
default -> System.out.println("Invalid choice. Try again.");
}
}
sc.close();
}
}