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).Javacase实现

时间: 2023-06-27 10:04:33 浏览: 101
以下是一个可能的Javacase实现,用于处理上述情况: ```java public class CLI { public static void main(String[] args) { try { Scanner scanner = new Scanner(System.in); System.out.println("Welcome to the library!"); System.out.print("Are you a lender or a borrower? "); String input = scanner.nextLine().trim().toLowerCase(); if (input.equals("lender")) { System.out.print("Enter the number of books you want to lend: "); int numBooks = readPosInt(scanner); Lender lender = new Lender(numBooks); System.out.println("You have successfully registered as a lender."); // do something with lender object } else if (input.equals("borrower")) { System.out.print("Enter the number of books you want to borrow: "); int numBooks = readPosInt(scanner); Borrower borrower = new Borrower(numBooks); System.out.println("You have successfully registered as a borrower."); // do something with borrower object } else { System.out.println("Invalid input. Please try again."); } } catch (NotALenderException e) { System.out.println("BUG! This must never happen!"); System.exit(1); } } public static int readPosInt(Scanner scanner) throws NotALenderException { int numBooks = scanner.nextInt(); if (numBooks < 0) { throw new NotALenderException("Number of books cannot be negative."); } return numBooks; } } ``` 该实现包括一个名为`readPosInt`的辅助方法,该方法使用`Scanner`对象读取正整数,并在输入为负数时引发`NotALenderException`。在`main`方法中,我们捕获该异常并立即终止程序。
阅读全文

相关推荐

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:

continue to use Java language, 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. The moreBook method of the Borrower class increases the number of books borrowed by the borrower by the number of books given as argument to the method (so the books borrowed by the borrower becomes more positive!) For example, if a borrower currently borrows 10 books and moreBook(2) is called then the borrower borrows 12 books. It is fine for the moreBook method to be given a negative value as argument, which means the borrower then just returned some books. For example, if a borrower currently borrows 10 books and moreBook(-2) is called then the borrower borrows 8 books. However, a borrower cannot lend books, so the number of books borrowed by the borrower must always be positive or zero, never negative. If the argument given to the moreBook method is too negative and would change the book variable into a negative value, then the number of books borrowed by the borrower must not change and the moreBook method must throw a NotALenderException with the message “A borrower cannot lend XXX book(s).”, where XXX is replaced with the result of -(book + number). For example, if a borrower currently borrows 10 books and moreBook(-12) is called then the borrower still borrows 10 books and the method throws a NotALenderException with the message “A borrower cannot lend 2 book(s).”. Note: to simplify the project, do not worry about the setBook method. Change other classes and interfaces as necessary

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

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

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

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

图像去雾基于基于Matlab界面的(多方法对比,PSNR,信息熵,GUI界面).rar

MATLAB设计
recommend-type

c语言打字母游戏源码.zip

c语言打字母游戏源码
recommend-type

c语言做的一个任务管理器.zip

c语言做的一个任务管理器
recommend-type

JetBra-2021.1.x-重置.mp4.zip

JetBra-2021.1.x-重置.mp4.zip
recommend-type

小学班主任与家长沟通现状及改进策略研究

内容概要:本文围绕小学班主任与家长沟通的现状进行了详尽分析,揭示了沟通方式不当、频率低、内容片面及理念不一致等问题,并基于访谈、文献研究及案例分析,提出了多元化的沟通方式、丰富沟通内容、讲究沟通艺术、转变家长观念和完善制度等多项策略,旨在提高家校合作的效能。 适合人群:从事小学教育教学的班主任、教师以及对家校合作感兴趣的教育工作者。 使用场景及目标:①通过本文提出的多种策略,改善小学班主任与家长之间的沟通;②促进家校互动,助力学生健康成长和发展;③推动教育领域的研究与发展。 阅读建议:本文详细阐述了沟通现状及具体问题,适合系统阅读。读者可根据实际情况,挑选适用于自身的沟通策略实施,并结合实例进行反思与改进。
recommend-type

易语言例程:用易核心支持库打造功能丰富的IE浏览框

资源摘要信息:"易语言-易核心支持库实现功能完善的IE浏览框" 易语言是一种简单易学的编程语言,主要面向中文用户。它提供了大量的库和组件,使得开发者能够快速开发各种应用程序。在易语言中,通过调用易核心支持库,可以实现功能完善的IE浏览框。IE浏览框,顾名思义,就是能够在一个应用程序窗口内嵌入一个Internet Explorer浏览器控件,从而实现网页浏览的功能。 易核心支持库是易语言中的一个重要组件,它提供了对IE浏览器核心的调用接口,使得开发者能够在易语言环境下使用IE浏览器的功能。通过这种方式,开发者可以创建一个具有完整功能的IE浏览器实例,它不仅能够显示网页,还能够支持各种浏览器操作,如前进、后退、刷新、停止等,并且还能够响应各种事件,如页面加载完成、链接点击等。 在易语言中实现IE浏览框,通常需要以下几个步骤: 1. 引入易核心支持库:首先需要在易语言的开发环境中引入易核心支持库,这样才能在程序中使用库提供的功能。 2. 创建浏览器控件:使用易核心支持库提供的API,创建一个浏览器控件实例。在这个过程中,可以设置控件的初始大小、位置等属性。 3. 加载网页:将浏览器控件与一个网页地址关联起来,即可在控件中加载显示网页内容。 4. 控制浏览器行为:通过易核心支持库提供的接口,可以控制浏览器的行为,如前进、后退、刷新页面等。同时,也可以响应浏览器事件,实现自定义的交互逻辑。 5. 调试和优化:在开发完成后,需要对IE浏览框进行调试,确保其在不同的操作和网页内容下均能够正常工作。对于性能和兼容性的问题需要进行相应的优化处理。 易语言的易核心支持库使得在易语言环境下实现IE浏览框变得非常方便,它极大地降低了开发难度,并且提高了开发效率。由于易语言的易用性,即使是初学者也能够在短时间内学会如何创建和操作IE浏览框,实现网页浏览的功能。 需要注意的是,由于IE浏览器已经逐渐被微软边缘浏览器(Microsoft Edge)所替代,使用IE核心的技术未来可能面临兼容性和安全性的挑战。因此,在实际开发中,开发者应考虑到这一点,并根据需求选择合适的浏览器控件实现技术。 此外,易语言虽然简化了编程过程,但其在功能上可能不如主流的编程语言(如C++, Java等)强大,且社区和技术支持相比其他语言可能较为有限,这些都是在选择易语言作为开发工具时需要考虑的因素。 文件名列表中的“IE类”可能是指包含实现IE浏览框功能的类库或者示例代码。在易语言中,类库是一组封装好的代码模块,其中包含了各种功能的实现。通过在易语言项目中引用这些类库,开发者可以简化开发过程,快速实现特定功能。而示例代码则为开发者提供了具体的实现参考,帮助理解和学习如何使用易核心支持库来创建IE浏览框。
recommend-type

