-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment4.java
More file actions
387 lines (338 loc) · 14.1 KB
/
Assignment4.java
File metadata and controls
387 lines (338 loc) · 14.1 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import java.io.*;
import java.util.*;
class Book implements Serializable, Comparable<Book> {
private static final long serialVersionUID = 1L;
private int bookId;
private String title;
private String author;
private String category;
private boolean isIssued;
public Book(int bookId, String title, String author, String category) {
this.bookId = bookId;
this.title = title;
this.author = author;
this.category = category;
this.isIssued = false;
}
public int getBookId() { return bookId; }
public String getTitle() { return title; }
public String getAuthor() { return author; }
public String getCategory() { return category; }
public boolean isIssued() { return isIssued; }
public void markAsIssued() { this.isIssued = true; }
public void markAsReturned() { this.isIssued = false; }
public void displayBookDetails() {
System.out.println("ID: " + bookId + ", Title: " + title + ", Author: " + author + ", Category: " + category + ", Status: " + (isIssued ? "Issued" : "Available"));
}
@Override
public int compareTo(Book other) {
return this.title.compareToIgnoreCase(other.title);
}
}
class Member implements Serializable {
private static final long serialVersionUID = 1L;
private int memberId;
private String name;
private String email;
private List<Integer> issuedBooks;
public Member(int memberId, String name, String email) {
this.memberId = memberId;
this.name = name;
this.email = email;
this.issuedBooks = new ArrayList<>();
}
public int getMemberId() { return memberId; }
public String getName() { return name; }
public List<Integer> getIssuedBooks() { return issuedBooks; }
public void addIssuedBook(int bookId) { issuedBooks.add(bookId); }
public boolean returnIssuedBook(int bookId) {
return issuedBooks.remove(Integer.valueOf(bookId));
}
public void displayMemberDetails() {
System.out.println("ID: " + memberId + ", Name: " + name + ", Email: " + email);
System.out.println("Books Issued: " + issuedBooks.size() + " " + issuedBooks);
}
}
class BookAuthorComparator implements Comparator<Book> {
@Override
public int compare(Book b1, Book b2) {
return b1.getAuthor().compareToIgnoreCase(b2.getAuthor());
}
}
class BookCategoryComparator implements Comparator<Book> {
@Override
public int compare(Book b1, Book b2) {
return b1.getCategory().compareToIgnoreCase(b2.getCategory());
}
}
public class Main {
private Map<Integer, Book> books;
private Map<Integer, Member> members;
private Scanner scanner;
private static final String BOOKS_FILE = "books.dat";
private static final String MEMBERS_FILE = "members.dat";
public Main() {
books = new HashMap<>();
members = new HashMap<>();
scanner = new Scanner(System.in);
loadFromFile();
}
private void loadFromFile() {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(BOOKS_FILE))) {
books = (Map<Integer, Book>) ois.readObject();
System.out.println("Books loaded successfully from " + BOOKS_FILE);
} catch (FileNotFoundException e) {
System.out.println("Book data file not found. Starting with empty book collection.");
} catch (IOException | ClassNotFoundException e) {
System.out.println("Error loading book data: " + e.getMessage());
}
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(MEMBERS_FILE))) {
members = (Map<Integer, Member>) ois.readObject();
System.out.println("Members loaded successfully from " + MEMBERS_FILE);
} catch (FileNotFoundException e) {
System.out.println("Member data file not found. Starting with empty member collection.");
} catch (IOException | ClassNotFoundException e) {
System.out.println("Error loading member data: " + e.getMessage());
}
}
private void saveToFile() {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(BOOKS_FILE))) {
oos.writeObject(books);
System.out.println("Books saved successfully to " + BOOKS_FILE);
} catch (IOException e) {
System.out.println("Error saving books: " + e.getMessage());
}
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(MEMBERS_FILE))) {
oos.writeObject(members);
System.out.println("Members saved successfully to " + MEMBERS_FILE);
} catch (IOException e) {
System.out.println("Error saving members: " + e.getMessage());
}
}
private int getNextId(Map<Integer, ?> map) {
if (map.isEmpty()) return 101;
return Collections.max(map.keySet()) + 1;
}
public void addBook() {
int id = getNextId(books);
System.out.print("Enter Book Title: ");
String title = scanner.nextLine();
System.out.print("Enter Author: ");
String author = scanner.nextLine();
System.out.print("Enter Category: ");
String category = scanner.nextLine();
Book newBook = new Book(id, title, author, category);
books.put(id, newBook);
saveToFile();
System.out.println("Book added successfully with ID: " + id);
}
public void addMember() {
int id = getNextId(members);
System.out.print("Enter Member Name: ");
String name = scanner.nextLine();
System.out.print("Enter Email: ");
String email = scanner.nextLine();
Member newMember = new Member(id, name, email);
members.put(id, newMember);
saveToFile();
System.out.println("Member added successfully with ID: " + id);
}
public void issueBook() {
try {
System.out.print("Enter Member ID: ");
int memberId = scanner.nextInt();
System.out.print("Enter Book ID to issue: ");
int bookId = scanner.nextInt();
scanner.nextLine();
Member member = members.get(memberId);
Book book = books.get(bookId);
if (member == null) {
System.out.println("Error: Member not found with ID " + memberId);
return;
}
if (book == null) {
System.out.println("Error: Book not found with ID " + bookId);
return;
}
if (book.isIssued()) {
System.out.println("Error: Book is already issued.");
return;
}
book.markAsIssued();
member.addIssuedBook(bookId);
saveToFile();
System.out.println("Book " + book.getTitle() + " issued to " + member.getName() + " successfully.");
} catch (InputMismatchException e) {
System.out.println("Error: Invalid input. IDs must be integers.");
scanner.nextLine();
}
}
public void returnBook() {
try {
System.out.print("Enter Member ID: ");
int memberId = scanner.nextInt();
System.out.print("Enter Book ID to return: ");
int bookId = scanner.nextInt();
scanner.nextLine();
Member member = members.get(memberId);
Book book = books.get(bookId);
if (member == null) {
System.out.println("Error: Member not found with ID " + memberId);
return;
}
if (book == null) {
System.out.println("Error: Book not found with ID " + bookId);
return;
}
if (!book.isIssued()) {
System.out.println("Error: Book was not marked as issued.");
return;
}
if (member.returnIssuedBook(bookId)) {
book.markAsReturned();
saveToFile();
System.out.println("Book " + book.getTitle() + " returned successfully by " + member.getName() + ".");
} else {
System.out.println("Error: This book was not recorded as issued to this member.");
}
} catch (InputMismatchException e) {
System.out.println("Error: Invalid input. IDs must be integers.");
scanner.nextLine();
}
}
public void searchBooks() {
System.out.println("\nSearch Books By:");
System.out.println("1. Title");
System.out.println("2. Author");
System.out.println("3. Category");
System.out.print("Enter choice: ");
try {
int choice = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter search query: ");
String query = scanner.nextLine().toLowerCase();
List<Book> results = new ArrayList<>();
for (Book book : books.values()) {
boolean match = false;
switch (choice) {
case 1:
match = book.getTitle().toLowerCase().contains(query);
break;
case 2:
match = book.getAuthor().toLowerCase().contains(query);
break;
case 3:
match = book.getCategory().toLowerCase().contains(query);
break;
default:
System.out.println("Invalid search choice.");
return;
}
if (match) {
results.add(book);
}
}
if (results.isEmpty()) {
System.out.println("No books found matching the query.");
} else {
System.out.println("\n--- Search Results (" + results.size() + ") ---");
for (Book book : results) {
book.displayBookDetails();
}
}
} catch (InputMismatchException e) {
System.out.println("Error: Invalid input. Choice must be an integer.");
scanner.nextLine();
}
}
public void sortBooks() {
if (books.isEmpty()) {
System.out.println("No books to sort.");
return;
}
System.out.println("\nSort Books By:");
System.out.println("1. Title (Default/Comparable)");
System.out.println("2. Author (Comparator)");
System.out.println("3. Category (Comparator)");
System.out.print("Enter choice: ");
try {
int choice = scanner.nextInt();
scanner.nextLine();
List<Book> bookList = new ArrayList<>(books.values());
Comparator<Book> comparator = null;
switch (choice) {
case 1:
Collections.sort(bookList); // Uses Book's compareTo (by title)
System.out.println("Books sorted by Title.");
break;
case 2:
comparator = new BookAuthorComparator();
Collections.sort(bookList, comparator);
System.out.println("Books sorted by Author.");
break;
case 3:
comparator = new BookCategoryComparator();
Collections.sort(bookList, comparator);
System.out.println("Books sorted by Category.");
break;
default:
System.out.println("Invalid sort choice.");
return;
}
System.out.println("\n--- Sorted Books ---");
for (Book book : bookList) {
book.displayBookDetails();
}
} catch (InputMismatchException e) {
System.out.println("Error: Invalid input. Choice must be an integer.");
scanner.nextLine();
}
}
public void mainMenu() {
int choice = 0;
do {
System.out.println("\n===== City Library Digital Management System =====");
System.out.println("1. Add Book");
System.out.println("2. Add Member");
System.out.println("3. Issue Book");
System.out.println("4. Return Book");
System.out.println("5. Search Books");
System.out.println("6. Sort Books");
System.out.println("7. Exit & Save Data");
System.out.print("Enter your choice: ");
try {
choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1: addBook(); break;
case 2: addMember(); break;
case 3: issueBook(); break;
case 4: returnBook(); break;
case 5: searchBooks(); break;
case 6: sortBooks(); break;
case 7:
System.out.println("Saving data and Exiting program. Thank you!");
break;
default:
System.out.println("Invalid choice. Please enter a number from 1 to 7.");
}
} catch (InputMismatchException e) {
System.out.println("Error: Invalid input. Please enter a valid menu number.");
choice = 0;
scanner.nextLine();
}
} while (choice != 7);
}
public static void main(String[] args) {
Main manager = new Main();
try {
manager.mainMenu();
} finally {
if (manager.scanner != null) {
manager.saveToFile();
manager.scanner.close();
}
System.out.println("System resources released.");
}
}
}