diff --git a/finalproject/Question3/Borrower.java b/finalproject/Question3/Borrower.java new file mode 100644 index 0000000..64b700c --- /dev/null +++ b/finalproject/Question3/Borrower.java @@ -0,0 +1,63 @@ +/* + * 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).")); + } + } +} diff --git a/finalproject/Question3/IUser.java b/finalproject/Question3/IUser.java new file mode 100644 index 0000000..f2f1b19 --- /dev/null +++ b/finalproject/Question3/IUser.java @@ -0,0 +1,13 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-25 + * Description: This is the interface of IUser. + */ + +public interface IUser { + public String getName(); + + public int getBook(); + + public void moreBook(int number) throws NotALenderException; +} diff --git a/finalproject/Question3/Lender.java b/finalproject/Question3/Lender.java new file mode 100644 index 0000000..8d719bf --- /dev/null +++ b/finalproject/Question3/Lender.java @@ -0,0 +1,42 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-25 + * Description: This is the Lender class. + */ + +public class Lender extends User { + /** + * Constructor. + * + * @param name The user's name. + * @param book number of bookds lent by the user. This will be store in negetive value. + */ + public Lender(String name, int book) { + // lender negetive book value + super(name, -book); + } + + /** + * Increases the number of bookds. + * + * @param book the number of books. + */ + public void moreBook(int book) { + setBook(getBook() - book); + } + + /** + * Test. + */ + public static void testLender() { + Lender l = new Lender("Anna", 5); + System.out.println(l.getName() == "Anna"); + System.out.println(l.getBook() == -5); + l.setBook(-6); + System.out.println(l.getBook() == -6); + l.moreBook(2); + System.out.println(l.getBook() == -8); + l.moreBook(-9); + System.out.println(l.getBook() == 1); + } +} diff --git a/finalproject/Question3/NotALenderException.java b/finalproject/Question3/NotALenderException.java new file mode 100644 index 0000000..377f323 --- /dev/null +++ b/finalproject/Question3/NotALenderException.java @@ -0,0 +1,5 @@ +public class NotALenderException extends Exception { + public NotALenderException(String message) { + super(message); + } +} diff --git a/finalproject/Question3/Test.java b/finalproject/Question3/Test.java new file mode 100644 index 0000000..636cd79 --- /dev/null +++ b/finalproject/Question3/Test.java @@ -0,0 +1,7 @@ +public class Test { + public static void main(String[] args) { + User.testUser(); + Lender.testLender(); + Borrower.testBorrower(); + } +} diff --git a/finalproject/Question3/User.java b/finalproject/Question3/User.java new file mode 100644 index 0000000..6eec5a9 --- /dev/null +++ b/finalproject/Question3/User.java @@ -0,0 +1,69 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-25 + * Description: This is the User class. + */ + +public abstract class User implements IUser { + /** + * The user's name. + */ + private String name; + + /** + * The number of books borrowed by the user. + */ + private int book; + + /** + * Constructor. + * + * @param name The user's name. + * @param book The number of books borrowed by the user. + */ + public User(String name, int book) { + this.name = name; + this.book = book; + } + + /** + * Getter of the user's name. + * + * @return The user's name. + */ + public String getName() { + return name; + } + + /** + * Getter of the number of books borrowed by the user. + * + * @return The number of books borrowed by the user. + */ + public int getBook() { + return book; + } + + /** + * Change the number of books borrowed by the user. + * + * @param book The number of books borrowed by the user. + */ + protected void setBook(int book) { + this.book = book; + } + + /** + * Increase the number of books borrowed by the user. + * + * @param book The number of books borrowed by the user. + */ + public abstract void moreBook(int number) throws NotALenderException; + + /** + * Test. + */ + public static void testUser() { + // abstract class not testing + } +}