In the empty main method of the CLI class, create a single Library object with the name "UIC Library". The main method of the CLI class must then print a menu that allows the user of your system to do six different actions that involve the library object, and your program must then read an integer from the user that indicates which action must be performed by the program (see below for the details about each action). Use the readPosInt method to print the menu (give the string for the menu as the argument of readPosInt) and to read the integer typed by the user.
时间: 2023-07-02 14:14:04 浏览: 57
Sure, I understand your question. Here is the code snippet that you can use to create a Library object named "UIC Library" and print a menu that allows the user to do six different actions:
```java
public class CLI {
public static void main(String[] args) {
Library library = new Library("UIC Library");
int choice = 0;
while (choice != 6) {
System.out.println("Welcome to " + library.getName() + "!");
System.out.println("Please select an action:");
System.out.println("1. Add a book to the library");
System.out.println("2. Remove a book from the library");
System.out.println("3. Check out a book");
System.out.println("4. Return a book");
System.out.println("5. List all books in the library");
System.out.println("6. Exit");
choice = readPosInt("Enter your choice (1-6): ");
switch (choice) {
case 1:
// add a book to the library
break;
case 2:
// remove a book from the library
break;
case 3:
// check out a book
break;
case 4:
// return a book
break;
case 5:
// list all books in the library
break;
case 6:
System.out.println("Thanks for using " + library.getName() + "!");
break;
default:
System.out.println("Invalid choice. Please try again.");
break;
}
}
}
private static int readPosInt(String message) {
Scanner scanner = new Scanner(System.in);
int value = 0;
do {
System.out.print(message);
try {
value = scanner.nextInt();
} catch (InputMismatchException ex) {
scanner.nextLine(); // consume the invalid input
}
} while (value <= 0);
return value;
}
}
```
This code creates a Library object named "UIC Library", and then presents a menu of six options to the user. The `readPosInt` method is used to read an integer from the user that indicates which action should be performed. The `while` loop keeps running until the user chooses to exit by selecting option 6. The implementation of each action should be added to the corresponding `case` block in the `switch` statement.