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代码

时间: 2024-01-20 11:02:18 浏览: 204
TXT

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.
阅读全文

相关推荐

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代码

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代码

Action 2: adding a new user to the library. When the user of the software specifies action 2, your program must add a new user to the library. To add a new user, your program needs to ask the user three things: the role of user (an integer read using readPosInt: the integer 1 represents lender, the integer 2 represents borrower, any other integer must result in an error message "Unknown user role!" being printed and the software going immediately back to the main menu), the name of the user (a string read using readLine), and the initial number of books that the user lends (for a lender) or borrows (for a borrower). You program must then create the correct user, add it to the library, and print an information message. The program then goes back to the menu. For example (where 2, 3, 2, 1, Anna, 5, 2, 2, Bob, and 10 are inputs from the user): Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 2 Type the user role (lender:1 borrower:2): 3 Unknown user role! Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 2 Type the user role (lender:1 borrower:2): 1 Enter the name of the user: Anna Enter the initial number of borrowed books: 5 Lender "Anna" lending 5 book(s) has been added. Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 2 Type the user role (lender:1 borrower:2): 2 Enter the name of the user: Bob Enter the initial number of borrowed books: 10 Borrower "Bob" borrowing 10 book(s) has been added. Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): Note that the readPosInt method prevents the initial number of books from being negative, so the constructor for the Borrower class will never throw a NotALenderException when you create a borrower object. 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);

use java language ,In this project you need to write a book lending system for a Library. The system has different roles for registered users. There are two types of user roles: borrower and lender. Write an IUser interface for library users, with the following UML specification: +----------------------------------+ | <<interface>> | | IUser | +----------------------------------+ | + getName(): String | | + getBook(): int | | + moreBook(int number): void | +----------------------------------+ and a User class that implements IUser and has the following UML specification: +-----------------------------------+ | User | +-----------------------------------+ | - name: String | | - book: int | +-----------------------------------+ | + User(String name, int book) | | + getName(): String | | + getBook(): int | | # setBook(int book): void | | + moreBook(int number): void | | + testUser(): void | +-----------------------------------+ The name instance variable indicates the user name. The book instance variable indicates the number of books borrowed by the user. The setBook method changes the number of books borrowed by the user. The setBook method is protected, not public. This means that only subclasses of the User class can use the setBook method. All the other classes in the system cannot use the setBook method, so they cannot change the number of books borrowed by a user. The purpose of the moreBook method is to increase the number of books borrowed or lent by the user (depending on what kind of user it is) by the number given as argument to the method. The moreBook method of the User class is abstract, since we do not know what kind of role the user is (a borrower borrows books from other users and a lender lend books to other users). Also add to your program a Test class to test your User class. public class Test { public static void main(String[] args) { User.testUser(); } }

帮我修改一下我的代码错误:帮我看看我这段代码有什么错误:int choice = readPosInt("Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): "); // Perform the selected action while (choice != 6) { switch (choice) { case 1: System.out.println("Total number of borrowed books: " + Library.totalBorrowedBooks(null)); break; case 2: System.out.print("Type the user role (lender:1 borrower:2): "); int role = readPosInt(null); if (role == 1) { System.out.print("Enter the name of the user: "); String name = readLine(name); System.out.print("Enter the initial number of lent books: "); int numBooks = readPosInt(null); Lender lender = new Lender(name, numBooks); Library.addUser(lender); System.out.println("Lender "" + name + "" lending " + numBooks + " book(s) has been added."); } else if (role == 2) { System.out.print("Enter the name of the user: "); String name = readLine(name); System.out.print("Enter the initial number of borrowed books: "); int numBooks = readPosInt(null); Borrower borrower = new Borrower(name, numBooks); Library.addUser(borrower); System.out.println("Borrower "" + name + "" borrowing " + numBooks + " book(s) has been added."); } else { System.out.println("Unknown user role!"); } break; case 3: System.out.print("Enter the name of the user: "); String username = input.nextLine(); try { int numBorrowed = Library.totalBorrowedBooks(username); System.out.println(username + " borrows " + numBorrowed + " book(s)."); } catch (UnknownUserException e) { System.out.println("User " + username + " unknown."); } break; case 4: try { System.out.print("Enter the name of the user: "); String name = input.nextLine(); System.out.print("Enter the number of books: "); int num = input.nextInt(); input.nextLine(); library.moreBook(username, role); } catch (UnknownUserException e) { System.out.println("User " + username + " unknown."); } break; case 5: System.out.print("Enter the name of the user: "); username = input.next(); System.out.print("Enter the number of books: "); int numBooks = input.nextInt(); library.moreBook(username, -numBooks); // simulate decreasing books break; case 6: System.out.println("Goodbye!"); System.exit(0); break; }

