帮我看看我的代码有什么错误:public static void moreBook(String name, int number) throws NotALenderException, UnknownUserException { for (User user : users) { if (user.getName().equals(name)) { if (user instanceof Lender) { ((Lender) user).moreBook(number); } else if (user instanceof Borrower) { ((Borrower) user).moreBook(number); } notifyListeners(); return; } } throw new UnknownUserException("User " + name + " unknown."); }
时间: 2023-06-27 09:07:09 浏览: 92
该代码段看起来没有明显的错误,但是需要确保以下几点:
1. `users` 中是否包含了所有的用户对象,如果不包含,那么该方法可能会抛出 `UnknownUserException`。
2. 在调用 `moreBook()` 方法之前,需要确保该用户对象是 Lender 或 Borrower 类型的,否则会抛出 `NotALenderException` 异常。
3. `notifyListeners()` 方法是否被正确调用。
如果以上几点都被满足,则该方法应该可以正常工作。
相关问题
write java。The moreBook method takes the name of a user and a number of books (as a string) as arguments. The moreBook method of the controller then transforms the number of books from a string to an integer (using the Integer.parseInt static method) and calls the moreBook method of the library to increase the number of books borrowed or lent by the user (depending on what kind of user it is) of a specific user, by the given argument. • If no exception occurs then the moreBook method of the controller returns the empty string. • If the moreBook method of the library throws an UnknownUserException then the moreBook method of the controller must catch this exception and return as result the error message from the exception object. • If the moreBook method of the library throws a NotALenderException then the moreBook 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 moreBook method of the controller must catch this exception and return as result the error message from the exception object.
Here is the Java code for the described functionality:
```
public class LibraryController {
private Library library;
public LibraryController(Library library) {
this.library = library;
}
public String moreBook(String userName, String numBooks) {
try {
int num = Integer.parseInt(numBooks);
library.moreBook(userName, num);
return "";
} catch (UnknownUserException e) {
return e.getMessage();
} catch (NotALenderException e) {
return e.getMessage();
} catch (NumberFormatException e) {
return e.getMessage();
}
}
}
public class Library {
private Map<String, User> users;
public Library() {
users = new HashMap<>();
}
public void moreBook(String userName, int numBooks) throws UnknownUserException, NotALenderException {
User user = users.get(userName);
if (user == null) {
throw new UnknownUserException("Unknown user: " + userName);
}
if (!(user instanceof Lender)) {
throw new NotALenderException(userName + " is not a lender");
}
((Lender) user).moreBooks(numBooks);
}
}
public class User {
private String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
public class Lender extends User {
private int numBooks;
public Lender(String name) {
super(name);
numBooks = 0;
}
public int getNumBooks() {
return numBooks;
}
public void moreBooks(int num) {
numBooks += num;
}
}
public class UnknownUserException extends Exception {
public UnknownUserException(String message) {
super(message);
}
}
public class NotALenderException extends Exception {
public NotALenderException(String message) {
super(message);
}
}
```
The LibraryController class has a constructor that takes a Library object as a parameter. The moreBook method takes a user name and a number of books as arguments. It uses the Integer.parseInt static method to parse the number of books from a string to an integer. It then calls the moreBook method of the Library object to increase the number of books borrowed or lent by the user, depending on what kind of user it is.
If no exception occurs, the moreBook method of the controller returns the empty string. If the moreBook method of the library throws an UnknownUserException, the moreBook method of the controller catches this exception and returns the error message from the exception object. If the moreBook method of the library throws a NotALenderException, the moreBook method of the controller catches this exception and returns the error message from the exception object. If the parseInt method of the Integer class throws a NumberFormatException, the moreBook method of the controller catches this exception and returns the error message from the exception object.
The Library class has a map of users, where the key is the user name and the value is a User object. The moreBook method takes a user name and a number of books as arguments. It throws an UnknownUserException if the user is not found in the map, and a NotALenderException if the user is not an instance of the Lender class. Otherwise, it calls the moreBooks method of the Lender object to increase the number of books borrowed by the user.
The User class has a name field and a getName method.
The Lender class extends the User class and adds a numBooks field and a moreBooks method.
The UnknownUserException and NotALenderException classes extend the Exception class and have constructors that take an error message as a parameter.
public void moreBook(int number) 实现 number 与 book 变量 相加
假设book是一个整型变量,实现方法如下:
```
public void moreBook(int number) {
book += number;
}
```
这里使用了 "+=" 运算符,可以将 number 与 book 相加并将结果赋值给 book。
阅读全文