When the user of the software specifies action 4, your program must ask the user to type the name of a user, and a number of books, and the program then uses that number to increase the number of books lent or borrowed by the user. Then the program goes back to the main menu.If the name of the user is wrong, then an UnknownUserException exception will be thrown by the Library object. The code of the main method of your CLI class must catch this exception, print the error message from the exception object, and then it just goes back to printing the menu of actions (by just going back to the beginning of the while loop). Note that, even if a consumer is a borrower, the readPosInt method prevents the typed number of books from being negative. This means a borrower will never throw a NotALenderException. Nevertheless the code of the main method of your CLI class must handle this exception by printing the error message "BUG! This must never happen!" and immediately terminating the program using System.exit(1).
时间: 2023-11-22 09:55:39 浏览: 67
Armv7-a 参考手册
Here's an example implementation of the main method of the CLI class that follows the above requirements:
```java
import java.util.Scanner;
public class CLI {
private static final Scanner scanner = new Scanner(System.in);
private static final Library library = new Library();
public static void main(String[] args) {
while (true) {
printMenu();
int action = readPosInt("Type an action number:");
try {
switch (action) {
case 1:
System.out.println(library.listBooks());
break;
case 2:
String author = readString("Type an author name:");
System.out.println(library.listBooksByAuthor(author));
break;
case 3:
String title = readString("Type a book title:");
System.out.println(library.findBook(title));
break;
case 4:
String user = readString("Type a user name:");
int numBooks = readPosInt("Type a number of books:");
library.lendOrBorrow(user, numBooks);
break;
case 5:
System.exit(0);
default:
System.out.println("Unknown action number.");
break;
}
} catch (UnknownUserException e) {
System.out.println(e.getMessage());
} catch (NotALenderException e) {
System.out.println("BUG! This must never happen!");
System.exit(1);
}
}
}
private static void printMenu() {
System.out.println("Menu:");
System.out.println("[1] List all books");
System.out.println("[2] List books by author");
System.out.println("[3] Find a book by title");
System.out.println("[4] Lend/borrow books for a user");
System.out.println("[5] Quit");
}
private static String readString(String prompt) {
System.out.println(prompt);
return scanner.nextLine().trim();
}
private static int readPosInt(String prompt) {
int num = -1;
while (num <= 0) {
System.out.println(prompt);
try {
num = Integer.parseInt(scanner.nextLine().trim());
} catch (NumberFormatException e) {
System.out.println("Invalid number.");
}
}
return num;
}
}
```
Note that the code uses a `Library` object to perform the actions requested by the user, and catches the `UnknownUserException` and `NotALenderException` exceptions as required.
阅读全文