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:

时间: 2023-11-27 08:49:14 浏览: 46
Sure, here's the updated code for the CLI class: ``` import java.util.Scanner; public class CLI { private Library library; private Scanner scanner; public CLI(Library library) { this.library = library; this.scanner = new Scanner(System.in); } public void run() { while (true) { System.out.print("Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): "); int action = readPosInt(); switch (action) { case 1: System.out.println("Total number of books: " + library.getTotal()); break; case 2: System.out.print("Enter the number of books to add: "); int add = readPosInt(); library.add(add); System.out.println("Total number of books: " + library.getTotal()); break; case 3: System.out.print("Enter the name of the user: "); String borrower = scanner.nextLine(); try { int borrowed = library.get(borrower); System.out.println(borrower + " borrows " + borrowed + " book(s)."); } catch (UnknownUserException e) { System.out.println(e.getMessage()); } break; case 4: System.out.print("Enter the name of the user: "); String lender = scanner.nextLine(); System.out.print("Enter the number of books: "); int books = readPosInt(); try { library.more(lender, books); System.out.println(lender + " now lends " + library.get(lender) + " book(s)."); } catch (UnknownUserException e) { System.out.println(e.getMessage()); } break; case 5: System.out.print("Enter the name of the user: "); String borrower2 = scanner.nextLine(); System.out.print("Enter the number of books: "); int books2 = readPosInt(); try { library.less(borrower2, books2); System.out.println(borrower2 + " now borrows " + library.get(borrower2) + " book(s)."); } catch (UnknownUserException e) { System.out.println(e.getMessage()); } catch (NotALenderException e) { System.out.println("BUG! This must never happen!"); System.exit(1); } break; case 6: System.out.println("Goodbye!"); return; default: System.out.println("Invalid action."); break; } } } private int readPosInt() { while (true) { try { int num = Integer.parseInt(scanner.nextLine()); if (num < 0) { throw new NumberFormatException(); } return num; } catch (NumberFormatException e) { System.out.print("Positive integers only! Enter a number again: "); } } } } ``` I added a new case for action 4, where we prompt the user for the name of the user and the number of books to add. Then we call the `more` method on the library object with those parameters, catch any `UnknownUserException` exceptions that may be thrown, and print the appropriate message. I also added an additional catch block for the `NotALenderException` that may be thrown in the `less` method. In this case, we print an error message and immediately terminate the program using `System.exit(1)`. Let me know if you have any questions or concerns!

相关推荐

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

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

最新推荐

recommend-type

mysql Sort aborted: Out of sort memory, consider increasing server sort buffer size的解决方法

主要介绍了mysql Sort aborted: Out of sort memory, consider increasing server sort buffer size的解决方法,需要的朋友可以参考下
recommend-type

SecondactivityMainActivity.java

SecondactivityMainActivity.java
recommend-type

mmexport1719207093976.jpg

mmexport1719207093976.jpg
recommend-type

百度贴吧 安装包 全球最大的中文社区互动平台

百度贴吧安装包的相关信息如下: 应用介绍: 百度贴吧APP是全球最大中文社区互动平台,多样化的互动模式及板块都可自由进入,与吧友们一起互动交流。 它是一个以兴趣主题聚合志同道合者的互动平台,用户可以在这里畅所欲言,聊游戏、聊数码、聊动漫、聊收藏、聊手艺、聊运动等,满足各类用户的兴趣需求。 功能特点: 好内容,超懂你:提供专属内容推荐,根据用户兴趣推送相关贴吧和话题。 找同好,聊兴趣:用户可以轻松找到志同道合的吧友,进行深入的交流和讨论。 追热点,玩热梗:快速获取前沿热梗,与吧友一起分享讨论。 找游戏,看榜单:首页游戏中心提供丰富游戏资源和榜单,满足游戏玩家的需求。 主要功能: 兴趣频道:分类展现,精彩内容沉浸体验更过瘾。 话题热榜:热点榜单一手掌握,方便用户了解最新动态。 吧友评价:真实评价一目了然,帮助用户了解贴吧和吧友的情况。 贴吧好物:商品橱窗,吧友推荐一键购买更便捷,为用户提供购物便利。 更新日志: 百度贴吧APP不断更新优化,解决已知问题,提升用户体验。例如,增加了会员装扮升级、小尾巴、头像框等个性化设置,新增了虚拟形象、吧友互助等有趣玩法。
recommend-type

