Split_20250208_0304
Split_20250208_0304
1. Searchable (Interface)
• private String id
Methods:
Inherits: User
Purpose: Handles student-specific operations
Additional Variables:
Inherits: User
Purpose: Handles faculty-specific operations
Additional Variables:
Methods:
5. Book (Class)
Implements: Searchable
Purpose: Represents book entities
Variables:
Methods:
• boolean isAvailable()
6. Loan (Class)
Methods:
7. Library (Class)
Methods:
8. Staff (Class)
Methods:
Key Methods:
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
interface Searchable {
boolean matches(String query);
}
@Override
public boolean matches(String query) {
return name.contains(query) || id.contains(query);
}
// Getters
public String getName() { return name; }
public String getId() { return id; }
public String getContactInfo() { return contactInfo; }
}
@Override
public void borrowBook(Book book) {
if (borrowedBooks.size() >= maxBooksAllowed) {
throw new IllegalStateException("Borrowing limit reached");
}
borrowedBooks.add(book);
book.setAvailable(false);
}
@Override
public void returnBook(Book book) {
if (!borrowedBooks.remove(book)) {
throw new IllegalArgumentException("Book not borrowed by this user");
}
book.setAvailable(true);
}
@Override
public List<Book> getBorrowedBooks() {
return new ArrayList<>(borrowedBooks);
}
}
@Override
public void borrowBook(Book book) {
if (borrowedBooks.size() >= maxBooksAllowed) {
throw new IllegalStateException("Borrowing limit reached");
}
borrowedBooks.add(book);
book.setAvailable(false);
}
@Override
public void returnBook(Book book) {
if (!borrowedBooks.remove(book)) {
throw new IllegalArgumentException("Book not borrowed by this user");
}
book.setAvailable(true);
}
@Override
public List<Book> getBorrowedBooks() {
return new ArrayList<>(borrowedBooks);
}
}
@Override
public boolean matches(String query) {
return title.contains(query) || author.contains(query) || isbn.contains(query);
}
class Loan {
private Book book;
private User user;
private LocalDate dueDate;
private LocalDate returnDate;
private static final double DAILY_FINE = 0.50;
class Library {
private List<Book> books = new ArrayList<>();
private List<User> users = new ArrayList<>();
private List<Loan> loans = new ArrayList<>();
try {
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
handleStaffActions();
break;
case 2:
handleUserActions();
break;
case 3:
System.out.println("Exiting system...");
return;
default:
System.out.println("Invalid option!");
}
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
scanner.nextLine();
}
}
}
try {
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
addBook();
break;
case 2:
registerUser();
break;
case 3:
return;
default:
System.out.println("Invalid option!");
}
} catch (InputMismatchException e) {
System.out.println("Invalid input! Please enter a number.");
scanner.nextLine();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
User newUser;
if (type == 1) {
System.out.print("Enter major: ");
String major = scanner.nextLine();
newUser = new Student(name, id, contact, major);
} else if (type == 2) {
System.out.print("Enter department: ");
String dept = scanner.nextLine();
newUser = new Faculty(name, id, contact, dept);
} else {
System.out.println("Invalid user type!");
return;
}
staff.registerUser(newUser);
System.out.println("User registered successfully!");
} catch (Exception e) {
System.out.println("Error registering user: " + e.getMessage());
}
}
if (user == null) {
System.out.println("User not found!");
return;
}
while (true) {
System.out.println("\nUser Menu:");
System.out.println("1. Borrow Book");
System.out.println("2. Return Book");
System.out.println("3. Search Books");
System.out.println("4. View Fines");
System.out.println("5. Back to Main");
System.out.print("Select option: ");
switch (choice) {
case 1:
borrowBook(user);
break;
case 2:
returnBook(user);
break;
case 3:
searchBooks();
break;
case 4:
viewFines(user);
break;
case 5:
return;
default:
System.out.println("Invalid option!");
}
}
} catch (InputMismatchException e) {
System.out.println("Invalid input! Please enter a number.");
scanner.nextLine();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
private static void borrowBook(User user) {
try {
System.out.print("Enter search query (title/author/ISBN): ");
String query = scanner.nextLine();
List<Book> availableBooks = library.searchBooks(query).stream()
.filter(Book::isAvailable)
.collect(Collectors.toList());
if (results.isEmpty()) {
System.out.println("No books found.");
return;
}
System.out.println("\nSearch Results:");
results.forEach(book -> System.out.printf(
"- %s by %s (ISBN: %s) %s%n",
book.getTitle(),
book.getAuthor(),
book.getIsbn(),
book.isAvailable() ? "[Available]" : "[Borrowed]"
));
} catch (Exception e) {
System.out.println("Search error: " + e.getMessage());
}
}
System.out.println("\nCurrent Fines:");
for (Loan loan : userLoans) {
double fine = loan.calculateFine();
if (fine > 0) {
System.out.printf("- %s: Due %s, Fine $%.2f%n",
loan.getBook().getTitle(),
loan.getDueDate(),
fine
);
totalFine += fine;
}
}
System.out.printf("Total fines due: $%.2f%n", totalFine);
}
System.out.println("\nSelect a book:");
for (int i = 0; i < books.size(); i++) {
Book book = books.get(i);
System.out.printf("%d. %s by %s [%s]%n",
i + 1,
book.getTitle(),
book.getAuthor(),
book.isAvailable() ? "Available" : "Borrowed");
}
class Staff {
private Library library;
output