最新推荐

recommend-type

polylearn-0.1.dev0-cp35-cp35m-win32.whl.rar

python whl离线安装包 pip安装失败可以尝试使用whl离线安装包安装 第一步 下载whl文件,注意需要与python版本配套 python版本号、32位64位、arm或amd64均有区别 第二步 使用pip install XXXXX.whl 命令安装,如果whl路径不在cmd窗口当前目录下,需要带上路径 WHL文件是以Wheel格式保存的Python安装包, Wheel是Python发行版的标准内置包格式。 在本质上是一个压缩包,WHL文件中包含了Python安装的py文件和元数据,以及经过编译的pyd文件, 这样就使得它可以在不具备编译环境的条件下,安装适合自己python版本的库文件。 如果要查看WHL文件的内容,可以把.whl后缀名改成.zip,使用解压软件(如WinRAR、WinZIP)解压打开即可查看。 为什么会用到whl文件来安装python库文件呢? 在python的使用过程中,我们免不了要经常通过pip来安装自己所需要的包, 大部分的包基本都能正常安装,但是总会遇到有那么一些包因为各种各样的问题导致安装不了的。 这时我们就可以通过尝试去Python安装包大全中(whl包下载)下载whl包来安装解决问题。
recommend-type

基于Simulink的语音信号降噪与增强.docx

基于Simulink的语音信号降噪与增强.docx
recommend-type

java资源Java条形码生成库 Barcode4J

java资源Java条形码生成库 Barcode4J提取方式是百度网盘分享地址
recommend-type

pgmagick-0.7.5-cp27-cp27m-win32.whl.rar

python whl离线安装包 pip安装失败可以尝试使用whl离线安装包安装 第一步 下载whl文件,注意需要与python版本配套 python版本号、32位64位、arm或amd64均有区别 第二步 使用pip install XXXXX.whl 命令安装,如果whl路径不在cmd窗口当前目录下,需要带上路径 WHL文件是以Wheel格式保存的Python安装包, Wheel是Python发行版的标准内置包格式。 在本质上是一个压缩包,WHL文件中包含了Python安装的py文件和元数据,以及经过编译的pyd文件, 这样就使得它可以在不具备编译环境的条件下,安装适合自己python版本的库文件。 如果要查看WHL文件的内容,可以把.whl后缀名改成.zip,使用解压软件(如WinRAR、WinZIP)解压打开即可查看。 为什么会用到whl文件来安装python库文件呢? 在python的使用过程中,我们免不了要经常通过pip来安装自己所需要的包, 大部分的包基本都能正常安装,但是总会遇到有那么一些包因为各种各样的问题导致安装不了的。 这时我们就可以通过尝试去Python安装包大全中(whl包下载)下载whl包来安装解决问题。
recommend-type

pendulum-2.1.2-cp310-cp310-win32.whl.rar

python whl离线安装包 pip安装失败可以尝试使用whl离线安装包安装 第一步 下载whl文件,注意需要与python版本配套 python版本号、32位64位、arm或amd64均有区别 第二步 使用pip install XXXXX.whl 命令安装,如果whl路径不在cmd窗口当前目录下,需要带上路径 WHL文件是以Wheel格式保存的Python安装包, Wheel是Python发行版的标准内置包格式。 在本质上是一个压缩包,WHL文件中包含了Python安装的py文件和元数据,以及经过编译的pyd文件, 这样就使得它可以在不具备编译环境的条件下,安装适合自己python版本的库文件。 如果要查看WHL文件的内容,可以把.whl后缀名改成.zip,使用解压软件(如WinRAR、WinZIP)解压打开即可查看。 为什么会用到whl文件来安装python库文件呢? 在python的使用过程中,我们免不了要经常通过pip来安装自己所需要的包, 大部分的包基本都能正常安装,但是总会遇到有那么一些包因为各种各样的问题导致安装不了的。 这时我们就可以通过尝试去Python安装包大全中(whl包下载)下载whl包来安装解决问题。
recommend-type

