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 22:11:14 浏览: 38
下面是实现上述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`方法来比较字符串是否相等,而不是使用`==`运算符。这是因为`==`运算符只能用于比较常量字符串,而不能用于比较变量中存储的字符串。

相关推荐

continue to use Java language, Add a Library class with the following UML specification: +-------------------------------------------+ | Library | +-------------------------------------------+ | - name: String | | - users: ArrayList<IUser> | +-------------------------------------------+ | + 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.

最新推荐

recommend-type

IEEE 8023cg-2019.pdf

This amendment to IEEE Std 802.3-2018 specifies additions and appropriate modifications to add 10 Mb/s Physical Layer (PHY) specifications and management parameters for operation, and associated ...
recommend-type

Nginx配置跨域请求Access-Control-Allow-Origin * 详解

主要给大家介绍了关于Nginx配置跨域请求Access-Control-Allow-Origin * 的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Nginx具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
recommend-type

5种WIN-XP解除开机密码方法

WIN-XP解除开机密码方法. 在开机时,按下F8进入”带命令提示符的安全”模式;输入”NET USER+用户名+123456/ADD”可把某用户的密码强行设置为”123456″
recommend-type

IEEE 802.3ch-2020 /10GBase T1标准

Specify additions to and appropriate modifications of IEEE Std 802.3 to add greater than 1 Gb/s Physical Layer (PHY) specifications and management parameters for media and operating conditions for ...
recommend-type

函数版-简单的学生信息管理系统(python实现)

函数版-简单的学生信息管理系统(python实现)功能如下开始部分增加学生信息修改学生信息删除学生信息...def add(): dict1 = {} sName = input("请输入学生姓名:") sAge = eval&#40;input("请输入学生年龄:"&#41;)
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

解释minorization-maximization (MM) algorithm,并给出matlab代码编写的例子

Minorization-maximization (MM) algorithm是一种常用的优化算法,用于求解非凸问题或含有约束的优化问题。该算法的基本思想是通过构造一个凸下界函数来逼近原问题,然后通过求解凸下界函数的最优解来逼近原问题的最优解。具体步骤如下: 1. 初始化参数 $\theta_0$,设 $k=0$; 2. 构造一个凸下界函数 $Q(\theta|\theta_k)$,使其满足 $Q(\theta_k|\theta_k)=f(\theta_k)$; 3. 求解 $Q(\theta|\theta_k)$ 的最优值 $\theta_{k+1}=\arg\min_\theta Q(
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。