However, a borrower cannot lend books, so the number of books borrowed by the borrower must always be positive or zero, never negative. If the argument given to the moreBook method is too negative and would change the book variable into a negative value, then the number of books borrowed by the borrower must not change and the moreBook method must throw a NotALenderException with the message “A borrower cannot lend XXX book(s).”, where XXX is replaced with the result of -(book + number).java代码
时间: 2024-01-21 22:01:51 浏览: 21
根据您提供的问题,我猜测您想问的是,如何在Java中实现根据条件抛出NotALenderException异常。您可以使用以下代码实现:
```java
public void moreBook(int number) throws NotALenderException {
if (book + number < 0) {
throw new NotALenderException("A borrower cannot lend " + (-(book + number)) + " book(s).");
}
book += number;
}
```
在代码中,如果借阅者所借图书的数量将导致图书变量成为负数,则会抛出NotALenderException异常,并显示消息“A borrower cannot lend XXX book(s).”,其中XXX是计算得出的值。否则,将更改book变量的值。注意,方法声明中使用了throws关键字,以指示该方法可能抛出NotALenderException异常。
相关问题
Add a class Borrower that extends User. The constructor of the Borrower class takes a name and a number of books borrowed by the borrower. If the number of books given as argument is strictly less than zero, then the constructor must throw a NotALenderException with the message "A new borrower cannot lend books.". The borrower class does not have any instance variable.java代码
Sure, here's your Java code:
```java
public class Borrower extends User {
public Borrower(String name, int numBooksBorrowed) throws NotALenderException {
super(name);
if(numBooksBorrowed < 0) {
throw new NotALenderException("A new borrower cannot lend books.");
}
}
}
```
Here, we have defined a `Borrower` class that extends the `User` class. The constructor of the `Borrower` class takes a name and a number of books borrowed by the borrower. If the number of books given as argument is strictly less than zero, then the constructor throws a `NotALenderException` with the message "A new borrower cannot lend books.".
Note that we are calling the constructor of the parent `User` class using the `super` keyword, passing in the `name` argument. The `Borrower` class does not have any instance variables.
Note that, even if a consumer is a borrower, the readPosInt method prevents the typed number of books from being negative. This means a borrower will never throw a NotALenderException. 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).Javacase实现
以下是一个可能的Javacase实现,用于处理上述情况:
```java
public class CLI {
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the library!");
System.out.print("Are you a lender or a borrower? ");
String input = scanner.nextLine().trim().toLowerCase();
if (input.equals("lender")) {
System.out.print("Enter the number of books you want to lend: ");
int numBooks = readPosInt(scanner);
Lender lender = new Lender(numBooks);
System.out.println("You have successfully registered as a lender.");
// do something with lender object
} else if (input.equals("borrower")) {
System.out.print("Enter the number of books you want to borrow: ");
int numBooks = readPosInt(scanner);
Borrower borrower = new Borrower(numBooks);
System.out.println("You have successfully registered as a borrower.");
// do something with borrower object
} else {
System.out.println("Invalid input. Please try again.");
}
} catch (NotALenderException e) {
System.out.println("BUG! This must never happen!");
System.exit(1);
}
}
public static int readPosInt(Scanner scanner) throws NotALenderException {
int numBooks = scanner.nextInt();
if (numBooks < 0) {
throw new NotALenderException("Number of books cannot be negative.");
}
return numBooks;
}
}
```
该实现包括一个名为`readPosInt`的辅助方法,该方法使用`Scanner`对象读取正整数,并在输入为负数时引发`NotALenderException`。在`main`方法中,我们捕获该异常并立即终止程序。