use java to continue the question,Add a class Lender that extends User. The constructor of the Lender class takes as arguments a name and the number of books lent by the user. The Lender class does not have any instance variable. Warning: the constructor of the Lender class takes as argument the number of books lent by the lender, but the book instance variable of the User class indicates how many books the user borrowed! Lending books is the same as borrowing a negative number of books. The moreBook method of the Lender class increases the number of books lent by the lender user by the number given as argument to the method (so the books borrowed by the lender becomes more negative!) Make sure you test all the methods of your new Lender class using testLender method. Here are some tests for your new Lender class: public static void testLender() { Lender l = new Lender("Anna",5); System.out.println(l.getName() == "Anna"); System.out.println(l.getBook() == -5); l.setBook(-6); System.out.println(l.getBook() == -6); l.moreBook(2); System.out.println(l.getBook() == -8); l.moreBook(-9); System.out.println(l.getBook() == 1); } Also, do not forget to modify your Test class to test the Lender class.

时间: 2023-12-14 07:39:36 浏览: 91
Alright, here's the updated code with the Lender class: ``` public class User { private String name; private int book; public User(String name) { this.name = name; this.book = 0; } public String getName() { return name; } public int getBook() { return book; } public void setBook(int book) { this.book = book; } public void borrowBook(int num) { book += num; } } public class Lender extends User { public Lender(String name, int numBooksLent) { super(name); borrowBook(-numBooksLent); } public void moreBook(int num) { borrowBook(-num); } } public class Test { public static void main(String[] args) { User u = new User("Bob"); System.out.println(u.getName() == "Bob"); System.out.println(u.getBook() == 0); u.borrowBook(3); System.out.println(u.getBook() == 3); u.setBook(5); System.out.println(u.getBook() == 5); Lender l = new Lender("Anna", 5); System.out.println(l.getName() == "Anna"); System.out.println(l.getBook() == -5); l.setBook(-6); System.out.println(l.getBook() == -6); l.moreBook(2); System.out.println(l.getBook() == -8); l.moreBook(-9); System.out.println(l.getBook() == 1); } } ``` In the Lender class constructor, we call the parent constructor with the `name` argument, and then immediately call `borrowBook(-numBooksLent)` to lend out the specified number of books. The `moreBook` method simply calls `borrowBook` with a negative value to lend out more books. In the `Test` class, we now have tests for both the `User` and `Lender` classes.
阅读全文

相关推荐

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

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(); } }

Action 4: increasing the number of books of a given user. When the user of the software specifies action 4, your program must ask the user to type the name of a user, and a number of books, and the program then uses that number to increase the number of books lent or borrowed by the user. Then the program goes back to the main menu. For example: Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 3 Enter the name of the user: Anna Anna borrows -5 book(s). Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 4 Enter the name of the user: Anna Enter the number of books: 2 Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 3 Enter the name of the user: Anna Anna borrows -7 book(s). Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 3 Enter the name of the user: Bob Bob borrows 10 book(s). Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 4 Enter the name of the user: Bob Enter the number of books: 2 Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 3 Enter the name of the user: Bob Bob borrows 12 book(s). Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): If the name of the user is wrong, then an UnknownUserException exception will be thrown by the Library object. The code of the main method of your CLI class must catch this exception, print the error message from the exception object, and then it just goes back to printing the menu of actions (by just going back to the beginning of the while loop). For example (where 4, aaaa, and 2 are inputs from the user): Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 4 Enter the name of the user: aaaa Enter the number of books: 2 User aaaa unknown. Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): Note that, even if a consumer is a borrower, the readPosInt method prevents the typed number of books from being negative. This means a borrower will never throw a NotALenderException. 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). For example (where 3, Bob, 4, Bob, and -15 are inputs from the user): Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 3 Enter the name of the user: Bob Bob borrows 12 book(s). Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): 4 Enter the name of the user: Bob Enter the number of books: -15 Positive integers only! Enter the number of books:

帮我修改一下我的代码错误:帮我看看我这段代码有什么错误: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

基于C语言课程设计学生成绩管理系统、详细文档+全部资料+高分项目.zip

【资源说明】 基于C语言课程设计学生成绩管理系统、详细文档+全部资料+高分项目.zip 【备注】 1、该项目是个人高分项目源码,已获导师指导认可通过,答辩评审分达到95分 2、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 3、本项目适合计算机相关专业(人工智能、通信工程、自动化、电子信息、物联网等)的在校学生、老师或者企业员工下载使用,也可作为毕业设计、课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
recommend-type

基于springboot的简历系统源码(java毕业设计完整源码+LW).zip

