Repository navigation
Expand file tree
/
Copy pathLibraryApp.java
More file actions
127 lines (108 loc) · 4.82 KB
/
Copy pathLibraryApp.java
File metadata and controls
127 lines (108 loc) · 4.82 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
import java.util.List;
import java.util.Scanner;
public class LibraryApp {
public static void main(String[] args) {
// 1. Initialize the Library and Scanner for input
Library library = new Library();
Scanner scanner = new Scanner(System.in);
// A few initial books for testing purposes (Optional)
library.addBook(new Book("The Java Programming", "James Gosling", "978-0131489025"));
library.addBook(new Book("Data Structures in Java", "Goodrich", "978-1119475501"));
int choice = -1;
// 2. Main application loop
while (choice != 6) {
displayMenu();
// Handle non-integer input to prevent crash
if (scanner.hasNextInt()) {
choice = scanner.nextInt();
scanner.nextLine(); // Consume the newline character
} else {
System.out.println("Invalid input. Please enter a number (1-6).");
scanner.nextLine(); // Consume the invalid input
continue; // Skip the rest of the loop and show menu again
}
// 3. Process the user's choice
switch (choice) {
case 1:
addBookUI(library, scanner);
break;
case 2:
library.displayAllBooks();
break;
case 3:
searchBookUI(library, scanner);
break;
case 4:
issueBookUI(library, scanner, "Issue");
break;
case 5:
issueBookUI(library, scanner, "Return");
break;
case 6:
System.out.println("\nThank you for using the Library Management System. Exiting...");
break;
default:
System.out.println("Invalid choice. Please select a number between 1 and 6.");
}
}
scanner.close(); // Clean up the scanner resource
}
// --- Menu Display Method ---
private static void displayMenu() {
System.out.println("\n==================================");
System.out.println(" Library Management System Menu ");
System.out.println("==================================");
System.out.println("1. Add New Book");
System.out.println("2. View All Books");
System.out.println("3. Search Book (Title or ISBN)");
System.out.println("4. Issue Book (Mark as Issued)");
System.out.println("5. Return Book (Mark as Available)");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
}
// --- UI Helper Method: Add Book ---
private static void addBookUI(Library library, Scanner scanner) {
System.out.println("\n--- Add New Book ---");
System.out.print("Enter Title: ");
String title = scanner.nextLine();
System.out.print("Enter Author: ");
String author = scanner.nextLine();
System.out.print("Enter ISBN (Unique ID): ");
String isbn = scanner.nextLine();
// Create the Book object and pass it to the Library
Book newBook = new Book(title, author, isbn);
library.addBook(newBook);
}
// --- UI Helper Method: Search Book ---
private static void searchBookUI(Library library, Scanner scanner) {
System.out.println("\n--- Search Book ---");
System.out.print("Enter Title or ISBN to search: ");
String query = scanner.nextLine();
List<Book> results = library.searchBook(query);
if (results.isEmpty()) {
System.out.println("No books found matching '" + query + "'.");
} else {
System.out.println("\n--- Found " + results.size() + " Book(s) ---");
for (Book book : results) {
System.out.println(book);
}
System.out.println("------------------------------------");
}
}
// --- UI Helper Method: Issue/Return Book ---
private static void issueBookUI(Library library, Scanner scanner, String action) {
System.out.println("\n--- " + action + " Book ---");
System.out.print("Enter ISBN of the book to " + action.toLowerCase() + ": ");
String isbn = scanner.nextLine();
boolean success = false;
if (action.equals("Issue")) {
success = library.issueBook(isbn);
} else if (action.equals("Return")) {
success = library.returnBook(isbn);
}
if (success) {
System.out.println("Success! Book with ISBN " + isbn + " has been " + action.toLowerCase() + "d.");
}
// Note: Failure messages are handled inside the Library class methods
}
}