Advertisement
LynchzDEV

LibraryManagement

Jul 24th, 2023
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | Source Code | 0 0
  1. package librarymanagement;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class LibraryManagement {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         Library library = new Library(10); // Create a library with a capacity of 10 books
  10.  
  11.         while (true) {
  12.             System.out.println("\n--- Library Management System ---");
  13.             System.out.println("1. Add a book");
  14.             System.out.println("2. Display available books");
  15.             System.out.println("3. Borrow a book");
  16.             System.out.println("4. Return a book");
  17.             System.out.println("5. Exit");
  18.             System.out.print("Enter your choice: ");
  19.  
  20.             int choice;
  21.             if (scanner.hasNextInt()) {
  22.                 choice = scanner.nextInt();
  23.                 System.out.println("");
  24.                 scanner.nextLine(); // Consume the newline character
  25.             } else {
  26.                 System.out.println("Invalid choice. Please try again.");
  27.                 scanner.nextLine(); // Consume the invalid input
  28.                 continue;
  29.             }
  30.  
  31.             switch (choice) {
  32.                 case 1:
  33.                     System.out.print("Enter book title: ");
  34.                     String title = scanner.nextLine();
  35.                     System.out.print("Enter book author: ");
  36.                     String author = scanner.nextLine();
  37.                     Book book = new Book(title, author);
  38.                     library.addBook(book);
  39.                     break;
  40.                 case 2:
  41.                     library.displayAvailableBooks();
  42.                     break;
  43.                 case 3:
  44.                     System.out.print("Enter the title of the book you want to borrow: ");
  45.                     String borrowTitle = scanner.nextLine();
  46.                     library.borrowBook(borrowTitle);
  47.                     break;
  48.                 case 4:
  49.                     System.out.print("Enter the title of the book you want to return: ");
  50.                     String returnTitle = scanner.nextLine();
  51.                     library.returnBook(returnTitle);
  52.                     break;
  53.                 case 5:
  54.                     System.out.println("Exiting... Thank you!");
  55.                     scanner.close();
  56.                     return;
  57.                 default:
  58.                     System.out.println("Invalid choice. Please try again.");
  59.             }
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement