Also create a ControllerGetBook class that extends Controller .The getBook method takes the name of a user as argument. The getBook method of the controller then calls the getBook method of the library to get the number of books currently borrowed by that user. The getBook method of the controller then transforms the integer result of the getBook method of the library into a string and returns that string as result (to the view). If the getBook method of the library throws an UnknownUserException then the getBook 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 ViewGetBook view that uses a ControllerGetBook controller and the same model as before (not a new model!) Do not delete the previous view. Run your GUI and check that you can correctly use the new view to query the number of books borrowed by different users of your library (obviously your library must have some users in it to test this: see the last paragraph of Question 7). Also check that querying the number of books of an unknown user correctly shows an error message. 写出符合以上要求的java代码

时间: 2023-07-03 10:03:47 浏览: 54
下面是符合要求的Java代码: ```java // UnknownUserException.java public class UnknownUserException extends Exception { public UnknownUserException(String message) { super(message); } } // Library.java public class Library { private Map<String, Integer> borrowedBooks = new HashMap<>(); public void borrowBook(String user) { if (borrowedBooks.containsKey(user)) { borrowedBooks.put(user, borrowedBooks.get(user) + 1); } else { borrowedBooks.put(user, 1); } } public void returnBook(String user) throws UnknownUserException { if (!borrowedBooks.containsKey(user)) { throw new UnknownUserException("Unknown user: " + user); } else { int numBooks = borrowedBooks.get(user); if (numBooks == 1) { borrowedBooks.remove(user); } else { borrowedBooks.put(user, numBooks - 1); } } } public int getBook(String user) throws UnknownUserException { if (!borrowedBooks.containsKey(user)) { throw new UnknownUserException("Unknown user: " + user); } else { return borrowedBooks.get(user); } } } // ControllerGetBook.java public class ControllerGetBook extends Controller { private Library library; public ControllerGetBook(Library library) { this.library = library; } public String getBook(String user) { try { int numBooks = library.getBook(user); return Integer.toString(numBooks); } catch (UnknownUserException e) { return e.getMessage(); } } } // ViewGetBook.java public class ViewGetBook extends View { private ControllerGetBook controller; private JTextArea textArea; public ViewGetBook(Model model, ControllerGetBook controller) { super(model); this.controller = controller; textArea = new JTextArea(); add(new JScrollPane(textArea)); } public void update() { String user = ((ModelUser)getModel()).getUser(); String numBooks = controller.getBook(user); textArea.setText(numBooks); } } // GUI.java (modified run method) public class GUI { private ModelUser modelUser; private ViewBorrowBook viewBorrowBook; private ViewReturnBook viewReturnBook; private ViewGetBook viewGetBook; public void run() { Library library = new Library(); modelUser = new ModelUser(); ControllerBorrowBook controllerBorrowBook = new ControllerBorrowBook(library); viewBorrowBook = new ViewBorrowBook(modelUser, controllerBorrowBook); modelUser = new ModelUser(); ControllerReturnBook controllerReturnBook = new ControllerReturnBook(library); viewReturnBook = new ViewReturnBook(modelUser, controllerReturnBook); modelUser = new ModelUser(); ControllerGetBook controllerGetBook = new ControllerGetBook(library); viewGetBook = new ViewGetBook(modelUser, controllerGetBook); JFrame frame = new JFrame("Library"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(new GridLayout(3, 1)); frame.add(viewBorrowBook); frame.add(viewReturnBook); frame.add(viewGetBook); frame.pack(); frame.setVisible(true); } } ```

相关推荐

最新推荐

recommend-type

埃森哲制药企业数字化转型项目顶层规划方案glq.pptx

埃森哲制药企业数字化转型项目顶层规划方案glq.pptx
recommend-type

华为OD机试D卷 - 机场航班调度程序 - 免费看解析和代码.html

私信博主免费获取真题解析以及代码
recommend-type

基于FPGA读取设计的心电图代码源码+全部资料齐全.zip

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

基于Hadoop平台的分布式搜索引擎的设计与实现+部署文档+全部资料 高分项目.zip

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

华为OD机试D卷 - 数的分解 - 免费看解析和代码.html

私信博主免费获取真题解析以及代码
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

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

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