帮我看看我的代码有什么错误:public static void moreBook(String name, int number) throws NotALenderException, UnknownUserException { for (User user : users) { if (user.getName().equals(name)) { if (user instanceof Lender) { ((Lender) user).moreBook(number); } else if (user instanceof Borrower) { ((Borrower) user).moreBook(number); } notifyListeners(); return; } } throw new UnknownUserException("User " + name + " unknown."); }

时间: 2023-06-27 09:07:09 浏览: 92
该代码段看起来没有明显的错误,但是需要确保以下几点: 1. `users` 中是否包含了所有的用户对象,如果不包含,那么该方法可能会抛出 `UnknownUserException`。 2. 在调用 `moreBook()` 方法之前,需要确保该用户对象是 Lender 或 Borrower 类型的,否则会抛出 `NotALenderException` 异常。 3. `notifyListeners()` 方法是否被正确调用。 如果以上几点都被满足,则该方法应该可以正常工作。
相关问题

write java。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.

Here is the Java code for the described functionality: ``` public class LibraryController { private Library library; public LibraryController(Library library) { this.library = library; } public String moreBook(String userName, String numBooks) { try { int num = Integer.parseInt(numBooks); library.moreBook(userName, num); return ""; } catch (UnknownUserException e) { return e.getMessage(); } catch (NotALenderException e) { return e.getMessage(); } catch (NumberFormatException e) { return e.getMessage(); } } } public class Library { private Map<String, User> users; public Library() { users = new HashMap<>(); } public void moreBook(String userName, int numBooks) throws UnknownUserException, NotALenderException { User user = users.get(userName); if (user == null) { throw new UnknownUserException("Unknown user: " + userName); } if (!(user instanceof Lender)) { throw new NotALenderException(userName + " is not a lender"); } ((Lender) user).moreBooks(numBooks); } } public class User { private String name; public User(String name) { this.name = name; } public String getName() { return name; } } public class Lender extends User { private int numBooks; public Lender(String name) { super(name); numBooks = 0; } public int getNumBooks() { return numBooks; } public void moreBooks(int num) { numBooks += num; } } public class UnknownUserException extends Exception { public UnknownUserException(String message) { super(message); } } public class NotALenderException extends Exception { public NotALenderException(String message) { super(message); } } ``` The LibraryController class has a constructor that takes a Library object as a parameter. The moreBook method takes a user name and a number of books as arguments. It uses the Integer.parseInt static method to parse the number of books from a string to an integer. It then calls the moreBook method of the Library object to increase the number of books borrowed or lent by the user, depending on what kind of user it is. If no exception occurs, the moreBook method of the controller returns the empty string. If the moreBook method of the library throws an UnknownUserException, the moreBook method of the controller catches this exception and returns the error message from the exception object. If the moreBook method of the library throws a NotALenderException, the moreBook method of the controller catches this exception and returns the error message from the exception object. If the parseInt method of the Integer class throws a NumberFormatException, the moreBook method of the controller catches this exception and returns the error message from the exception object. The Library class has a map of users, where the key is the user name and the value is a User object. The moreBook method takes a user name and a number of books as arguments. It throws an UnknownUserException if the user is not found in the map, and a NotALenderException if the user is not an instance of the Lender class. Otherwise, it calls the moreBooks method of the Lender object to increase the number of books borrowed by the user. The User class has a name field and a getName method. The Lender class extends the User class and adds a numBooks field and a moreBooks method. The UnknownUserException and NotALenderException classes extend the Exception class and have constructors that take an error message as a parameter.

public void moreBook(int number) 实现 number 与 book 变量 相加

假设book是一个整型变量,实现方法如下: ``` public void moreBook(int number) { book += number; } ``` 这里使用了 "+=" 运算符,可以将 number 与 book 相加并将结果赋值给 book。
阅读全文

相关推荐

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

