Repository navigation
Expand file tree
/
Copy pathLibrarySystem.java
More file actions
287 lines (256 loc) · 10.2 KB
/
Copy pathLibrarySystem.java
File metadata and controls
287 lines (256 loc) · 10.2 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
//LibrarySystem
import java.io.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class LibrarySystem {
private List<User> users;
private List<Book> books;
private List<Transaction> transactions;
public LibrarySystem() {
users = new ArrayList<>();
books = new ArrayList<>();
transactions = new ArrayList<>();
// Add default admin user if no users exist
users.add(new Admin("admin", "admin123", "System Administrator"));
}
// Authentication
public User authenticateUser(String username, String password) {
for (User user : users) {
if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
return user;
}
}
return null;
}
// User management
public void addUser(User user) {
users.add(user);
}
public boolean removeUser(String username) {
return users.removeIf(user -> user.getUsername().equals(username));
}
public List<User> getAllUsers() {
return new ArrayList<>(users);
}
public User getUserByUsername(String username) {
for (User user : users) {
if (user.getUsername().equals(username)) {
return user;
}
}
return null;
}
// Book management
public void addBook(Book book) {
books.add(book);
}
public boolean removeBook(String isbn) {
return books.removeIf(book -> book.getIsbn().equals(isbn));
}
public List<Book> getAllBooks() {
return new ArrayList<>(books);
}
public Book getBookByIsbn(String isbn) {
for (Book book : books) {
if (book.getIsbn().equals(isbn)) {
return book;
}
}
return null;
}
public List<Book> getAvailableBooks() {
List<Book> availableBooks = new ArrayList<>();
for (Book book : books) {
if (book.isAvailable()) {
availableBooks.add(book);
}
}
return availableBooks;
}
// Transaction management
public void addTransaction(Transaction transaction) {
transactions.add(transaction);
}
public List<Transaction> getAllTransactions() {
return new ArrayList<>(transactions);
}
public List<Transaction> getTransactionsByUser(String username) {
List<Transaction> userTransactions = new ArrayList<>();
for (Transaction transaction : transactions) {
if (transaction.getUsername().equals(username)) {
userTransactions.add(transaction);
}
}
return userTransactions;
}
public List<Book> getBorrowedBooksByUser(String username) {
List<Book> borrowedBooks = new ArrayList<>();
for (Transaction transaction : transactions) {
if (transaction.getUsername().equals(username) &&
transaction.getType().equals("Borrow") &&
transaction.getReturnDate() == null) {
Book book = getBookByIsbn(transaction.getIsbn());
if (book != null) {
borrowedBooks.add(book);
}
}
}
return borrowedBooks;
}
// File handling for persistence
public void loadData() {
loadUsers();
loadBooks();
loadTransactions();
}
public void saveData() {
saveUsers();
saveBooks();
saveTransactions();
}
private void loadUsers() {
try (BufferedReader reader = new BufferedReader(new FileReader("users.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 4) {
String username = parts[0];
String password = parts[1];
String name = parts[2];
String role = parts[3];
User user;
switch (role) {
case "Admin":
user = new Admin(username, password, name);
break;
case "Librarian":
user = new Librarian(username, password, name);
break;
case "Reader":
user = new Reader(username, password, name);
break;
default:
continue;
}
users.add(user);
}
}
} catch (IOException e) {
System.out.println("Users file not found. Using default admin user.");
}
}
private void saveUsers() {
try (PrintWriter writer = new PrintWriter(new FileWriter("users.txt"))) {
for (User user : users) {
writer.println(user.getUsername() + "," +
user.getPassword() + "," +
user.getName() + "," +
user.getRole());
}
} catch (IOException e) {
System.out.println("Error saving users: " + e.getMessage());
}
}
private void loadBooks() {
try (BufferedReader reader = new BufferedReader(new FileReader("books.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length >= 6) {
String title = parts[0];
String author = parts[1];
String genre = parts[2];
String isbn = parts[3];
boolean available = Boolean.parseBoolean(parts[4]);
String type = parts[5];
Book book;
if (type.equals("PrintedBook") && parts.length == 7) {
int pages = Integer.parseInt(parts[6]);
book = new PrintedBook(title, author, genre, isbn, pages);
} else if (type.equals("Ebook") && parts.length == 7) {
String format = parts[6];
book = new Ebook(title, author, genre, isbn, format);
} else {
continue;
}
book.setAvailable(available);
books.add(book);
}
}
} catch (IOException e) {
System.out.println("Books file not found. Starting with empty book collection.");
}
}
private void saveBooks() {
try (PrintWriter writer = new PrintWriter(new FileWriter("books.txt"))) {
for (Book book : books) {
if (book instanceof PrintedBook) {
PrintedBook printedBook = (PrintedBook) book;
writer.println(book.getTitle() + "," +
book.getAuthor() + "," +
book.getGenre() + "," +
book.getIsbn() + "," +
book.isAvailable() + "," +
"PrintedBook" + "," +
printedBook.getPages());
} else if (book instanceof Ebook) {
Ebook ebook = (Ebook) book;
writer.println(book.getTitle() + "," +
book.getAuthor() + "," +
book.getGenre() + "," +
book.getIsbn() + "," +
book.isAvailable() + "," +
"Ebook" + "," +
ebook.getFormat());
}
}
} catch (IOException e) {
System.out.println("Error saving books: " + e.getMessage());
}
}
private void loadTransactions() {
try (BufferedReader reader = new BufferedReader(new FileReader("transactions.txt"))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length >= 5) {
String id = parts[0];
String username = parts[1];
String isbn = parts[2];
String type = parts[3];
LocalDate borrowDate = LocalDate.parse(parts[4]);
LocalDate returnDate = null;
if (parts.length == 6 && !parts[5].isEmpty()) {
returnDate = LocalDate.parse(parts[5]);
}
Transaction transaction = new Transaction(id, username, isbn, type, borrowDate);
if (returnDate != null) {
transaction.setReturnDate(returnDate);
}
transactions.add(transaction);
}
}
} catch (IOException e) {
System.out.println("Transactions file not found. Starting with empty transactions.");
}
}
private void saveTransactions() {
try (PrintWriter writer = new PrintWriter(new FileWriter("transactions.txt"))) {
for (Transaction transaction : transactions) {
writer.print(transaction.getId() + "," +
transaction.getUsername() + "," +
transaction.getIsbn() + "," +
transaction.getType() + "," +
transaction.getBorrowDate());
if (transaction.getReturnDate() != null) {
writer.println("," + transaction.getReturnDate());
} else {
writer.println(",");
}
}
} catch (IOException e) {
System.out.println("Error saving transactions: " + e.getMessage());
}
}
}