write java.The getBook method takes the name of a user as argument. The getBook method of the controller then calls the getBook method of the library to get the number of books currently borrowed by that user. The getBook method of the controller then transforms the integer result of the getBook method of the library into a string and returns that string as result (to the view). If the getBook method of the library throws an UnknownUserException then the getBook method of the controller must catch this exception and return as result the error message from the exception object.
时间: 2023-11-28 10:51:31 浏览: 87
Here is a sample Java code that implements the described functionality:
```java
class Library {
public int getBook(String username) throws UnknownUserException {
// Implementation of getBook method of library
// Throws UnknownUserException if the user is not found
}
}
class Controller {
private Library library;
public Controller(Library library) {
this.library = library;
}
public String getBook(String username) {
try {
int numBooks = library.getBook(username);
return Integer.toString(numBooks);
} catch (UnknownUserException e) {
return e.getMessage();
}
}
}
```
In this code, the `Library` class has a `getBook` method that takes a username as argument and returns the number of books borrowed by that user. If the user is not found, it throws an `UnknownUserException` exception.
The `Controller` class has a reference to the `Library` instance and provides a `getBook` method that takes a username as argument. It calls the `getBook` method of the library to get the number of books borrowed by the user. If the library method throws an exception, the controller method catches it and returns the error message from the exception object. Otherwise, it transforms the integer result into a string and returns it as the method result.
阅读全文