项目均经过测试,可正常运行! 环境说明: 开发语言:java JDK版本:jdk1.8 框架:springboot 数据库:mysql 5.7/8 数据库工具:navicat 开发软件:eclipse/idea
recommend-type

“招聘智能化”:线上招聘问答系统的功能开发

互联网技术经过数十年的发展,已经积累了深厚的理论基础,并在实际应用中无处不在,极大地消除了地理信息的障碍,实现了全球即时通讯,极大地便利了人们的日常生活。因此,利用计算机技术设计的线上招聘问答系统,不仅在管理上更加系统化和操作性强,更重要的是在数据保存和使用上能够节省大量时间,使得系统变得非常高效和实用。 线上招聘问答系统采用MySQL作为数据管理工具,Java作为编码语言,以及SSM框架作为开发架构。系统主要实现了简历管理、论坛帖子管理、职位招聘管理、职位招聘留言管理、招聘岗位管理、所在行业管理以及求职意向管理等功能。 该系统的设计不仅方便了操作人员,而且合理性高,能有效避免误操作,确保数据在录入时就符合设计要求,从而最大限度地减少源头性输入错误,使数据更加可控和可靠,将出错率降至最低。
recommend-type

simulink实现标准IEEE33配电网系统,50HZ,将各节点数据统计起来输出到工作区,再matlab中跑出某时刻节点电压分布,适合用于观察某时刻节点电压变化情况 #特别是当用于接入双馈风机时

simulink实现标准IEEE33配电网系统,50HZ,将各节点数据统计起来输出到工作区,再matlab中跑出某时刻节点电压分布,适合用于观察某时刻节点电压变化情况。 #特别是当用于接入双馈风机时,用powergui无法进行潮流计算,通过此方法能过很好的解决此问题。 有参考文献。
recommend-type

给袋式真空包装机UG10全套技术资料100%好用.zip

给袋式真空包装机UG10全套技术资料100%好用.zip
recommend-type

WildFly 8.x中Apache Camel结合REST和Swagger的演示

资源摘要信息:"CamelEE7RestSwagger:Camel on EE 7 with REST and Swagger Demo" 在深入分析这个资源之前,我们需要先了解几个关键的技术组件,它们是Apache Camel、WildFly、Java DSL、REST服务和Swagger。下面是这些知识点的详细解析: 1. Apache Camel框架: Apache Camel是一个开源的集成框架,它允许开发者采用企业集成模式(Enterprise Integration Patterns,EIP)来实现不同的系统、应用程序和语言之间的无缝集成。Camel基于路由和转换机制,提供了各种组件以支持不同类型的传输和协议,包括HTTP、JMS、TCP/IP等。 2. WildFly应用服务器: WildFly(以前称为JBoss AS)是一款开源的Java应用服务器,由Red Hat开发。它支持最新的Java EE(企业版Java)规范,是Java企业应用开发中的关键组件之一。WildFly提供了一个全面的Java EE平台,用于部署和管理企业级应用程序。 3. Java DSL(领域特定语言): Java DSL是一种专门针对特定领域设计的语言,它是用Java编写的小型语言,可以在Camel中用来定义路由规则。DSL可以提供更简单、更直观的语法来表达复杂的集成逻辑,它使开发者能够以一种更接近业务逻辑的方式来编写集成代码。 4. REST服务: REST(Representational State Transfer)是一种软件架构风格,用于网络上客户端和服务器之间的通信。在RESTful架构中,网络上的每个资源都被唯一标识,并且可以使用标准的HTTP方法(如GET、POST、PUT、DELETE等)进行操作。RESTful服务因其轻量级、易于理解和使用的特性,已经成为Web服务设计的主流风格。 5. Swagger: Swagger是一个开源的框架,它提供了一种标准的方式来设计、构建、记录和使用RESTful Web服务。Swagger允许开发者描述API的结构,这样就可以自动生成文档、客户端库和服务器存根。通过Swagger,可以清晰地了解API提供的功能和如何使用这些API,从而提高API的可用性和开发效率。 结合以上知识点,CamelEE7RestSwagger这个资源演示了如何在WildFly应用服务器上使用Apache Camel创建RESTful服务,并通过Swagger来记录和展示API信息。整个过程涉及以下几个技术步骤: - 首先,需要在WildFly上设置和配置Camel环境,确保Camel能够运行并且可以作为路由引擎来使用。 - 其次,通过Java DSL编写Camel路由,定义如何处理来自客户端的HTTP请求,并根据请求的不同执行相应的业务逻辑。 - 接下来,使用Swagger来记录和描述创建的REST API。这包括定义API的路径、支持的操作、请求参数和响应格式等。 - 最后,通过Swagger提供的工具生成API文档和客户端代码,以及服务器端的存根代码,从而使得开发者可以更加便捷地理解和使用这些RESTful服务。 这个资源的实践演示对于想要学习如何在Java EE平台上使用Camel集成框架,并且希望提供和记录REST服务的开发者来说是非常有价值的。通过这种方式,开发者可以更加快速和简单地创建和管理Web服务,同时也增强了API的可访问性和可维护性。
recommend-type

管理建模和仿真的文件

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

【声子晶体模拟全能指南】:20年经验技术大佬带你从入门到精通

![【声子晶体模拟全能指南】:20年经验技术大佬带你从入门到精通](https://docs.lammps.org/_images/lammps-gui-main.png) # 摘要 声子晶体作为一种具有周期性结构的材料,在声学隐身、微波和红外领域具有广泛的应用潜力。本文从基础理论出发,深入探讨了声子晶体的概念、物理模型和声子带结构的理论解析,同时介绍了声子晶体的数值模拟方法,包括有限元方法(FEM)、离散元方法(DEM)和分子动力学(MD)。本文还提供了一套完整的声子晶体模拟实践指南,涵盖了模拟前的准备工作、详细的模拟步骤以及结果验证和案例分析。此外,文章探讨了声子晶体模拟的高级技巧和拓展
recommend-type

2024-07-27怎么用python转换成农历日期

在Python中,可以使用`lunarcalendar`库来将公历日期转换为农历日期。首先,你需要安装这个库,可以通过pip命令进行安装: ```bash pip install lunarcalendar ``` 安装完成后,你可以使用以下代码将公历日期转换为农历日期: ```python from lunarcalendar import Converter, Solar, Lunar, DateNotExist # 创建一个公历日期对象 solar_date = Solar(2024, 7, 27) # 将公历日期转换为农历日期 try: lunar_date = Co
recommend-type

FDFS客户端Python库1.2.6版本发布

资源摘要信息:"FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括文件存储、文件同步、文件访问等,适用于大规模文件存储和高并发访问场景。FastDFS为互联网应用量身定制,充分考虑了冗余备份、负载均衡、线性扩容等机制,保证系统的高可用性和扩展性。 FastDFS 架构包含两个主要的角色:Tracker Server 和 Storage Server。Tracker Server 作用是负载均衡和调度,它接受客户端的请求,为客户端提供文件访问的路径。Storage Server 作用是文件存储,一个 Storage Server 中可以有多个存储路径,文件可以存储在不同的路径上。FastDFS 通过 Tracker Server 和 Storage Server 的配合,可以完成文件上传、下载、删除等操作。 Python 客户端库 fdfs-client-py 是为了解决 FastDFS 文件系统在 Python 环境下的使用。fdfs-client-py 使用了 Thrift 协议,提供了文件上传、下载、删除、查询等接口,使得开发者可以更容易地利用 FastDFS 文件系统进行开发。fdfs-client-py 通常作为 Python 应用程序的一个依赖包进行安装。 针对提供的压缩包文件名 fdfs-client-py-master,这很可能是一个开源项目库的名称。根据文件名和标签“fdfs”,我们可以推测该压缩包包含的是 FastDFS 的 Python 客户端库的源代码文件。这些文件可以用于构建、修改以及扩展 fdfs-client-py 功能以满足特定需求。 由于“标题”和“描述”均与“fdfs-client-py-master1.2.6.zip”有关,没有提供其它具体的信息,因此无法从标题和描述中提取更多的知识点。而压缩包文件名称列表中只有一个文件“fdfs-client-py-master”,这表明我们目前讨论的资源摘要信息是基于对 FastDFS 的 Python 客户端库的一般性了解,而非基于具体文件内容的分析。 根据标签“fdfs”,我们可以深入探讨 FastDFS 相关的概念和技术细节,例如: - FastDFS 的分布式架构设计 - 文件上传下载机制 - 文件同步机制 - 元数据管理 - Tracker Server 的工作原理 - Storage Server 的工作原理 - 容错和数据恢复机制 - 系统的扩展性和弹性伸缩 在实际使用中,开发者可以通过 fdfs-client-py 库来与 FastDFS 文件系统进行交互,利用其提供的 API 接口实现文件的存储、管理等功能,从而开发出高效、可靠的文件处理应用。开发者可以根据项目的实际需求,选择合适的 FastDFS 版本,并根据官方文档进行安装、配置及优化,确保系统稳定运行。 总的来说,fdfs-client-py 是 FastDFS 文件系统与 Python 应用之间的一座桥梁,它使得开发者能够更加方便地将 FastDFS 集成到基于 Python 开发的应用中,发挥出 FastDFS 在文件管理方面的优势。"