For input string: "Lender"
时间: 2023-12-03 07:05:37 浏览: 35
I'm sorry, could you please provide more context or information? Are you asking for the meaning or definition of the word "lender"? Or are you trying to use it in a sentence?
相关问题
write java。The create method takes as arguments the name of a new library user, a number of books (as a string), and an integer representing the role of user to create (where the integer 0 means a lender and the integer 1 means a borrower). The create method of the controller then transforms the book number from a string to an integer (using the Integer.parseInt static method), creates an object from the correct class (based on the role specified by the user input: lender or borrower) and calls the addUser method of the library to add the new user object to the library. • If no exception occurs then the create method of the controller returns the empty string. • If the constructor of the Borrower class throws a NotALenderException then the create method of the controller must catch this exception and return as result the error message from the exception object. • If the parseInt method of the Integer class throws a NumberFormatException (because the user typed something which is not an integer) then the create method of the controller must catch this exception and return as result the error message from the exception object.
Here is a possible implementation of the `create` method in Java:
```java
public class LibraryController {
private Library library;
public LibraryController(Library library) {
this.library = library;
}
public String create(String name, String bookString, int role) {
try {
int bookCount = Integer.parseInt(bookString);
User user;
if (role == 0) {
user = new Lender(name, bookCount);
} else if (role == 1) {
user = new Borrower(name, bookCount);
} else {
return "Invalid role: " + role;
}
library.addUser(user);
return "";
} catch (NotALenderException e) {
return e.getMessage();
} catch (NumberFormatException e) {
return "Invalid book count: " + bookString;
}
}
}
```
In this implementation, the `create` method takes three arguments: the name of the new user, the book count as a string, and the role as an integer. The method first tries to parse the book count using `Integer.parseInt`, and if successful, creates a user object based on the role specified by the user input. If the role is not 0 or 1, an error message is returned.
If the user object is successfully created, the method calls the `addUser` method of the library to add the new user object to the library. If no exception occurs, an empty string is returned.
If the constructor of the `Borrower` class throws a `NotALenderException`, the method catches the exception and returns the error message from the exception object.
If the `parseInt` method of the `Integer` class throws a `NumberFormatException` (because the user typed something which is not an integer), the method catches the exception and returns an error message indicating that the book count is invalid.
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);
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.