管理建模和仿真的文件

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

STM32F407ZG引脚功能深度剖析:掌握引脚分布与配置的秘密(全面解读)

![STM32F407ZG引脚功能深度剖析:掌握引脚分布与配置的秘密(全面解读)](https://tapit.vn/wp-content/uploads/2019/01/cubemx-peripheral-1024x545.png) # 摘要 本文全面介绍了STM32F407ZG微控制器的引脚特性、功能、配置和应用。首先概述了该芯片的引脚布局,然后详细探讨了标准外设、高级控制以及特殊功能引脚的不同配置和使用方法。在此基础上,文章深入分析了引脚模式配置、高级配置技巧,并提供了实际应用案例,如LED控制和串口通信。在设计方面,阐述了引脚布局策略、多层板设计及高密度引脚应用的解决方案。最后,介绍
recommend-type

给出文档中问题的答案代码

您提到的是需要编写MATLAB代码来实现文档中的实验任务。以下是根据文档内容编写的MATLAB代码示例: ```matlab % 上机2 实验代码 % 读取输入图像 inputImage = imread('your_face_image.jpg'); % 替换为您的图像文件路径 if size(inputImage, 1) < 1024 || size(inputImage, 2) < 1024 error('图像尺寸必须大于1024x1024'); end % 将彩色图像转换为灰度图像 grayImage = rgb2gray(inputImage); % 调整图像大小为5
recommend-type

Docker构建与运行Next.js应用的指南

资源摘要信息:"rivoltafilippo-next-main" 在探讨“rivoltafilippo-next-main”这一资源时,首先要从标题“rivoltafilippo-next”入手。这个标题可能是某一项目、代码库或应用的命名,结合描述中提到的Docker构建和运行命令,我们可以推断这是一个基于Docker的Node.js应用,特别是使用了Next.js框架的项目。Next.js是一个流行的React框架,用于服务器端渲染和静态网站生成。 描述部分提供了构建和运行基于Docker的Next.js应用的具体命令: 1. `docker build`命令用于创建一个新的Docker镜像。在构建镜像的过程中,开发者可以定义Dockerfile文件,该文件是一个文本文件,包含了创建Docker镜像所需的指令集。通过使用`-t`参数,用户可以为生成的镜像指定一个标签,这里的标签是`my-next-js-app`,意味着构建的镜像将被标记为`my-next-js-app`,方便后续的识别和引用。 2. `docker run`命令则用于运行一个Docker容器,即基于镜像启动一个实例。在这个命令中,`-p 3000:3000`参数指示Docker将容器内的3000端口映射到宿主机的3000端口,这样做通常是为了让宿主机能够访问容器内运行的应用。`my-next-js-app`是容器运行时使用的镜像名称,这个名称应该与构建时指定的标签一致。 最后,我们注意到资源包含了“TypeScript”这一标签,这表明项目可能使用了TypeScript语言。TypeScript是JavaScript的一个超集,它添加了静态类型定义的特性,能够帮助开发者更容易地维护和扩展代码,尤其是在大型项目中。 结合资源名称“rivoltafilippo-next-main”,我们可以推测这是项目的主目录或主仓库。通常情况下,开发者会将项目的源代码、配置文件、构建脚本等放在一个主要的目录中,这个目录通常命名为“main”或“src”等,以便于管理和维护。 综上所述,我们可以总结出以下几个重要的知识点: - Docker容器和镜像的概念以及它们之间的关系:Docker镜像是静态的只读模板,而Docker容器是从镜像实例化的动态运行环境。 - `docker build`命令的使用方法和作用:这个命令用于创建新的Docker镜像,通常需要一个Dockerfile来指定构建的指令和环境。 - `docker run`命令的使用方法和作用:该命令用于根据镜像启动一个或多个容器实例,并可指定端口映射等运行参数。 - Next.js框架的特点:Next.js是一个支持服务器端渲染和静态网站生成的React框架,适合构建现代的Web应用。 - TypeScript的作用和优势:TypeScript是JavaScript的一个超集,它提供了静态类型检查等特性,有助于提高代码质量和可维护性。 - 项目资源命名习惯:通常项目会有一个主目录,用来存放项目的源代码和核心配置文件,以便于项目的版本控制和团队协作。 以上内容基于给定的信息进行了深入的分析,为理解该项目的构建、运行方式以及技术栈提供了基础。在实际开发中,开发者应当参考更详细的文档和指南,以更高效地管理和部署基于Docker和TypeScript的Next.js项目。