Add a Library class with the following UML specification: +-------------------------------------------+ | Library | +-------------------------------------------+ | - name: String | | - users: ArrayList | +-------------------------------------------+ | + Library(String name) | | + addUser(IUser user): void | | + totalBorrowedBooks(): int | | + getBook(String name): int | | + moreBook(String name, int number): void | | + testLibrary(): void | +-------------------------------------------+ When a library is created, it has an arraylist of users (IUser) but the arraylist is empty (the arraylist does not contain any user). The addUser method takes a user (IUser) as argument and adds the user to the arraylist of users for the library. The totalBorrowedBooks method returns as result the total number of books borrowed by all users of the library (the result can be either positive or negative). The getBook method takes as argument the name of a user and returns as result the number of books currently borrowed by the user. If the library does not have a user with the given name, then the getBook method must throw an UnknownUserException with the message "User XXX unknown.", where XXX is replaced with the name of the user. Do not worry about multiple users having the same name. You can assume all user names are unique in the arraylist. The moreBook method takes as argument the name of a user and a number of books and changes the number of books currently borrowed by that user. If the library does not have a user with the given name, then the moreBook method must throw an UnknownUserException with the message "User XXX unknown.", where XXX is replaced with the name of the user. Do not worry about multiple users having the same name. Note: the moreBook method does not catch any exception, it only throws exceptions. Hint: use the equals method to compare strings, not the == operator which only works with constant strings. 写java文件

continue to use Java language, Add a Library class with the following UML specification: +-------------------------------------------+ | Library | +-------------------------------------------+ | - name: String | | - users: ArrayList<IUser> | +-------------------------------------------+ | + Library(String name) | | + addUser(IUser user): void | | + totalBorrowedBooks(): int | | + getBook(String name): int | | + moreBook(String name, int number): void | | + testLibrary(): void | +-------------------------------------------+ When a library is created, it has an arraylist of users (IUser) but the arraylist is empty (the arraylist does not contain any user). The addUser method takes a user (IUser) as argument and adds the user to the arraylist of users for the library. The totalBorrowedBooks method returns as result the total number of books borrowed by all users of the library (the result can be either positive or negative). The getBook method takes as argument the name of a user and returns as result the number of books currently borrowed by the user. If the library does not have a user with the given name, then the getBook method must throw an UnknownUserException with the message "User XXX unknown.", where XXX is replaced with the name of the user. Do not worry about multiple users having the same name. You can assume all user names are unique in the arraylist. The moreBook method takes as argument the name of a user and a number of books and changes the number of books currently borrowed by that user. If the library does not have a user with the given name, then the moreBook method must throw an UnknownUserException with the message "User XXX unknown.", where XXX is replaced with the name of the user. Do not worry about multiple users having the same name. Note: the moreBook method does not catch any exception, it only throws exceptions. Hint: use the equals method to compare strings, not the == operator which only works with constant strings.

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

最新推荐

recommend-type

基于对知乎热榜话题的数据抓取分析与可视化python实现源码+文档说明(高分完整项目)

基于对知乎热榜话题的数据抓取分析与可视化python实现源码+文档说明(高分完整项目),个人经导师指导并认可通过的高分毕业设计项目,评审分98分。主要针对计算机相关专业的正在做大作业和毕业设计的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。 基于对知乎热榜话题的数据抓取分析与可视化python实现源码+文档说明(高分完整项目)基于对知乎热榜话题的数据抓取分析与可视化python实现源码+文档说明(高分完整项目)基于对知乎热榜话题的数据抓取分析与可视化python实现源码+文档说明(高分完整项目)基于对知乎热榜话题的数据抓取分析与可视化python实现源码+文档说明(高分完整项目)基于对知乎热榜话题的数据抓取分析与可视化python实现源码+文档说明(高分完整项目)基于对知乎热榜话题的数据抓取分析与可视化python实现源码+文档说明(高分完整项目)基于对知乎热榜话题的数据抓取分析与可视化python实个人经导师指导并认可通过的高分毕业设计项目,评审分98分。主要针对计算机相关专业的正在做大作业和毕业设计的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。
recommend-type

电子技术课程 电路分析技术 12 非正弦周期电流电路及电路频率特性 共43页.pptx

电子技术课程 电路分析技术 12 非正弦周期电流电路及电路频率特性 共43页.pptx
recommend-type

(完整数据)全国及各省森林覆盖率、森林面积,700个城市绿地面积、绿化率等数据