2024年东南亚3-甲氧基丙胺(MOPA)市场深度研究及预测报告.pdf

东南亚位于我国倡导推进的“一带一路”海陆交汇地带,作为当今全球发展最为迅速的地区之一,近年来区域内生产总值实现了显著且稳定的增长。根据东盟主要经济体公布的最新数据,印度尼西亚2023年国内生产总值(GDP)增长5.05%;越南2023年经济增长5.05%;马来西亚2023年经济增速为3.7%;泰国2023年经济增长1.9%;新加坡2023年经济增长1.1%;柬埔寨2023年经济增速预计为5.6%。 东盟国家在“一带一路”沿线国家中的总体GDP经济规模、贸易总额与国外直接投资均为最大,因此有着举足轻重的地位和作用。当前,东盟与中国已互相成为双方最大的交易伙伴。中国-东盟贸易总额已从2013年的443亿元增长至 2023年合计超逾6.4万亿元,占中国外贸总值的15.4%。在过去20余年中,东盟国家不断在全球多变的格局里面临挑战并寻求机遇。2023东盟国家主要经济体受到国内消费、国外投资、货币政策、旅游业复苏、和大宗商品出口价企稳等方面的提振,经济显现出稳步增长态势和强韧性的潜能。 本调研报告旨在深度挖掘东南亚市场的增长潜力与发展机会,分析东南亚市场竞争态势、销售模式、客户偏好、整体市场营商环境,为国内企业出海开展业务提供客观参考意见。 本文核心内容: 市场空间:全球行业市场空间、东南亚市场发展空间。 竞争态势:全球份额,东南亚市场企业份额。 销售模式:东南亚市场销售模式、本地代理商 客户情况:东南亚本地客户及偏好分析 营商环境:东南亚营商环境分析 本文纳入的企业包括国外及印尼本土企业,以及相关上下游企业等,部分名单 QYResearch是全球知名的大型咨询公司,行业涵盖各高科技行业产业链细分市场,横跨如半导体产业链(半导体设备及零部件、半导体材料、集成电路、制造、封测、分立器件、传感器、光电器件)、光伏产业链(设备、硅料/硅片、电池片、组件、辅料支架、逆变器、电站终端)、新能源汽车产业链(动力电池及材料、电驱电控、汽车半导体/电子、整车、充电桩)、通信产业链(通信系统设备、终端设备、电子元器件、射频前端、光模块、4G/5G/6G、宽带、IoT、数字经济、AI)、先进材料产业链(金属材料、高分子材料、陶瓷材料、纳米材料等)、机械制造产业链(数控机床、工程机械、电气机械、3C自动化、工业机器人、激光、工控、无人机)、食品药品、医疗器械、农业等。邮箱:market@qyresearch.com
recommend-type

BSC绩效考核指标汇总 (2).docx

BSC(Balanced Scorecard,平衡计分卡)是一种战略绩效管理系统,它将企业的绩效评估从传统的财务维度扩展到非财务领域,以提供更全面、深入的业绩衡量。在提供的文档中,BSC绩效考核指标主要分为两大类:财务类和客户类。 1. 财务类指标: - 部门费用的实际与预算比较:如项目研究开发费用、课题费用、招聘费用、培训费用和新产品研发费用,均通过实际支出与计划预算的百分比来衡量,这反映了部门在成本控制上的效率。 - 经营利润指标:如承保利润、赔付率和理赔统计,这些涉及保险公司的核心盈利能力和风险管理水平。 - 人力成本和保费收益:如人力成本与计划的比例,以及标准保费、附加佣金、续期推动费用等与预算的对比,评估业务运营和盈利能力。 - 财务效率:包括管理费用、销售费用和投资回报率,如净投资收益率、销售目标达成率等,反映公司的财务健康状况和经营效率。 2. 客户类指标: - 客户满意度:通过包装水平客户满意度调研,了解产品和服务的质量和客户体验。 - 市场表现:通过市场销售月报和市场份额,衡量公司在市场中的竞争地位和销售业绩。 - 服务指标:如新契约标保完成度、续保率和出租率,体现客户服务质量和客户忠诚度。 - 品牌和市场知名度:通过问卷调查、公众媒体反馈和总公司级评价来评估品牌影响力和市场认知度。 BSC绩效考核指标旨在确保企业的战略目标与财务和非财务目标的平衡,通过量化这些关键指标,帮助管理层做出决策,优化资源配置,并驱动组织的整体业绩提升。同时,这份指标汇总文档强调了财务稳健性和客户满意度的重要性,体现了现代企业对多维度绩效管理的重视。
recommend-type

