-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.java
More file actions
355 lines (321 loc) · 13.4 KB
/
Copy pathcode.java
File metadata and controls
355 lines (321 loc) · 13.4 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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
class User {
private String username;
private String password;
private double fine;
public User(String username, String password) {
this.username = username;
this.password = password;
this.fine = 0.0;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
public double getFine() {
return fine;
}
public void setFine(double fine) {
this.fine = fine;
}
}
class Book {
private String title;
private String author;
private String category;
private boolean available;
private LocalDate borrowedDate;
private LocalDate dueDate;
public Book(String title, String author, String category) {
this.title = title;
this.author = author;
this.category = category;
this.available = true;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getCategory() {
return category;
}
public boolean isAvailable() {
return available;
}
public void setAvailable(boolean available) {
this.available = available;
}
public void setBorrowedDate(LocalDate borrowedDate) {
this.borrowedDate = borrowedDate;
}
public void setDueDate(LocalDate dueDate) {
this.dueDate = dueDate;
}
public LocalDate getBorrowedDate() {
return borrowedDate;
}
public LocalDate getDueDate() {
return dueDate;
}
}
class Library {
private ArrayList<Book> books;
public Library() {
this.books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
}
public void removeBook(int index) {
if (index >= 0 && index < books.size()) {
books.remove(index);
System.out.println("Book removed successfully.");
} else {
System.out.println("Invalid book index.");
}
}
public void displayBooks() {
System.out.println("Available Books:");
for (int i = 0; i < books.size(); i++) {
Book book = books.get(i);
if (book.isAvailable()) {
System.out.println((i + 1) + ". " + book.getTitle() + " by " + book.getAuthor());
}
}
}
public ArrayList<Book> searchBooks(String keyword) {
ArrayList<Book> results = new ArrayList<>();
for (Book book : books) {
if (book.getTitle().toLowerCase().contains(keyword.toLowerCase()) ||
book.getAuthor().toLowerCase().contains(keyword.toLowerCase())) {
results.add(book);
}
}
return results;
}
public ArrayList<Book> searchBooksByCategory(String category) {
ArrayList<Book> results = new ArrayList<>();
for (Book book : books) {
if (book.getCategory().equalsIgnoreCase(category)) {
results.add(book);
}
}
return results;
}
public void returnBook(int index) {
if (index >= 0 && index < books.size()) {
Book book = books.get(index);
if (!book.isAvailable()) {
book.setAvailable(true);
System.out.println("Book returned successfully.");
} else {
System.out.println("Book is already available.");
}
} else {
System.out.println("Invalid book index.");
}
}
public void borrowBook(int index) {
if (index >= 0 && index < books.size()) {
Book book = books.get(index);
if (book.isAvailable()) {
book.setAvailable(false);
LocalDate borrowedDate = LocalDate.now();
book.setBorrowedDate(borrowedDate);
LocalDate dueDate = borrowedDate.plusDays(15); // Due date set to 15 days after borrowing
book.setDueDate(dueDate);
System.out.println("Book borrowed successfully. Return by: " + book.getDueDate());
} else {
System.out.println("Book is not available for borrowing.");
}
} else {
System.out.println("Invalid book index.");
}
}
public void calculateFine(int index) {
if (index >= 1 && index <= books.size()) {
Book book = books.get(index - 1);
if (!book.isAvailable()) {
LocalDate currentDate = LocalDate.now();
long daysLate = ChronoUnit.DAYS.between(book.getDueDate(), currentDate);
if (daysLate > 0) {
double fine = daysLate * 0.50; // Assuming a fine of 0.50 per day
System.out.println("Fine for returning the book late: $" + fine);
} else {
System.out.println("No fine. The book was returned on time.");
}
} else {
System.out.println("Book is already available.");
}
} else {
System.out.println("Invalid book index.");
}
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Library library = new Library();
Map<String, User> users = new HashMap<>();
boolean running = true;
String currentUser = "";
library.addBook(new Book("1984", "George Orwell", "Dystopian Fiction"));
library.addBook(new Book("To Kill a Mockingbird", "Harper Lee", "Fiction"));
library.addBook(new Book("The Great Gatsby", "F. Scott Fitzgerald", "Fiction"));
library.addBook(new Book("Pride and Prejudice", "Jane Austen", "Classic Fiction"));
library.addBook(new Book("The Catcher in the Rye", "J.D. Salinger", "Fiction"));
while (running) {
if (currentUser.isEmpty()) {
System.out.println("Welcome to the Library Management System.");
System.out.println("1. Log in");
System.out.println("2. Sign up");
System.out.println("3. Exit");
System.out.print("Enter your choice: ");
int authChoice = scanner.nextInt();
scanner.nextLine();
switch (authChoice) {
case 1:
System.out.print("Enter your username: ");
String existingUser = scanner.nextLine();
if (users.containsKey(existingUser)) {
System.out.print("Enter your password: ");
String existingPassword = scanner.nextLine();
if (users.get(existingUser).equals(existingPassword)) {
currentUser = existingUser;
System.out.println("Authentication successful. Welcome, " + currentUser + "!");
} else {
System.out.println("Incorrect password. Please try again.");
}
} else {
System.out.println("User not found. Please sign up.");
}
break;
case 2:
System.out.print("Create a username: ");
String newUser = scanner.nextLine();
if (users.containsKey(newUser)) {
System.out.println("Username already exists. Please try a different username.");
} else {
System.out.print("Create a password: ");
String newPassword = scanner.nextLine();
users.put(newUser, new User(newUser, newPassword));
currentUser = newUser;
System.out.println("Sign up successful. Welcome, " + currentUser + "!");
}
break;
case 3:
running = false;
break;
default:
System.out.println("Invalid choice. Please try again.");
break;
}
} else {
System.out.println("\nWelcome, " + currentUser + "! What would you like to do?");
System.out.println("1. Display available books");
System.out.println("2. Search for books");
System.out.println("3. Borrow a book");
System.out.println("4. Return a book");
System.out.println("5. Add a book");
System.out.println("6. Remove a book");
System.out.println("7. Calculate Fine");
System.out.println("8. Search Books by Category");
System.out.println("9. Log out");
System.out.println("10. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
System.out.print("\n");
switch (choice) {
case 1:
library.displayBooks();
break;
case 2:
System.out.print("Enter a keyword to search for: ");
String keyword = scanner.nextLine();
ArrayList<Book> searchResults = library.searchBooks(keyword);
if (searchResults.isEmpty()) {
System.out.println("No books found.");
} else {
System.out.println("Search Results:");
for (int i = 0; i < searchResults.size(); i++) {
Book book = searchResults.get(i);
System.out.println((i + 1) + ". " + book.getTitle() + " by " + book.getAuthor());
}
}
break;
case 3:
library.displayBooks();
System.out.print("Enter the index of the book to borrow: ");
int borrowIndex = scanner.nextInt();
scanner.nextLine();
library.borrowBook(borrowIndex - 1);
break;
case 4:
System.out.print("Enter the index of the book to return: ");
int returnIndex = scanner.nextInt();
scanner.nextLine();
library.returnBook(returnIndex - 1);
break;
case 5:
System.out.print("Enter the title of the book: ");
String title = scanner.nextLine();
System.out.print("Enter the author of the book: ");
String author = scanner.nextLine();
System.out.print("Enter the category of the book: ");
String category = scanner.nextLine();
library.addBook(new Book(title, author, category));
System.out.println("Book added successfully.");
break;
case 6:
library.displayBooks();
System.out.print("Enter the index of the book to remove: ");
int removeIndex = scanner.nextInt();
scanner.nextLine();
library.removeBook(removeIndex - 1);
break;
case 7:
System.out.print("Enter the index of the book: ");
int fine = scanner.nextInt();
scanner.nextLine();
library.calculateFine(fine);
break;
case 8:
System.out.print("Enter the category: ");
String categoryChoice = scanner.nextLine();
ArrayList<Book> booksByCategory = library.searchBooksByCategory(categoryChoice);
if (booksByCategory.isEmpty()) {
System.out.println("No books found in the category.");
} else {
System.out.println("Books in the " + categoryChoice + " category:");
for (int i = 0; i < booksByCategory.size(); i++) {
Book book = booksByCategory.get(i);
System.out.println((i + 1) + ". " + book.getTitle() + " by " + book.getAuthor());
}
}
break;
case 9:
currentUser = "";
System.out.println("Logged out successfully.");
break;
case 10:
running = false;
break;
default:
System.out.println("Invalid choice. Please try again.");
break;
}
}
}
scanner.close();
}
}