For input string: "Lender"
时间: 2023-12-03 11:05:37 浏览: 59
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.
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 is the code implementation for adding a new user to the library based on the given specifications:
```
import java.util.Scanner;
public class CLI {
private static Library library = new Library();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int action;
do {
System.out.print("Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): ");
action = scanner.nextInt();
switch (action) {
case 1:
System.out.println("Total number of books in the library: " + library.getTotal());
break;
case 2:
addNewUser(scanner);
break;
case 3:
getBooks(scanner);
break;
case 4:
addBooks(scanner);
break;
case 5:
lessBooks(scanner);
break;
case 6:
System.out.println("Goodbye!");
break;
default:
System.out.println("Unknown action!");
}
} while (action != 6);
}
private static void addNewUser(Scanner scanner) {
System.out.print("Type the user role (lender:1 borrower:2): ");
int role = scanner.nextInt();
if (role == 1) {
System.out.print("Enter the name of the user: ");
String name = scanner.next();
System.out.print("Enter the initial number of lent books: ");
int numBooks = readPosInt(scanner);
Lender lender = new Lender(name, numBooks);
library.addUser(lender);
System.out.println("Lender \"" + name + "\" lending " + numBooks + " book(s) has been added.");
} else if (role == 2) {
System.out.print("Enter the name of the user: ");
String name = scanner.next();
System.out.print("Enter the initial number of borrowed books: ");
int numBooks = readPosInt(scanner);
Borrower borrower = new Borrower(name, numBooks);
library.addUser(borrower);
System.out.println("Borrower \"" + name + "\" borrowing " + numBooks + " book(s) has been added.");
} else {
System.out.println("Unknown user role!");
}
}
private static void getBooks(Scanner scanner) {
System.out.print("Enter the name of the borrower: ");
String name = scanner.next();
User user = library.findUser(name);
if (user != null && user instanceof Borrower) {
System.out.print("Enter the number of books borrowed: ");
int numBooks = readPosInt(scanner);
try {
library.getBorrowed((Borrower) user, numBooks);
System.out.println(numBooks + " book(s) have been borrowed by " + name);
} catch (NotEnoughBooksException e) {
System.out.println("Not enough books available to borrow!");
}
} else {
System.out.println("Borrower \"" + name + "\" not found!");
}
}
private static void addBooks(Scanner scanner) {
System.out.print("Enter the name of the lender: ");
String name = scanner.next();
User user = library.findUser(name);
if (user != null && user instanceof Lender) {
System.out.print("Enter the number of books lent: ");
int numBooks = readPosInt(scanner);
try {
library.getLent((Lender) user, numBooks);
System.out.println(numBooks + " book(s) have been lent by " + name);
} catch (NotEnoughSpaceException e) {
System.out.println("Not enough space available to lend books!");
}
} else {
System.out.println("Lender \"" + name + "\" not found!");
}
}
private static void lessBooks(Scanner scanner) {
System.out.print("Enter the name of the borrower: ");
String name = scanner.next();
User user = library.findUser(name);
if (user != null && user instanceof Borrower) {
System.out.print("Enter the number of books returned: ");
int numBooks = readPosInt(scanner);
try {
library.returnBooks((Borrower) user, numBooks);
System.out.println(numBooks + " book(s) have been returned by " + name);
} catch (NotEnoughBooksException e) {
System.out.println("Invalid number of books returned!");
}
} else {
System.out.println("Borrower \"" + name + "\" not found!");
}
}
private static int readPosInt(Scanner scanner) {
int num = scanner.nextInt();
if (num < 0) {
System.out.println("BUG! This must never happen!");
System.exit(1);
}
return num;
}
}
```
Note that the method `readPosInt` is used to read a positive integer input from the user, and it handles the case where the user enters a negative integer by printing an error message and immediately terminating the program using `System.exit(1)`.
阅读全文