From 6347507ededec21bf10573b2f19a56685fe39297 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Tue, 24 May 2022 22:08:20 +0800 Subject: [PATCH] final project q2 --- finalproject/Question2/IUser.java | 13 +++++ finalproject/Question2/Lender.java | 42 ++++++++++++++++ finalproject/Question2/Test.java | 6 +++ finalproject/Question2/User.java | 78 ++++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+) create mode 100644 finalproject/Question2/IUser.java create mode 100644 finalproject/Question2/Lender.java create mode 100644 finalproject/Question2/Test.java create mode 100644 finalproject/Question2/User.java diff --git a/finalproject/Question2/IUser.java b/finalproject/Question2/IUser.java new file mode 100644 index 0000000..95accaa --- /dev/null +++ b/finalproject/Question2/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); +} diff --git a/finalproject/Question2/Lender.java b/finalproject/Question2/Lender.java new file mode 100644 index 0000000..8d719bf --- /dev/null +++ b/finalproject/Question2/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/Question2/Test.java b/finalproject/Question2/Test.java new file mode 100644 index 0000000..07cb17b --- /dev/null +++ b/finalproject/Question2/Test.java @@ -0,0 +1,6 @@ +public class Test { + public static void main(String[] args) { + User.testUser(); + Lender.testLender(); + } +} diff --git a/finalproject/Question2/User.java b/finalproject/Question2/User.java new file mode 100644 index 0000000..2c9d266 --- /dev/null +++ b/finalproject/Question2/User.java @@ -0,0 +1,78 @@ +/* + * 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); + + /** + * Test. + */ + public static void testUser() { + User user = new User("Walter", 3) { + @Override + public void moreBook(int number) { + setBook(getBook() + number); + } + }; + System.out.println(user.getName() == "Walter"); + System.out.println(user.getBook() == 3); + user.moreBook(2); + System.out.println(user.getBook() == 5); + } +}