## 数据指标说明 数据名称:中国及各省森林资源指标面板数据 数据来源:中国环境统计年鉴(2004-2020年) 森林覆盖率是指森林面积占土地总面积的比率,是反映一个国家(或地区)森林资源和林地占有的实际水平的重要指标,一般使用百分比表示。 林业用地面积是指生长乔木、竹类、灌木、沿海红树林等林木的土地面积。 森林面积是指由自然生长或人工种植且原地高度至少为5米的直立树木(无论是否属于生产性)所覆盖的土地,不包括农业生产系统中的立木。 根据第九次全国森林资源清查成果《中国森林资源报告(2014—2018)》,中国森林覆盖率达22.96%。2020年底,全国森林覆盖率达到23.04%,草原综合植被覆盖度达到56.1%,湿地保护率达到50%以上。
recommend-type

公司面试题记录汇总11

公司面试题记录汇总11
recommend-type

【教程4>第2章>第21节】软解调讲解2——FPGA开发,以16QAM为例_16qam软解调算法实现-CSDN博客.pdf

【教程4>第2章>第21节】软解调讲解2——FPGA开发,以16QAM为例_16qam软解调算法实现-CSDN博客.pdf
recommend-type

C语言数组操作:高度检查器编程实践

资源摘要信息: "C语言编程题之数组操作高度检查器" C语言是一种广泛使用的编程语言,它以其强大的功能和对低级操作的控制而闻名。数组是C语言中一种基本的数据结构,用于存储相同类型数据的集合。数组操作包括创建、初始化、访问和修改元素以及数组的其他高级操作,如排序、搜索和删除。本资源名为“c语言编程题之数组操作高度检查器.zip”,它很可能是一个围绕数组操作的编程实践,具体而言是设计一个程序来检查数组中元素的高度。在这个上下文中,“高度”可能是对数组中元素值的一个比喻,或者特定于某个应用场景下的一个术语。 知识点1:C语言基础 C语言编程题之数组操作高度检查器涉及到了C语言的基础知识点。它要求学习者对C语言的数据类型、变量声明、表达式、控制结构(如if、else、switch、循环控制等)有清晰的理解。此外,还需要掌握C语言的标准库函数使用,这些函数是处理数组和其他数据结构不可或缺的部分。 知识点2:数组的基本概念 数组是C语言中用于存储多个相同类型数据的结构。它提供了通过索引来访问和修改各个元素的方式。数组的大小在声明时固定,之后不可更改。理解数组的这些基本特性对于编写有效的数组操作程序至关重要。 知识点3:数组的创建与初始化 在C语言中,创建数组时需要指定数组的类型和大小。例如,创建一个整型数组可以使用int arr[10];语句。数组初始化可以在声明时进行,也可以在之后使用循环或单独的赋值语句进行。初始化对于定义检查器程序的初始状态非常重要。 知识点4:数组元素的访问与修改 通过使用数组索引(下标),可以访问数组中特定位置的元素。在C语言中,数组索引从0开始。修改数组元素则涉及到了将新值赋给特定索引位置的操作。在编写数组操作程序时,需要频繁地使用这些操作来实现功能。 知识点5:数组高级操作 除了基本的访问和修改之外,数组的高级操作包括排序、搜索和删除。这些操作在很多实际应用中都有广泛用途。例如,检查器程序可能需要对数组中的元素进行排序,以便于进行高度检查。搜索功能用于查找特定值的元素,而删除操作则用于移除数组中的元素。 知识点6:编程实践与问题解决 标题中提到的“高度检查器”暗示了一个具体的应用场景,可能涉及到对数组中元素的某种度量或标准进行判断。编写这样的程序不仅需要对数组操作有深入的理解,还需要将这些操作应用于解决实际问题。这要求编程者具备良好的逻辑思维能力和问题分析能力。 总结:本资源"c语言编程题之数组操作高度检查器.zip"是一个关于C语言数组操作的实际应用示例,它结合了编程实践和问题解决的综合知识点。通过实现一个针对数组元素“高度”检查的程序,学习者可以加深对数组基础、数组操作以及C语言编程技巧的理解。这种类型的编程题目对于提高编程能力和逻辑思维能力都有显著的帮助。
recommend-type

管理建模和仿真的文件

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

