Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package librarymanagement;
- import java.util.Scanner;
- public class LibraryManagement {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Library library = new Library(10); // Create a library with a capacity of 10 books
- while (true) {
- System.out.println("\n--- Library Management System ---");
- System.out.println("1. Add a book");
- System.out.println("2. Display available books");
- System.out.println("3. Borrow a book");
- System.out.println("4. Return a book");
- System.out.println("5. Exit");
- System.out.print("Enter your choice: ");
- int choice;
- if (scanner.hasNextInt()) {
- choice = scanner.nextInt();
- System.out.println("");
- scanner.nextLine(); // Consume the newline character
- } else {
- System.out.println("Invalid choice. Please try again.");
- scanner.nextLine(); // Consume the invalid input
- continue;
- }
- switch (choice) {
- case 1:
- System.out.print("Enter book title: ");
- String title = scanner.nextLine();
- System.out.print("Enter book author: ");
- String author = scanner.nextLine();
- Book book = new Book(title, author);
- library.addBook(book);
- break;
- case 2:
- library.displayAvailableBooks();
- break;
- case 3:
- System.out.print("Enter the title of the book you want to borrow: ");
- String borrowTitle = scanner.nextLine();
- library.borrowBook(borrowTitle);
- break;
- case 4:
- System.out.print("Enter the title of the book you want to return: ");
- String returnTitle = scanner.nextLine();
- library.returnBook(returnTitle);
- break;
- case 5:
- System.out.println("Exiting... Thank you!");
- scanner.close();
- return;
- default:
- System.out.println("Invalid choice. Please try again.");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement