Action 2: adding a new user to the library. When the user of the software specifies action 2, your program must add a new user to the library. To add a new user, your program needs to ask the user three things: the role of user (an integer read using readPosInt: the integer 1 represents lender, the integer 2 represents borrower, any other integer must result in an error message "Unknown user role!" being printed and the software going immediately back to the main menu), the name of the user (a string read using readLine), and the initial number of books that the user lends (for a lender) or borrows (for a borrower). You program must then create the correct user, add it to the library, and print an information message. The program then goes back to the menu. For example (where 2, 3, 2, 1, Anna, 5, 2, 2, Bob, and 10 are inputs from the user): Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 2 Type the user role (lender:1 borrower:2): 3 Unknown user role! Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 2 Type the user role (lender:1 borrower:2): 1 Enter the name of the user: Anna Enter the initial number of borrowed books: 5 Lender "Anna" lending 5 book(s) has been added. Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 2 Type the user role (lender:1 borrower:2): 2 Enter the name of the user: Bob Enter the initial number of borrowed books: 10 Borrower "Bob" borrowing 10 book(s) has been added. Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): Note that the readPosInt method prevents the initial number of books from being negative, so the constructor for the Borrower class will never throw a NotALenderException when you create a borrower object. 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-07-16 13:15:34 浏览: 97
在oracle中添加一个新用户
4星 · 用户满意度95%
Here's the code for action 2:
```java
case 2:
System.out.println("Type the user role (lender:1 borrower:2):");
int role = readPosInt(input); // read user's role
if (role == 1) { // lender
System.out.println("Enter the name of the user:");
String name = readLine(input); // read user's name
System.out.println("Enter the initial number of books lent:");
int numBooks = readPosInt(input); // read number of books lent
Lender lender = new Lender(name, numBooks); // create lender object
library.addUser(lender); // add lender to the library
System.out.println("Lender \"" + name + "\" lending " + numBooks + " book(s) has been added.");
} else if (role == 2) { // borrower
System.out.println("Enter the name of the user:");
String name = readLine(input); // read user's name
System.out.println("Enter the initial number of books borrowed:");
int numBooks = readPosInt(input); // read number of books borrowed
Borrower borrower = new Borrower(name, numBooks); // create borrower object
library.addUser(borrower); // add borrower to the library
System.out.println("Borrower \"" + name + "\" borrowing " + numBooks + " book(s) has been added.");
} else { // unknown role
System.out.println("Unknown user role!");
}
break;
```
Note that this code assumes that the `Library` object is already initialized and stored in a variable called `library`. Also, the `readPosInt` and `readLine` methods are not shown here, but they should behave as described in the problem statement. Finally, make sure to handle the `NotALenderException` as described in the problem statement.
阅读全文