SSM Java项目:StudentInfo 数据管理与可视化分析

资源摘要信息:"StudentInfo 2.zip文件是一个压缩包,包含了多种数据可视化和数据分析相关的文件和代码。根据描述,此压缩包中包含了实现人员信息管理系统的增删改查功能,以及生成饼图、柱状图、热词云图和进行Python情感分析的代码或脚本。项目使用了SSM框架,SSM是Spring、SpringMVC和MyBatis三个框架整合的简称,主要应用于Java语言开发的Web应用程序中。 ### 人员增删改查 人员增删改查是数据库操作中的基本功能,通常对应于CRUD(Create, Retrieve, Update, Delete)操作。具体到本项目中,这意味着实现了以下功能: - 增加(Create):可以向数据库中添加新的人员信息记录。 - 查询(Retrieve):可以检索数据库中的人员信息,可能包括基本的查找和复杂的条件搜索。 - 更新(Update):可以修改已存在的人员信息。 - 删除(Delete):可以从数据库中移除特定的人员信息。 实现这些功能通常需要编写相应的后端代码,比如使用Java语言编写服务接口,然后通过SSM框架与数据库进行交互。 ### 数据可视化 数据可视化部分包括了生成饼图、柱状图和热词云图的功能。这些图形工具可以直观地展示数据信息,帮助用户更好地理解和分析数据。具体来说: - 饼图:用于展示分类数据的比例关系,可以清晰地显示每类数据占总体数据的比例大小。 - 柱状图:用于比较不同类别的数值大小,适合用来展示时间序列数据或者不同组别之间的对比。 - 热词云图:通常用于文本数据中,通过字体大小表示关键词出现的频率,用以直观地展示文本中频繁出现的词汇。 这些图表的生成可能涉及到前端技术,如JavaScript图表库(例如ECharts、Highcharts等)配合后端数据处理实现。 ### Python情感分析 情感分析是自然语言处理(NLP)的一个重要应用,主要目的是判断文本的情感倾向,如正面、负面或中立。在这个项目中,Python情感分析可能涉及到以下几个步骤: - 文本数据的获取和预处理。 - 应用机器学习模型或深度学习模型对预处理后的文本进行分类。 - 输出情感分析的结果。 Python是实现情感分析的常用语言,因为有诸如NLTK、TextBlob、scikit-learn和TensorFlow等成熟的库和框架支持相关算法的实现。 ### IJ项目与readme文档 "IJ项目"可能是指IntelliJ IDEA项目,IntelliJ IDEA是Java开发者广泛使用的集成开发环境(IDE),支持SSM框架。readme文档通常包含项目的安装指南、运行步骤、功能描述、开发团队和联系方式等信息,是项目入门和理解项目结构的首要参考。 ### 总结 "StudentInfo 2.zip"是一个综合性的项目,涉及到后端开发、前端展示、数据分析及自然语言处理等多个技术领域。通过这个项目,可以学习到如何使用SSM框架进行Web应用开发、实现数据可视化和进行基于Python的情感分析。这对于想要掌握Java Web开发和数据处理能力的学习者来说是一个很好的实践机会。
recommend-type

管理建模和仿真的文件

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

负载均衡技术深入解析:确保高可用性的网络服务策略

