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(); } }
时间: 2023-08-15 11:11:30 浏览: 134
Sure, I can help you with that! Here's the code for the IUser interface and User class:
IUser interface:
```java
public interface IUser {
String getName();
int getBook();
void moreBook(int number);
}
```
User class:
```java
public class User implements IUser {
private String name;
private int book;
public User(String name, int book) {
this.name = name;
this.book = book;
}
@Override
public String getName() {
return name;
}
@Override
public int getBook() {
return book;
}
protected void setBook(int book) {
this.book = book;
}
public void moreBook(int number) {
// abstract method, implementation will be provided in subclasses
}
public static void testUser() {
User borrower = new Borrower("John", 2);
User lender = new Lender("Jane", 5);
System.out.println("Borrower " + borrower.getName() + " has " + borrower.getBook() + " books borrowed.");
System.out.println("Lender " + lender.getName() + " has " + lender.getBook() + " books lent.");
borrower.moreBook(1);
lender.moreBook(3);
System.out.println("Borrower " + borrower.getName() + " has " + borrower.getBook() + " books borrowed.");
System.out.println("Lender " + lender.getName() + " has " + lender.getBook() + " books lent.");
}
}
```
The User class has a constructor that takes a name and a number of books as arguments. It also implements the methods of the IUser interface.
The setBook method is protected, meaning it can only be accessed by subclasses of User.
The moreBook method is abstract, as its implementation will depend on the type of user (borrower or lender). This will be implemented in the subclasses.
Here's an example of two subclasses, Borrower and Lender, that extend User and implement the moreBook method:
```java
public class Borrower extends User {
public Borrower(String name, int book) {
super(name, book);
}
@Override
public void moreBook(int number) {
setBook(getBook() + number);
}
}
public class Lender extends User {
public Lender(String name, int book) {
super(name, book);
}
@Override
public void moreBook(int number) {
setBook(getBook() - number);
}
}
```
The Borrower class increases the number of books borrowed, while the Lender class decreases the number of books lent.
And finally, here's the Test class to test the User class:
```java
public class Test {
public static void main(String[] args) {
User.testUser();
}
}
```
This will create a Borrower and a Lender object, print their initial number of books, call the moreBook method on both objects, and print the new number of books.
阅读全文