Repository navigation
Expand file tree
/
Copy pathReader.java
More file actions
109 lines (87 loc) · 3.42 KB
/
Copy pathReader.java
File metadata and controls
109 lines (87 loc) · 3.42 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
import java.time.LocalDate;
import java.util.List;
public class Reader extends User {
public Reader(String username, String password, String name) {
super(username, password, name);
}
@Override
public String getRole() {
return "Reader";
}
public void borrowBook(LibrarySystem library, String isbn) {
Book book = library.getBookByIsbn(isbn);
if (book == null) {
System.out.println("Book with ISBN " + isbn + " not found.");
return;
}
if (!book.isAvailable()) {
System.out.println("Book is not available for borrowing.");
return;
}
if (book instanceof Ebook) {
System.out.println("Ebooks cannot be borrowed, only viewed online.");
return;
}
// Set the book as unavailable
book.setAvailable(false);
// Create a transaction record
Transaction transaction = new Transaction(getUsername(), isbn, "Borrow");
library.addTransaction(transaction);
// Calculate due date (14 days from now)
LocalDate dueDate = LocalDate.now().plusDays(14);
System.out.println("Book borrowed successfully.");
System.out.println("Due date: " + dueDate);
}
public void returnBook(LibrarySystem library, String isbn) {
Book book = library.getBookByIsbn(isbn);
if (book == null) {
System.out.println("Book with ISBN " + isbn + " not found.");
return;
}
// Check if the user has borrowed this book
boolean hasBorrowed = false;
List<Transaction> transactions = library.getAllTransactions();
for (Transaction transaction : transactions) {
if (transaction.getUsername().equals(getUsername()) &&
transaction.getIsbn().equals(isbn) &&
transaction.getType().equals("Borrow") &&
transaction.getReturnDate() == null) {
hasBorrowed = true;
transaction.setReturnDate(LocalDate.now());
break;
}
}
if (!hasBorrowed) {
System.out.println("You have not borrowed this book.");
return;
}
// Set the book as available
book.setAvailable(true);
// Create a return transaction
Transaction transaction = new Transaction(getUsername(), isbn, "Return");
library.addTransaction(transaction);
System.out.println("Book returned successfully.");
}
public void listAvailableBooks(LibrarySystem library) {
List<Book> books = library.getAvailableBooks();
if (books.isEmpty()) {
System.out.println("No available books in the library.");
return;
}
System.out.println("Available Books:");
for (Book book : books) {
System.out.println(book);
}
}
public void listBorrowedBooks(LibrarySystem library) {
List<Book> books = library.getBorrowedBooksByUser(getUsername());
if (books.isEmpty()) {
System.out.println("You have not borrowed any books.");
return;
}
System.out.println("Your Borrowed Books:");
for (Book book : books) {
System.out.println(book);
}
}
}