-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLibraryManagementSystem.java
More file actions
69 lines (51 loc) · 2.91 KB
/
LibraryManagementSystem.java
File metadata and controls
69 lines (51 loc) · 2.91 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
import java.util.Scanner;
public class LibraryManagementSystem {
public static void main(String[] args){
// Create a scanner object for user input.
Scanner scanner = new Scanner(System.in);
// Create a Library object to manage the collection of the books.
Library library = new Library();
// Add some books to the Library's collection.
library.addBook(new Book("Verity", "Colleen Hoover"));
library.addBook(new Book("The Secret", "Rhonda Byrne"));
library.addBook(new Book("Grit", "Angela Duckworth"));
// Infinite loop to keep the system running until the user chooses to exit.
while(true) {
// Display the available books in the Library.
System.out.println("\nAvailable Books: ");
library.displayAvailableBooks();
// Prompt the user for an action: borrow, return, or exit.
System.out.println("\nDo you want to borrow or return a book?");
System.out.println("Type 'borrow' to borrow a book, 'return' to return a book, or 'exit' to quit.");
String action = scanner.nextLine().toLowerCase(); // Read the userinput and convert it into lower case.
// Check if the user wants to borrow a book.
if(action.equalsIgnoreCase("Borrow")){
// Get the title and author of the book to be borrowed.
System.out.print("Enter the title of the book you want to borrow: ");
String borrowTitle = scanner.nextLine();
System.out.print("Enter the author of the book you want to borrow: ");
String borrowAuthor = scanner.nextLine();
// Borrow the specified book from the Library.
library.borrowBook(borrowTitle, borrowAuthor);
} // Check if the user wants to return a book.
else if(action.equalsIgnoreCase("Return")){
// Get the title and author of the book to return.
System.out.print("Enter the title of the book you want to return: ");
String returnTitle = scanner.nextLine();
System.out.print("Enter the author of the book you want to return: ");
String returnAuthor = scanner.nextLine();
// Return the specified bookt o the Library.
library.returnBook(returnTitle, returnAuthor);
} // Check if the user wants to exit the system.
else if (action.equalsIgnoreCase("exit")) {
// Exit condition
System.out.println("Exiting the system. Goodbye!");
break;
} // Handle inavlid input from the user.
else {
System.out.println("Invalid option. Please enter 'borrow', 'return', or 'exit'.");
}
} // Close the scanner to prevent resource leaks.
scanner.close();
}
}