You are trying to start Visual Studio Code as a super user which isn't recommended. If this was intended, please add the argument `--no-sandbox` and specify an alternate user data directory using the `--user-data-dir` argument. 是什么意思
时间: 2024-04-19 10:29:33 浏览: 236
这个提示意味着你正在尝试以超级用户(管理员)身份启动 Visual Studio Code。不建议以超级用户身份运行该应用程序,因为这可能会导致安全风险。如果你确实需要以超级用户身份运行 Visual Studio Code,请使用 `--no-sandbox` 参数,并使用 `--user-data-dir` 参数指定一个备用的用户数据目录。这样可以降低潜在的风险。
相关问题
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代码
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);
}
```
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. Modify the run method of the GUI class to add a ViewCreate view that uses a ControllerCreate controller and the same model as before (not a new model!) Do not delete the previous views. Note: if at the end of Question 7 you had manually added to your library (model object) some users for testing, then you must now remove those users from the run method of the anonymous class inside the GUI class. You do not need these test users anymore because you have now a graphical user interface to create new users! Run your GUI and check that you can correctly use the new view to create different users for your library, with different types of roles. • Check that, when you create a new user, the simple view is automatically correctly updated to show the new total number of books borrowed by all users. • Also use the “get book” view to check that the users are correctly created with the correct names and correct number of books. • Also check that trying to create a borrower with a negative number of books correctly shows an error message. Also check that trying to create a user with a number of books which is not an integer correctly shows an error message (do not worry about the content of the error message). After you created a new user, you can also check whether it is a lender or a borrower using the “more book” view to increase the number of books of the user by a big negative number: • if the new user you created is a lender, then increasing the number of books by a big negative value will work and the number of books borrowed by the user will just become a larger value (you can then check that using the “get book” view); • if the new user you created is a borrower, then increasing the number of books by a big negative value will fail with an error message and the number of books borrowed by the user will not change (you can then check that using the “get book” view). 完成符合以上要求的java代码
Model class:
```
import java.util.ArrayList;
public class Library {
private ArrayList<User> users;
private int totalBooksBorrowed;
public Library() {
users = new ArrayList<>();
totalBooksBorrowed = 0;
}
public void addUser(User user) {
users.add(user);
totalBooksBorrowed += user.getNumberOfBooks();
}
public void removeUser(User user) {
users.remove(user);
totalBooksBorrowed -= user.getNumberOfBooks();
}
public ArrayList<User> getUsers() {
return users;
}
public int getTotalBooksBorrowed() {
return totalBooksBorrowed;
}
}
```
User class:
```
public abstract class User {
private String name;
private int numberOfBooks;
public User(String name, int numberOfBooks) {
this.name = name;
this.numberOfBooks = numberOfBooks;
}
public String getName() {
return name;
}
public int getNumberOfBooks() {
return numberOfBooks;
}
public void setNumberOfBooks(int numberOfBooks) {
this.numberOfBooks = numberOfBooks;
}
public abstract boolean isLender();
}
```
Lender class:
```
public class Lender extends User {
public Lender(String name, int numberOfBooks) {
super(name, numberOfBooks);
}
@Override
public boolean isLender() {
return true;
}
}
```
Borrower class:
```
public class Borrower extends User {
public Borrower(String name, int numberOfBooks) throws NotALenderException {
super(name, numberOfBooks);
if (numberOfBooks < 0) {
throw new NotALenderException("A borrower can't have a negative number of books!");
}
}
@Override
public boolean isLender() {
return false;
}
}
```
NotALenderException class:
```
public class NotALenderException extends Exception {
public NotALenderException(String message) {
super(message);
}
}
```
ViewCreate class:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ViewCreate extends JPanel implements ActionListener {
private JTextField nameField = new JTextField(10);
private JTextField numberOfBooksField = new JTextField(10);
private JRadioButton lenderButton = new JRadioButton("Lender");
private JRadioButton borrowerButton = new JRadioButton("Borrower");
private ButtonGroup roleButtonGroup = new ButtonGroup();
private JButton createButton = new JButton("Create");
private ControllerCreate controller;
public ViewCreate(Model model) {
this.controller = new ControllerCreate(model, this);
setLayout(new GridBagLayout());
roleButtonGroup.add(lenderButton);
roleButtonGroup.add(borrowerButton);
lenderButton.setSelected(true);
GridBagConstraints gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = 0;
gc.anchor = GridBagConstraints.LINE_END;
gc.insets = new Insets(5, 5, 5, 5);
add(new JLabel("Name: "), gc);
gc.gridx++;
gc.anchor = GridBagConstraints.LINE_START;
add(nameField, gc);
gc.gridx = 0;
gc.gridy++;
gc.anchor = GridBagConstraints.LINE_END;
add(new JLabel("Number of books: "), gc);
gc.gridx++;
gc.anchor = GridBagConstraints.LINE_START;
add(numberOfBooksField, gc);
gc.gridx = 0;
gc.gridy++;
gc.anchor = GridBagConstraints.LINE_END;
add(new JLabel("Role: "), gc);
gc.gridx++;
gc.anchor = GridBagConstraints.LINE_START;
add(lenderButton, gc);
gc.gridx++;
add(borrowerButton, gc);
gc.gridx = 1;
gc.gridy++;
gc.anchor = GridBagConstraints.LINE_END;
add(createButton, gc);
createButton.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
String name = nameField.getText();
String numberOfBooksString = numberOfBooksField.getText();
int role = lenderButton.isSelected() ? 0 : 1;
String result = controller.create(name, numberOfBooksString, role);
JOptionPane.showMessageDialog(this, result, "Result", JOptionPane.INFORMATION_MESSAGE);
}
}
```
ControllerCreate class:
```
public class ControllerCreate extends Controller {
private ViewCreate view;
public ControllerCreate(Model model, ViewCreate view) {
super(model);
this.view = view;
}
public String create(String name, String numberOfBooksString, int role) {
try {
int numberOfBooks = Integer.parseInt(numberOfBooksString);
User user = null;
if (role == 0) {
user = new Lender(name, numberOfBooks);
} else if (role == 1) {
user = new Borrower(name, numberOfBooks);
}
model.addUser(user);
view.updateTotalBooksBorrowed();
return "";
} catch (NumberFormatException e) {
return "Invalid number of books!";
} catch (NotALenderException e) {
return e.getMessage();
}
}
}
```
GUI class:
```
import javax.swing.*;
import java.awt.*;
public class GUI {
private JFrame frame = new JFrame("Library");
private Model model = new Library();
private ViewSimple viewSimple = new ViewSimple(model);
private ViewGetBook viewGetBook = new ViewGetBook(model);
private ViewMoreBook viewMoreBook = new ViewMoreBook(model);
private ViewCreate viewCreate = new ViewCreate(model);
public GUI() {
Container cp = frame.getContentPane();
cp.setLayout(new BorderLayout());
JTabbedPane tp = new JTabbedPane();
tp.addTab("Simple", viewSimple);
tp.addTab("Get book", viewGetBook);
tp.addTab("More book", viewMoreBook);
tp.addTab("Create", viewCreate);
cp.add(tp, BorderLayout.CENTER);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// Remove test users from model
model.removeUser(new Lender("John", 2));
model.removeUser(new Borrower("Mary", 3));
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new GUI();
}
});
}
}
```
阅读全文