Also create a ControllerCreate class that extends Controller.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. 完成符合以上要求的java代码
时间: 2023-07-16 12:14:55 浏览: 91
下面是符合要求的Java代码:
```java
public class ControllerCreate extends Controller {
public String create(String name, String bookCount, int role) {
try {
int bookNum = Integer.parseInt(bookCount);
User user = null;
if (role == 0) {
user = new Lender(name, bookNum);
} else if (role == 1) {
user = new Borrower(name, bookNum);
} else {
return "Invalid role specified.";
}
library.addUser(user);
return "";
} catch (NumberFormatException e) {
return e.getMessage();
} catch (NotALenderException e) {
return e.getMessage();
}
}
}
```
在这个类中,我们定义了一个名为 `create` 的方法,该方法接收三个参数:用户名称、书籍数量和用户角色。根据用户角色的不同,我们创建不同类型的用户对象,并将其添加到图书馆中。如果出现异常,我们将捕获并返回相应的错误消息。
阅读全文