64 lines
1.6 KiB
Java
64 lines
1.6 KiB
Java
/*
|
|
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
|
* Date: 2022-04-25
|
|
* Description: This is the borrower class.
|
|
*/
|
|
|
|
public class Borrower extends User {
|
|
/**
|
|
* Constructor.
|
|
*
|
|
* @param name Borrower's name
|
|
* @param int book number of books borrowed by user
|
|
*/
|
|
public Borrower(String name, int book) throws NotALenderException {
|
|
super(name, book);
|
|
|
|
// check
|
|
if (book < 0) {
|
|
throw new NotALenderException("A new borrower cannot lend books.");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* increase the number of books borrowed by the user.
|
|
*
|
|
* @param number Incrased number of books
|
|
* @throws NotALenderException
|
|
*/
|
|
public void moreBook(int number) throws NotALenderException {
|
|
int newBook = getBook() + number;
|
|
if (number < 0 && newBook < 0) {
|
|
throw new NotALenderException("A borrower cannot lend " + (-newBook) + " book(s).");
|
|
}
|
|
setBook(newBook);
|
|
}
|
|
|
|
/**
|
|
* Test.
|
|
*/
|
|
public static void testBorrower() {
|
|
try {
|
|
Borrower b = new Borrower("Bob", -1);
|
|
} catch (NotALenderException e) {
|
|
System.out.println(e.getMessage().equals("A new borrower cannot lend books."));
|
|
}
|
|
try {
|
|
Borrower b = new Borrower("Bob", 10);
|
|
System.out.println(b.getName() == "Bob");
|
|
System.out.println(b.getBook() == 10);
|
|
b.setBook(5);
|
|
System.out.println(b.getBook() == 5);
|
|
b.moreBook(2);
|
|
System.out.println(b.getBook() == 7);
|
|
b.moreBook(-2);
|
|
System.out.println(b.getBook() == 5);
|
|
b.moreBook(-5);
|
|
System.out.println(b.getBook() == 0);
|
|
b.moreBook(-1);
|
|
} catch (NotALenderException e) {
|
|
System.out.println(e.getMessage().equals("A borrower cannot lend 1 book(s)."));
|
|
}
|
|
}
|
|
}
|