Also create a ControllerMoreBook class that extends Controller.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. Note: to keep things simple, it is allowed for a user of your system to increase the number of books of a user by a negative number, so there is no need to check for that. Modify the run method of the GUI class to add a ViewMoreBook view that uses a ControllerMoreBook controller and the same model as before (not a new model!) Do not delete the previous views. Run your GUI and check that you can correctly use the new view to increase the number of books for different users of your library (obviously your library must have some users in it to test this: see the last paragraph of Question 7). • Check that, when you increase a user’s book, the simple view is automatically correctly updated to show the new total number of borrowed books for all users of the library. • Also use the “get book” view to check that the user’s book value correctly changed. • Also check that increasing the book number of an unknown user correctly shows an error message.Also check that increasing the book of a user by a large negative number correctly shows an error message. Also check that trying to increase the book of a user by a number which is not an integer correctly shows an error message (do not worry about the content of the error message). 完成符合以上要求的java代码
时间: 2023-07-03 07:03:51 浏览: 99
js代码-class A, typeof() 输出 function
Library.java:
```java
import java.util.HashMap;
import java.util.Map;
public class Library {
private Map<String, User> users;
public Library() {
users = new HashMap<>();
}
public void addUser(User user) {
users.put(user.getName(), user);
}
public void moreBook(String name, int num) throws UnknownUserException, NotALenderException {
User user = users.get(name);
if (user == null) {
throw new UnknownUserException(name);
}
if (!(user instanceof Lender)) {
throw new NotALenderException(name);
}
Lender lender = (Lender) user;
lender.moreBook(num);
}
public int getNumBorrowed(String name) throws UnknownUserException {
User user = users.get(name);
if (user == null) {
throw new UnknownUserException(name);
}
return user.getNumBorrowed();
}
}
```
User.java:
```java
public abstract class User {
private String name;
private int numBorrowed;
public User(String name) {
this.name = name;
numBorrowed = 0;
}
public String getName() {
return name;
}
public int getNumBorrowed() {
return numBorrowed;
}
public void setNumBorrowed(int numBorrowed) {
this.numBorrowed = numBorrowed;
}
}
```
Lender.java:
```java
public class Lender extends User {
public Lender(String name) {
super(name);
}
public void moreBook(int num) {
setNumBorrowed(getNumBorrowed() + num);
}
}
```
UnknownUserException.java:
```java
public class UnknownUserException extends Exception {
private String name;
public UnknownUserException(String name) {
this.name = name;
}
public String getMessage() {
return "Unknown user: " + name;
}
}
```
NotALenderException.java:
```java
public class NotALenderException extends Exception {
private String name;
public NotALenderException(String name) {
this.name = name;
}
public String getMessage() {
return name + " is not a lender";
}
}
```
ControllerMoreBook.java:
```java
public class ControllerMoreBook extends Controller {
private Library library;
public ControllerMoreBook(Library library) {
this.library = library;
}
public String moreBook(String name, String numStr) {
try {
int num = Integer.parseInt(numStr);
library.moreBook(name, num);
return "";
} catch (UnknownUserException e) {
return e.getMessage();
} catch (NotALenderException e) {
return e.getMessage();
} catch (NumberFormatException e) {
return "Invalid number format";
}
}
}
```
ViewMoreBook.java:
```java
import javax.swing.*;
import java.awt.*;
public class ViewMoreBook extends JFrame implements View {
private JTextField nameField;
private JTextField numField;
private JButton okButton;
private JTextArea resultArea;
private ControllerMoreBook controller;
public ViewMoreBook(ControllerMoreBook controller) {
this.controller = controller;
setTitle("More Books");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
panel.add(new JLabel("Name:"));
nameField = new JTextField();
panel.add(nameField);
panel.add(new JLabel("Number of Books:"));
numField = new JTextField();
panel.add(numField);
okButton = new JButton("OK");
panel.add(okButton);
resultArea = new JTextArea();
resultArea.setEditable(false);
panel.add(resultArea);
add(panel);
okButton.addActionListener(e -> {
String name = nameField.getText();
String numStr = numField.getText();
String result = controller.moreBook(name, numStr);
resultArea.setText(result);
});
}
}
```
修改 GUI 类的 run 方法:
```java
public void run() {
Library library = new Library();
library.addUser(new Lender("Alice"));
library.addUser(new Lender("Bob"));
library.addUser(new Borrower("Charlie"));
ControllerSimple simpleController = new ControllerSimple(library);
ViewSimple simpleView = new ViewSimple(simpleController);
simpleView.setVisible(true);
ControllerGetBook getBookController = new ControllerGetBook(library);
ViewGetBook getBookView = new ViewGetBook(getBookController);
getBookView.setVisible(true);
ControllerMoreBook moreBookController = new ControllerMoreBook(library);
ViewMoreBook moreBookView = new ViewMoreBook(moreBookController);
moreBookView.setVisible(true);
}
```
阅读全文