【KUKA系统变量进阶】:揭秘从理论到实践的5大关键技巧

![【KUKA系统变量进阶】:揭秘从理论到实践的5大关键技巧](https://giecdn.blob.core.windows.net/fileuploads/image/2022/11/17/kuka-visual-robot-guide.jpg) 参考资源链接:[KUKA机器人系统变量手册(KSS 8.6 中文版):深入解析与应用](https://wenku.csdn.net/doc/p36po06uv7?spm=1055.2635.3001.10343) # 1. KUKA系统变量的理论基础 ## 理解系统变量的基本概念 KUKA系统变量是机器人控制系统中的一个核心概念,它允许
recommend-type

如何使用Python编程语言创建一个具有动态爱心图案作为背景并添加文字'天天开心(高级版)'的图形界面?

要在Python中创建一个带动态爱心图案和文字的图形界面,可以结合使用Tkinter库(用于窗口和基本GUI元素)以及PIL(Python Imaging Library)处理图像。这里是一个简化的例子,假设你已经安装了这两个库: 首先,安装必要的库: ```bash pip install tk pip install pillow ``` 然后,你可以尝试这个高级版的Python代码: ```python import tkinter as tk from PIL import Image, ImageTk def draw_heart(canvas): heart = I
recommend-type

基于Swift开发的嘉定单车LBS iOS应用项目解析

资源摘要信息:"嘉定单车汇(IOS app).zip" 从标题和描述中,我们可以得知这个压缩包文件包含的是一套基于iOS平台的移动应用程序的开发成果。这个应用是由一群来自同济大学软件工程专业的学生完成的,其核心功能是利用位置服务(LBS)技术,面向iOS用户开发的单车共享服务应用。接下来将详细介绍所涉及的关键知识点。 首先,提到的iOS平台意味着应用是为苹果公司的移动设备如iPhone、iPad等设计和开发的。iOS是苹果公司专有的操作系统,与之相对应的是Android系统,另一个主要的移动操作系统平台。iOS应用通常是用Swift语言或Objective-C(OC)编写的,这在标签中也得到了印证。 Swift是苹果公司在2014年推出的一种新的编程语言,用于开发iOS和macOS应用程序。Swift的设计目标是与Objective-C并存,并最终取代后者。Swift语言拥有现代编程语言的特性,包括类型安全、内存安全、简化的语法和强大的表达能力。因此,如果一个项目是使用Swift开发的,那么它应该会利用到这些特性。 Objective-C是苹果公司早前主要的编程语言,用于开发iOS和macOS应用程序。尽管Swift现在是主要的开发语言,但仍然有许多现存项目和开发者在使用Objective-C。Objective-C语言集成了C语言与Smalltalk风格的消息传递机制,因此它通常被认为是一种面向对象的编程语言。 LBS(Location-Based Services,位置服务)是基于位置信息的服务。LBS可以用来为用户提供地理定位相关的信息服务,例如导航、社交网络签到、交通信息、天气预报等。本项目中的LBS功能可能包括定位用户位置、查找附近的单车、计算骑行路线等功能。 从文件名称列表来看,包含的三个文件分别是: 1. ios期末项目文档.docx:这份文档可能是对整个iOS项目的设计思路、开发过程、实现的功能以及遇到的问题和解决方案等进行的详细描述。对于理解项目的背景、目标和实施细节至关重要。 2. 移动应用开发项目期末答辩.pptx:这份PPT文件应该是为项目答辩准备的演示文稿,里面可能包括项目的概览、核心功能演示、项目亮点以及团队成员介绍等。这可以作为了解项目的一个快速入门方式,尤其是对项目的核心价值和技术难点有直观的认识。 3. LBS-ofo期末项目源码.zip:这是项目的源代码压缩包,包含了完成单车汇项目所需的全部Swift或Objective-C代码。源码对于理解项目背后的逻辑和实现细节至关重要,同时也是评估项目质量、学习最佳实践、复用或扩展功能的基础。 综合上述信息,"嘉定单车汇(IOS app).zip"不仅仅是一个应用程序的压缩包,它还代表了一个团队在软件工程项目中的完整工作流程,包含了项目文档、演示材料和实际编码,为学习和评估提供了一个很好的案例。