-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryManager.java
More file actions
224 lines (206 loc) · 8.44 KB
/
LibraryManager.java
File metadata and controls
224 lines (206 loc) · 8.44 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
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;
/**
* Manages the library's books, students, and borrow records.
* Interacts with CSVHandler to read/write data to books.csv, students.csv, and borrows.csv.
*/
public class LibraryManager {
private List<Book> books; // List of books
private List<Student> students; // List of students
private List<Borrow> borrows; // List of borrow records
private CSVHandler csvHandler; // Handles CSV file operations
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd"); // Date format
/**
* Initializes the library manager by loading data from CSV files.
* Adds default books if books.csv is empty.
*/
public LibraryManager() {
this.csvHandler = new CSVHandler();
this.books = csvHandler.loadBooks(); // Load books from books.csv
this.students = csvHandler.loadStudents(); // Load students from students.csv
this.borrows = csvHandler.loadBorrows(); // Load borrows from borrows.csv
if (books.isEmpty()) {
initializeDefaultBooks(); // Add default books if none exist
}
}
/**
* Adds default books to the library if books.csv is empty.
* Saves to books.csv after adding.
*/
private void initializeDefaultBooks() {
// Add sample books
books.add(new Book("B1", "Java Programming", true));
books.add(new Book("B2", "Data Structures", true));
books.add(new Book("B3", "Algorithms", true));
books.add(new Book("B4", "Operating Systems", true));
books.add(new Book("B5", "Database Systems", true));
csvHandler.saveBooks(books); // Save to books.csv
}
/**
* Adds a new book to the library.
* @param book Book to add
* @throws IllegalArgumentException if book ID already exists
*/
public void addBook(Book book) {
if (books.stream().anyMatch(b -> b.getId().equals(book.getId()))) {
throw new IllegalArgumentException("Book ID already exists!");
}
books.add(book);
csvHandler.saveBooks(books); // Update books.csv
}
/**
* Removes a book from the library.
* @param bookId Book's ID
* @throws IllegalArgumentException if book not found or is borrowed
*/
public void removeBook(String bookId) {
Book book = findBook(bookId);
if (book == null) {
throw new IllegalArgumentException("Book not found!");
}
if (borrows.stream().anyMatch(b -> b.getBookId().equals(bookId) && b.getStatus().equals("Active"))) {
throw new IllegalArgumentException("Cannot remove a borrowed book!");
}
books.remove(book);
csvHandler.saveBooks(books); // Update books.csv
}
/**
* Adds a new student to the library.
* @param student Student to add
* @throws IllegalArgumentException if student already exists
*/
public void addStudent(Student student) {
if (students.stream().anyMatch(s -> s.getRollNo().equals(student.getRollNo()))) {
throw new IllegalArgumentException("Student already exists!");
}
students.add(student);
csvHandler.saveStudents(students); // Update students.csv
}
/**
* Authenticates an admin with hardcoded credentials.
* @param username Admin username
* @param password Admin password
* @return true if credentials are valid (admin/admin123)
*/
public boolean adminLogin(String username, String password) {
return username.equals("admin") && password.equals("admin123");
}
/**
* Authenticates a student using roll number and password.
* @param rollNo Student's roll number
* @param password Student's password
* @return true if credentials match a student in students.csv
*/
public boolean studentLogin(String rollNo, String password) {
return students.stream()
.anyMatch(s -> s.getRollNo().equals(rollNo) &&
s.getPassword().equals(password));
}
/**
* Gets a copy of the books list to prevent external modification.
* @return List of books
*/
public List<Book> getBooks() {
return new ArrayList<>(books);
}
/**
* Gets a copy of the students list to prevent external modification.
* @return List of students
*/
public List<Student> getStudents() {
return new ArrayList<>(students);
}
/**
* Gets a copy of the borrow records list to prevent external modification.
* @return List of borrows
*/
public List<Borrow> getBorrows() {
return new ArrayList<>(borrows);
}
/**
* Borrows a book for a student, updating books.csv and borrows.csv.
* @param rollNo Student's roll number
* @param bookId Book's ID
* @param dueDate Due date in YYYY-MM-DD format
* @throws IllegalArgumentException if book not found, already borrowed, or invalid due date
*/
public void borrowBook(String rollNo, String bookId, String dueDate) {
Book book = findBook(bookId);
if (book == null) {
throw new IllegalArgumentException("Book not found!");
}
// Validate due date format and range
try {
LocalDate borrowDate = LocalDate.now();
LocalDate due = LocalDate.parse(dueDate, DATE_FORMATTER);
if (!due.isAfter(borrowDate)) {
throw new IllegalArgumentException("Due date must be after today!");
}
if (due.getYear() < borrowDate.getYear() ||
due.getMonthValue() < 1 || due.getMonthValue() > 12 ||
due.getDayOfMonth() < 1 || due.getDayOfMonth() > due.lengthOfMonth()) {
throw new IllegalArgumentException("Invalid due date: month (1-12) or day (1-" + due.lengthOfMonth() + ")!");
}
} catch (DateTimeParseException e) {
throw new IllegalArgumentException("Invalid due date format! Use YYYY-MM-DD");
}
book.borrow(rollNo, dueDate); // Mark book as borrowed
borrows.add(new Borrow(rollNo, bookId, LocalDate.now().toString(), dueDate, "", "Active"));
csvHandler.saveBooks(books); // Update books.csv
csvHandler.saveBorrows(borrows); // Update borrows.csv
}
/**
* Returns a borrowed book, updating books.csv and borrows.csv.
* @param rollNo Student's roll number
* @param bookId Book's ID
* @throws IllegalArgumentException if no active borrow record exists
*/
public void returnBook(String rollNo, String bookId) {
Borrow borrow = findBorrow(rollNo, bookId);
if (borrow == null || borrow.getStatus().equals("Returned")) {
throw new IllegalArgumentException("No active borrow record found!");
}
Book book = findBook(bookId);
if (book != null) {
book.returnItem(); // Mark book as available
borrow.markAsReturned(LocalDate.now().toString()); // Update borrow record
csvHandler.saveBooks(books); // Update books.csv
csvHandler.saveBorrows(borrows); // Update borrows.csv
}
}
/**
* Finds a book by ID.
* @param id Book's ID
* @return Book or null if not found
*/
private Book findBook(String id) {
return books.stream()
.filter(b -> b.getId().equals(id))
.findFirst()
.orElse(null);
}
/**
* Finds an active borrow record by roll number and book ID.
* @param rollNo Student's roll number
* @param bookId Book's ID
* @return Borrow or null if not found
*/
private Borrow findBorrow(String rollNo, String bookId) {
return borrows.stream()
.filter(b -> b.getRollNo().equals(rollNo) &&
b.getBookId().equals(bookId) &&
b.getStatus().equals("Active"))
.findFirst()
.orElse(null);
}
/**
* Gets the borrowing history for admin view.
* @return List of all borrow records
*/
public List<Borrow> getBorrowHistory() {
return new ArrayList<>(borrows);
}
}