管理建模和仿真的文件

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

【进阶】Flask中的会话与用户管理

![python网络编程合集](https://media.geeksforgeeks.org/wp-content/uploads/20201021201514/pythonrequests.PNG) # 2.1 用户注册和登录 ### 2.1.1 用户注册表单的设计和验证 用户注册表单是用户创建帐户的第一步,因此至关重要。它应该简单易用,同时收集必要的用户信息。 * **字段设计:**表单应包含必要的字段,如用户名、电子邮件和密码。 * **验证:**表单应验证字段的格式和有效性,例如电子邮件地址的格式和密码的强度。 * **错误处理:**表单应优雅地处理验证错误,并提供清晰的错误消
recommend-type

卷积神经网络实现手势识别程序

卷积神经网络(Convolutional Neural Network, CNN)在手势识别中是一种非常有效的机器学习模型。CNN特别适用于处理图像数据,因为它能够自动提取和学习局部特征,这对于像手势这样的空间模式识别非常重要。以下是使用CNN实现手势识别的基本步骤: 1. **输入数据准备**:首先,你需要收集或获取一组带有标签的手势图像,作为训练和测试数据集。 2. **数据预处理**:对图像进行标准化、裁剪、大小调整等操作,以便于网络输入。 3. **卷积层(Convolutional Layer)**:这是CNN的核心部分,通过一系列可学习的滤波器(卷积核)对输入图像进行卷积,以
recommend-type

BSC资料.pdf

"BSC资料.pdf" 战略地图是一种战略管理工具,它帮助企业将战略目标可视化,确保所有部门和员工的工作都与公司的整体战略方向保持一致。战略地图的核心内容包括四个相互关联的视角:财务、客户、内部流程和学习与成长。 1. **财务视角**:这是战略地图的最终目标,通常表现为股东价值的提升。例如,股东期望五年后的销售收入达到五亿元,而目前只有一亿元,那么四亿元的差距就是企业的总体目标。 2. **客户视角**:为了实现财务目标,需要明确客户价值主张。企业可以通过提供最低总成本、产品创新、全面解决方案或系统锁定等方式吸引和保留客户,以实现销售额的增长。 3. **内部流程视角**:确定关键流程以支持客户价值主张和财务目标的实现。主要流程可能包括运营管理、客户管理、创新和社会责任等,每个流程都需要有明确的短期、中期和长期目标。 4. **学习与成长视角**:评估和提升企业的人力资本、信息资本和组织资本,确保这些无形资产能够支持内部流程的优化和战略目标的达成。 绘制战略地图的六个步骤: 1. **确定股东价值差距**:识别与股东期望之间的差距。 2. **调整客户价值主张**:分析客户并调整策略以满足他们的需求。 3. **设定价值提升时间表**:规划各阶段的目标以逐步缩小差距。 4. **确定战略主题**:识别关键内部流程并设定目标。 5. **提升战略准备度**:评估并提升无形资产的战略准备度。 6. **制定行动方案**:根据战略地图制定具体行动计划,分配资源和预算。 战略地图的有效性主要取决于两个要素: 1. **KPI的数量及分布比例**:一个有效的战略地图通常包含20个左右的指标,且在四个视角之间有均衡的分布,如财务20%,客户20%,内部流程40%。 2. **KPI的性质比例**:指标应涵盖财务、客户、内部流程和学习与成长等各个方面,以全面反映组织的绩效。 战略地图不仅帮助管理层清晰传达战略意图,也使员工能更好地理解自己的工作如何对公司整体目标产生贡献,从而提高执行力和组织协同性。