Add a Library class with the following UML specification: +-------------------------------------------+ | Library | +-------------------------------------------+ | - name: String | | - users: ArrayList | +-------------------------------------------+ | + Library(String name) | | + addUser(IUser user): void | | + totalBorrowedBooks(): int | | + getBook(String name): int | | + moreBook(String name, int number): void | | + testLibrary(): void | +-------------------------------------------+ When a library is created, it has an arraylist of users (IUser) but the arraylist is empty (the arraylist does not contain any user). The addUser method takes a user (IUser) as argument and adds the user to the arraylist of users for the library. The totalBorrowedBooks method returns as result the total number of books borrowed by all users of the library (the result can be either positive or negative). The getBook method takes as argument the name of a user and returns as result the number of books currently borrowed by the user. If the library does not have a user with the given name, then the getBook method must throw an UnknownUserException with the message "User XXX unknown.", where XXX is replaced with the name of the user. Do not worry about multiple users having the same name. You can assume all user names are unique in the arraylist. The moreBook method takes as argument the name of a user and a number of books and changes the number of books currently borrowed by that user. If the library does not have a user with the given name, then the moreBook method must throw an UnknownUserException with the message "User XXX unknown.", where XXX is replaced with the name of the user. Do not worry about multiple users having the same name. Note: the moreBook method does not catch any exception, it only throws exceptions. Hint: use the equals method to compare strings, not the == operator which only works with constant strings. 写java文件
时间: 2023-08-15 13:11:14 浏览: 83
下面是实现上述UML规范的Java代码:
```java
import java.util.ArrayList;
public class Library {
private String name;
private ArrayList<IUser> users;
public Library(String name) {
this.name = name;
users = new ArrayList<IUser>();
}
public void addUser(IUser user) {
users.add(user);
}
public int totalBorrowedBooks() {
int total = 0;
for (IUser user : users) {
total += user.getBorrowedBooks();
}
return total;
}
public int getBook(String name) throws UnknownUserException {
for (IUser user : users) {
if (user.getName().equals(name)) {
return user.getBorrowedBooks();
}
}
throw new UnknownUserException("User " + name + " unknown.");
}
public void moreBook(String name, int number) throws UnknownUserException {
for (IUser user : users) {
if (user.getName().equals(name)) {
user.setBorrowedBooks(user.getBorrowedBooks() + number);
return;
}
}
throw new UnknownUserException("User " + name + " unknown.");
}
public void testLibrary() {
IUser user1 = new User("Alice");
IUser user2 = new User("Bob");
addUser(user1);
addUser(user2);
try {
System.out.println(getBook("Alice")); // should print 0
moreBook("Bob", 2);
System.out.println(getBook("Bob")); // should print 2
moreBook("Charlie", 1); // should throw an UnknownUserException
} catch (UnknownUserException e) {
System.out.println(e.getMessage());
}
System.out.println(totalBorrowedBooks()); // should print 2
}
}
```
在上面的代码中,我们定义了一个`Library`类,用于管理图书馆的用户和借阅信息。该类有一个私有成员变量`name`,表示图书馆的名称,还有一个私有成员变量`users`,表示图书馆的用户列表。我们提供了一个构造方法`Library(String name)`,用于创建一个新的图书馆对象。
我们还提供了以下公共方法:
- `void addUser(IUser user)`:将一个新用户添加到图书馆的用户列表中。
- `int totalBorrowedBooks()`:返回当前所有用户已借阅的书籍总数。
- `int getBook(String name)`:返回指定用户当前已借阅的书籍数。如果图书馆中不存在该用户,则抛出`UnknownUserException`异常。
- `void moreBook(String name, int number)`:将指定用户已借阅的书籍数增加`number`。如果图书馆中不存在该用户,则抛出`UnknownUserException`异常。
我们还提供了一个`testLibrary`方法,用于测试`Library`类中的各个方法是否正常工作。
注意,在`getBook`和`moreBook`方法中,我们使用了`equals`方法来比较字符串是否相等,而不是使用`==`运算符。这是因为`==`运算符只能用于比较常量字符串,而不能用于比较变量中存储的字符串。
阅读全文