-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBorrowing.java
More file actions
473 lines (424 loc) · 17.9 KB
/
Copy pathBorrowing.java
File metadata and controls
473 lines (424 loc) · 17.9 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package javalibrary;
import java.io.*;
import java.util.*;
public class Borrowing implements Serializable
{
private static final long serialVersionUID = 1L;
private int borrowingId;
private int librarianId;
private int borrowerId;
private int bookId;
private String borrowingDate;
private String returnDate;
private boolean isReturned;
private int rate;
// Static map to store borrowing data
static HashMap<Integer, Borrowing> borrowinggMap = new HashMap<>();
private static Scanner input = new Scanner(System.in);
// Constructor for Borrowing class
public Borrowing(int librarianId, int borrowingId, int borrowerId, int bookId, String borrowingDate, String returnDate, boolean isReturned, int rate) {
this.librarianId = librarianId;
this.borrowingId = borrowingId;
this.borrowerId = borrowerId;
this.bookId = bookId;
this.borrowingDate = borrowingDate;
this.returnDate = returnDate;
this.isReturned = isReturned;
this.rate = rate;
}
public int getLibrarianId() {
return librarianId;
}
public void setLibrarianId(int librarianId) {
this.librarianId = librarianId;
}
public int getRate() {
return rate;
}
public void setRate(int rate) {
this.rate = rate;
}
// Getters and Setters
public int getBorrowingId() {
return borrowingId;
}
public void setBorrowingId(int borrowingId) {
this.borrowingId = borrowingId;
}
public int getBorrowerId() {
return borrowerId;
}
public void setBorrowerId(int borrowerId) {
this.borrowerId = borrowerId;
}
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBorrowingDate() {
return borrowingDate;
}
public void setBorrowingDate(String borrowingDate) {
this.borrowingDate = borrowingDate;
}
public String getReturnDate() {
return returnDate;
}
public void setReturnDate(String returnDate) {
this.returnDate = returnDate;
}
public boolean isReturned() {
return isReturned;
}
public void setReturned(boolean isReturned) {
this.isReturned = isReturned;
}
// Method to save borrowing data to a file
public static void savefile() {
try (ObjectOutputStream writing = new ObjectOutputStream(new FileOutputStream("borrowingdata.bin"))) {
writing.writeObject(borrowinggMap);
System.out.println("Borrowing data saved to file successfully.");
} catch (IOException e) {
System.out.println("Error while saving Borrowing: " + e.getMessage());
}
}
// Method to load borrowing data from a file
public static void readfile() {
try (ObjectInputStream loading = new ObjectInputStream(new FileInputStream("borrowingdata.bin"))) {
borrowinggMap = (HashMap<Integer, Borrowing>) loading.readObject();
System.out.println("Borrowing loaded from file successfully.");
} catch (IOException | ClassNotFoundException e) {
System.err.println("Error while loading Borrowing: " + e.getMessage());
}
}
// Method to create a new borrowing record
public static boolean createBorrowing(int librarianId, int borrowingId, int borrowerId, int bookId, String borrowingDate, String returnDate, boolean isReturned) {
Book.readFile(); // Load books data
Librarian.readfile();
Borrower.readfile(); // Load borrowers data
Borrowing.readfile(); // Load borrowing data
// Check if librarian exists
if (!Librarian.librarianmap.containsKey(librarianId)) {
System.out.println("Librarian ID " + librarianId + " not Found");
return false;
}
// Check if the borrowing ID already exists
if (borrowinggMap.containsKey(borrowingId)) {
System.out.println("Borrowing ID " + borrowingId + " already exists.");
return false;
}
// Check if the borrower exists
if (!Borrower.boorrowermap.containsKey(borrowerId)) {
System.out.println("Borrower ID " + borrowerId + " not found.");
return false;
}
// Check if the book exists
if (!Book.map.containsKey(bookId)) {
System.out.println("Book ID " + bookId + " not found.");
return false;
}
Book selectedBook = Book.map.get(bookId);
// Check if the book has available copies
if (selectedBook.getCopiesAvailability() <= 0) {
System.out.println("No copies available for Book ID: " + bookId);
return false;
}
// Borrowing Date and Return Date
double bookPrice = selectedBook.getPrice();
Borrower borrower = Borrower.boorrowermap.get(borrowerId);
borrower.setBorroweringcounter(borrower.getBorroweringcounter() + 1);
borrower.setTotalrevenue(borrower.getTotalrevenue() + bookPrice);
Borrower.savefile();
// Create new borrowing object and save
int rate = 0; // Default rate is 0
Borrowing newBorrowing = new Borrowing(librarianId, borrowingId, borrowerId, bookId, borrowingDate, returnDate, isReturned, rate);
borrowinggMap.put(borrowingId, newBorrowing);
savefile();
// Update book details
selectedBook.setCopiesBorrowed(selectedBook.getCopiesBorrowed() + 1);
selectedBook.setCopiesAvailability(selectedBook.getCopiesAvailability() - 1);
selectedBook.setBorrowCount(selectedBook.getBorrowCount() + 1);
Book.updateRevenue(bookId);
Book.saveFile();
// Update librarian details
Librarian librarian = Librarian.librarianmap.get(librarianId);
librarian.setBorrowcount(librarian.getBorrowcount() + 1);
librarian.setRevenue(librarian.getRevenue() + bookPrice);
Librarian.savefile();
System.out.println("Item borrowed successfully.");
return true; // Success
}
// Method to cancel a borrowing record
// Method to cancel a borrowing record (corrected)
public static boolean cancelBorrowing(int borrowingId) {
// Check if borrowing ID exists
if (!borrowinggMap.containsKey(borrowingId)) {
System.out.println("Borrowing ID " + borrowingId + " not found.");
return false; // Return false if borrowing ID is not found
}
Borrowing borrowing = borrowinggMap.get(borrowingId);
// If the book has already been returned, it can't be cancelled
if (borrowing.isReturned) {
System.out.println("The book has already been returned and cannot be cancelled.");
return false; // Return false if the book is already returned
}
// Get the book associated with this borrowing
Book book = Book.map.get(borrowing.getBookId());
// Update the book's availability
book.setCopiesBorrowed(book.getCopiesBorrowed() - 1);
book.setCopiesAvailability(book.getCopiesAvailability() + 1);
Book.updateRevenue(book.getBookId());
Book.saveFile();
// Update borrower information
Borrower borrower = Borrower.boorrowermap.get(borrowing.getBorrowerId());
borrower.setBorroweringcounter(borrower.getBorroweringcounter() - 1);
borrower.setTotalrevenue(borrower.getTotalrevenue() - book.getPrice());
Borrower.savefile();
// Update librarian information
Librarian librarian = Librarian.librarianmap.get(borrowing.getLibrarianId());
librarian.setBorrowcount(librarian.getBorrowcount() - 1);
librarian.setRevenue(librarian.getRevenue() - book.getPrice());
Librarian.savefile();
// Remove the borrowing record from the map
borrowinggMap.remove(borrowingId);
savefile(); // Save the updated borrowing records
System.out.println("Borrowing cancelled successfully.");
return true; // Return true if the borrowing was successfully cancelled
}
// Method to display all borrowing records
public static void displayBorrowing() {
// Ensure borrowings are loaded from the file
readfile();
// Check if the borrowing map is empty
if (borrowinggMap.isEmpty()) {
System.out.println("No borrowings available.");
return;
}
// Display each borrowing record
System.out.println("Borrowing List:");
for (Map.Entry<Integer, Borrowing> entry : borrowinggMap.entrySet()) {
Borrowing borrow = entry.getValue();
// Check if the necessary fields are available and valid
if (borrow != null) {
System.out.println(
"Librarian ID: "+ borrow.getLibrarianId()+
"Borrowing ID: " + borrow.getBorrowingId() +
", Borrower ID: " + borrow.getBorrowerId() +
", Book ID: " + borrow.getBookId() +
", Borrowing Date: " + borrow.getBorrowingDate() +
", Return Date: " + borrow.getReturnDate() +
", Returned: " + borrow.isReturned() +
", Rate: " + borrow.getRate()); // Use getRate() instead of hashCode()
} else {
System.out.println("Error: Borrowing data is null.");
}
}
}
public static String borrowingHistory(int borrowerId) {
readfile(); // Ensure this loads the borrowing data correctly
boolean found = false;
StringBuilder result = new StringBuilder(); // To store the result string
result.append("Borrowing History for Borrower ID: ").append(borrowerId).append("\n");
for (Borrowing borrow : borrowinggMap.values()) {
if (borrow.getBorrowerId() == borrowerId) {
found = true;
result.append("Borrowing ID: ").append(borrow.getBorrowingId()).append("\n");
result.append("Book ID: ").append(borrow.getBookId()).append("\n");
result.append("Borrowing Date: ").append(borrow.getBorrowingDate()).append("\n");
result.append("Return Date: ").append(borrow.getReturnDate()).append("\n");
result.append("Returned: ").append(borrow.isReturned() ? "Yes" : "No").append("\n");
result.append("Rate: ").append(borrow.getRate()).append("\n");
}
}
if (!found) {
result.append("No borrowing history found for Borrower ID: ").append(borrowerId).append("\n");
}
return result.toString(); // Return the result as a string
}
public static boolean rateBook(int borrowingId) {
Borrowing.readfile();
Book.readFile();
// Find the borrowing
Borrowing borrow = Borrowing.borrowinggMap.get(borrowingId);
// Ensure borrowing exists
if (borrow == null) {
System.out.println("Invalid borrowing ID.");
return false;
}
// Prompt for rating
System.out.println("Please enter a rating between 1 and 5:");
int rate;
while (true) {
try {
rate = input.nextInt(); // Get the rating input
if (rate >= 1 && rate <= 5) {
break;
} else {
System.out.println("Invalid rating. Enter a number between 1 and 5.");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input. Please enter a number between 1 and 5.");
input.next(); // Clear invalid input
}
}
// Set the rating for this borrowing
borrow.setRate(rate);
// Save the updated data
Borrowing.savefile();
System.out.println("Your rating of " + rate + " has been recorded.");
return true;
}
public static void LibrarianBorrowings(int librarianId) {
readfile();
Librarian.readfile();
boolean found = false;
Librarian librarian = Librarian.librarianmap.get(librarianId);
System.out.println("Librarian Borrowing History for Librarian ID: " + librarianId);
System.out.println("Total Borrow Count: " + librarian.getBorrowcount());
for (Borrowing borrow : borrowinggMap.values())
{
if (borrow.getLibrarianId() == librarianId)
{
found = true;
System.out.println("Borrowing ID: " + borrow.getBorrowingId());
System.out.println("Book ID: " + borrow.getBookId());
System.out.println("Borrowing Date: " + borrow.getBorrowingDate());
System.out.println("Return Date: " + borrow.getReturnDate());
System.out.println("Returned: " + (borrow.isReturned() ? "Yes" : "No"));
System.out.println("Rate: " + borrow.getRate());
}
}
if (!found) {
System.out.println("No borrowing history found for Librarian ID: " + librarianId);
}
}
public static void searchBorrowingsdate(String date)
{
readfile();
boolean found = false;
for (Borrowing borrow : borrowinggMap.values()) {
if (String.valueOf(borrow.getBorrowingDate()).equals(date))
{
System.out.println("Borrowing ID: " + borrow.getBorrowingId());
System.out.println("Borrower ID: " + borrow.getBorrowerId());
System.out.println("Book ID: " + borrow.getBookId());
System.out.println("Borrowing Date: " + borrow.getBorrowingDate());
System.out.println("Return Date: " + borrow.getReturnDate());
System.out.println("Returned: " + (borrow.isReturned() ? "Yes" : "No"));
found = true;
}
}
if (!found) {
System.out.println("No borrowings found for the date");
}
}
public static void mostBorrowedBookOnDate(String specificDate) {
readfile();
int mostBorrowedBookId = -1;
int maxCount = 0;
for (Borrowing borrow : borrowinggMap.values()) {
String borrowingDate = borrow.getBorrowingDate();
if (borrowingDate.equals(specificDate))
{
int bookId = borrow.getBookId();
int borrowCount = 0;
for (Borrowing b : borrowinggMap.values())
{
if (b.getBookId() == bookId && b.getBorrowingDate().equals(specificDate))
{
borrowCount++;
}
}
if (borrowCount > maxCount) {
mostBorrowedBookId = bookId;
maxCount = borrowCount;
}
}
}
if (mostBorrowedBookId != -1) {
System.out.println("Most borrowed book ID: " + mostBorrowedBookId);
if (Book.map.containsKey(mostBorrowedBookId))
{
Book book = Book.map.get(mostBorrowedBookId);
System.out.println("Book Title: " + book.getTitle());
System.out.println("Book Author: " + book.getAuthor());
System.out.println("Times Borrowed: " + maxCount);
}
} else {
System.out.println("No books borrowed on the specified date.");
}
}
public static void mostRevenueBookOnDate(String specificDate) {
readfile();
Book.readFile();
int maxRevenueBookId = -1;
double maxRevenue = 0.0;
for (Borrowing borrow : borrowinggMap.values()) {
if (borrow.getBorrowingDate().equals(specificDate)) {
int bookId = borrow.getBookId();
Book book = Book.map.get(bookId);
if (book != null) {
double revenue = book.getPrice();
int borrowCount = 0;
for (Borrowing b : borrowinggMap.values()) {
if (b.getBookId() == bookId && b.getBorrowingDate().equals(specificDate)) {
borrowCount++;
}
}
revenue *= borrowCount;
if (revenue > maxRevenue)
{
maxRevenueBookId = bookId;
maxRevenue = revenue;
}
}
}
}
if (maxRevenueBookId != -1) {
System.out.println("Book that generated the most revenue on " + specificDate + ": " + maxRevenueBookId);
Book book = Book.map.get(maxRevenueBookId);
if (book != null)
{
System.out.println("Book Title: " + book.getTitle());
System.out.println("Book Author: " + book.getAuthor());
System.out.println("Total Revenue: " + maxRevenue);
}
} else {
System.out.println("No books borrowed on the specified date.");
}
}
public static String TotalAndAverageRevenueDate(String specificDate) {
readfile();
Book.readFile();
double totalRevenue = 0.0;
int totalBooksBorrowed = 0;
StringBuilder result = new StringBuilder(); // To hold the result
for (Borrowing borrow : borrowinggMap.values()) {
if (borrow.getBorrowingDate().equals(specificDate)) {
int bookId = borrow.getBookId();
Book book = Book.map.get(bookId);
if (book != null) {
double revenue = book.getPrice();
int borrowCount = 0;
for (Borrowing b : borrowinggMap.values()) {
if (b.getBookId() == bookId && b.getBorrowingDate().equals(specificDate)) {
borrowCount++;
}
}
totalRevenue += revenue * borrowCount;
totalBooksBorrowed++;
}
}
}
if (totalBooksBorrowed > 0) {
double averageRevenue = totalRevenue / totalBooksBorrowed;
result.append("Total Revenue on ").append(specificDate).append(": ").append(totalRevenue).append("\n");
result.append("Average Revenue on ").append(specificDate).append(": ").append(averageRevenue).append("\n");
} else {
result.append("No books borrowed on the specified date.");
}
return result.toString(); // Return the result as a string
}
}