![负载均衡技术深入解析:确保高可用性的网络服务策略](https://media.geeksforgeeks.org/wp-content/uploads/20240130183502/Source-IP-hash--(1).webp) # 1. 负载均衡技术概述 ## 1.1 负载均衡技术的重要性 在现代信息技术不断发展的今天,互联网应用的规模和服务的复杂性日益增长。因此,为了确保高性能、高可用性和扩展性,负载均衡技术变得至关重要。它能够有效地分配和管理网络或应用程序的流量,使得服务器和网络资源得以最优利用。 ## 1.2 负载均衡技术的基本概念 负载均衡是一种网络流量管理技术,旨
recommend-type

怎么解决头文件重复包含

解决头文件重复包含的问题主要有以下几个策略: 1. **包含 guards**:在头文件开头添加一种特殊的标识符(通常是宏),如 `#ifndef` 和 `#define` 对组合,检查某个特定宏是否已经定义过。如果没有定义,则包含内容,然后设置该宏。如果在同一文件内再次包含,由于宏已经存在,就不会再执行包含的内容,从而避免重复。 ```cpp #ifndef HEADER_NAME_H_ #define HEADER_NAME_H_ // 内容... #endif // HEADER_NAME_H_ ``` 2. **使用 extern 关键字**:对于非静态变量和函数,可以将它们
recommend-type

pyedgar:Python库简化EDGAR数据交互与文档下载

资源摘要信息:"pyedgar:用于与EDGAR交互的Python库" 知识点说明: 1. pyedgar库概述: pyedgar是一个Python编程语言下的开源库,专门用于与美国证券交易委员会(SEC)的电子数据获取、访问和检索(EDGAR)系统进行交互。通过该库,用户可以方便地下载和处理EDGAR系统中公开提供的财务报告和公司文件。 2. EDGAR系统介绍: EDGAR系统是一个自动化系统,它收集、处理、验证和发布美国证券交易委员会(SEC)要求的公司和其他机构提交的各种文件。EDGAR数据库包含了美国上市公司的详细财务报告,包括季度和年度报告、委托声明和其他相关文件。 3. pyedgar库的主要功能: 该库通过提供两个主要接口:文件(.py)和索引,实现了对EDGAR数据的基本操作。文件接口允许用户通过特定的标识符来下载和交互EDGAR表单。索引接口可能提供了对EDGAR数据库索引的访问,以便快速定位和获取数据。 4. pyedgar库的使用示例: 在描述中给出了一个简单的使用pyedgar库的例子,展示了如何通过Filing类与EDGAR表单进行交互。首先需要从pyedgar模块中导入Filing类,然后创建一个Filing实例,其中第一个参数(20)可能代表了提交年份的最后两位,第二个参数是一个特定的提交号码。创建实例后,可以打印实例来查看EDGAR接口的返回对象,通过打印实例的属性如'type',可以获取文件的具体类型(例如10-K),这代表了公司提交的年度报告。 5. Python语言的应用: pyedgar库的开发和应用表明了Python语言在数据分析、数据获取和自动化处理方面的强大能力。Python的简洁语法和丰富的第三方库使得开发者能够快速构建工具以处理复杂的数据任务。 6. 压缩包子文件信息: 文件名称列表中的“pyedgar-master”表明该库可能以压缩包的形式提供源代码和相关文件。文件列表中的“master”通常指代主分支或主版本,在软件开发中,主分支通常包含了最新的代码和功能。 7. 编程实践建议: 在使用pyedgar库之前,建议先阅读官方文档,了解其详细的安装、配置和使用指南。此外,进行编程实践时,应当注意遵守SEC的使用条款,确保只下载和使用公开提供的数据。 8. EDGAR数据的应用场景: EDGAR数据广泛应用于金融分析、市场研究、合规性检查、学术研究等领域。通过编程访问EDGAR数据可以让用户快速获取到一手的财务和公司运营信息,从而做出更加明智的决策。 9. Python库的维护和更新: 随着EDGAR数据库内容的持续更新和变化,pyedgar库也应定期进行维护和更新,以保证与EDGAR系统的接口兼容性。开发者社区对于这类开源项目的支持和贡献也非常重要。 10. 注意事项: 在使用pyedgar库下载和处理数据时,用户应当确保遵守相应的法律法规,尤其是关于数据版权和隐私方面的规定。此外,用户在处理敏感数据时,还需要考虑数据安全和隐私保护的问题。