-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
82 lines (70 loc) · 3.2 KB
/
Copy pathMain.java
File metadata and controls
82 lines (70 loc) · 3.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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Library library = new Library();
Scanner scanner = new Scanner(System.in);
// Add some default books
library.addBook(new Book("The Alchemist", "Paulo Coelho"));
library.addBook(new Book("1984", "George Orwell"));
library.addBook(new Book("To Kill a Mockingbird", "Harper Lee"));
System.out.print("Enter your name: ");
User user = new User(scanner.nextLine());
while (true) {
System.out.println("\n=== Library Menu ===");
System.out.println("1. View available books");
System.out.println("2. Issue a book");
System.out.println("3. Return a book");
System.out.println("4. Show issued books");
System.out.println("5. Exit");
System.out.print("Enter choice: ");
int choice = scanner.nextInt();
scanner.nextLine(); // consume newline
switch (choice) {
case 1:
System.out.println("Available Books:");
for (Book book : library.getAvailableBooks()) {
System.out.println(book.getTitle() + " by " + book.getAuthor());
}
break;
case 2:
System.out.print("Enter book title to issue: ");
String issueTitle = scanner.nextLine();
Book bookToIssue = findBookByTitle(library, issueTitle);
if (bookToIssue != null && library.issueBook(bookToIssue, user)) {
System.out.println("Book issued successfully.");
} else {
System.out.println("Book not available or already issued.");
}
break;
case 3:
System.out.print("Enter book title to return: ");
String returnTitle = scanner.nextLine();
Book bookToReturn = findBookByTitle(library, returnTitle);
if (bookToReturn != null && library.returnBook(bookToReturn)) {
System.out.println("Book returned successfully.");
} else {
System.out.println("Book not found or not issued.");
}
break;
case 4:
library.showIssuedBooks();
break;
case 5:
System.out.println("Thank you. Exiting.");
scanner.close();
return;
default:
System.out.println("Invalid choice. Try again.");
}
}
}
private static Book findBookByTitle(Library library, String title) {
for (Book book : library.getAvailableBooks()) {
if (book.getTitle().equalsIgnoreCase(title)) {
return book;
}
}
// Search also in issued books (for returning)
return new Book(title, ""); // fallback (but may fail due to equals() logic)
}
}