From 252d0c67bb25173b07125c2689db52d9c2c2c6b8 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Tue, 24 May 2022 23:51:13 +0800 Subject: [PATCH] final project q11 --- finalproject/Question11/Borrower.java | 63 ++++++++ finalproject/Question11/CLI.java | 145 ++++++++++++++++++ finalproject/Question11/Controller.java | 7 + finalproject/Question11/ControllerCreate.java | 27 ++++ .../Question11/ControllerGetBook.java | 15 ++ .../Question11/ControllerMoreBook.java | 27 ++++ finalproject/Question11/ControllerSimple.java | 12 ++ finalproject/Question11/GUI.java | 21 +++ finalproject/Question11/IUser.java | 13 ++ finalproject/Question11/Lender.java | 42 +++++ finalproject/Question11/Library.java | 122 +++++++++++++++ finalproject/Question11/ModelListener.java | 3 + .../Question11/NotALenderException.java | 5 + finalproject/Question11/Test.java | 8 + .../Question11/UnknownUserException.java | 5 + finalproject/Question11/User.java | 69 +++++++++ finalproject/Question11/View.java | 18 +++ finalproject/Question11/ViewCreate.java | 60 ++++++++ finalproject/Question11/ViewGetBook.java | 40 +++++ finalproject/Question11/ViewMoreBook.java | 52 +++++++ finalproject/Question11/ViewSimple.java | 29 ++++ 21 files changed, 783 insertions(+) create mode 100644 finalproject/Question11/Borrower.java create mode 100644 finalproject/Question11/CLI.java create mode 100644 finalproject/Question11/Controller.java create mode 100644 finalproject/Question11/ControllerCreate.java create mode 100644 finalproject/Question11/ControllerGetBook.java create mode 100644 finalproject/Question11/ControllerMoreBook.java create mode 100644 finalproject/Question11/ControllerSimple.java create mode 100644 finalproject/Question11/GUI.java create mode 100644 finalproject/Question11/IUser.java create mode 100644 finalproject/Question11/Lender.java create mode 100644 finalproject/Question11/Library.java create mode 100644 finalproject/Question11/ModelListener.java create mode 100644 finalproject/Question11/NotALenderException.java create mode 100644 finalproject/Question11/Test.java create mode 100644 finalproject/Question11/UnknownUserException.java create mode 100644 finalproject/Question11/User.java create mode 100644 finalproject/Question11/View.java create mode 100644 finalproject/Question11/ViewCreate.java create mode 100644 finalproject/Question11/ViewGetBook.java create mode 100644 finalproject/Question11/ViewMoreBook.java create mode 100644 finalproject/Question11/ViewSimple.java diff --git a/finalproject/Question11/Borrower.java b/finalproject/Question11/Borrower.java new file mode 100644 index 0000000..64b700c --- /dev/null +++ b/finalproject/Question11/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/Question11/CLI.java b/finalproject/Question11/CLI.java new file mode 100644 index 0000000..c172ec3 --- /dev/null +++ b/finalproject/Question11/CLI.java @@ -0,0 +1,145 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-25 + * Description: This is the CLI class. + */ + +import java.util.InputMismatchException; +import java.util.Scanner; + +public class CLI { + private static Scanner input = new Scanner(System.in); + + /** + * Read one line from CLI. + * + * @param message Hint printed to screen + * @return one line input + */ + private static String readLine(String message) { + System.out.print(message); + return input.nextLine(); + } + + /** + * Read one positive integer. + * + * @param message HINT printed to screen + * @return one positive integer + */ + private static int readPosInt(String message) { + while (true) { + System.out.print(message); + try { + int result = input.nextInt(); + input.nextLine(); + if (result >= 0) { + return result; + } else { + System.out.println("Positive integers only!"); + } + } catch (InputMismatchException e) { + System.out.println("You must type an integer!"); + input.nextLine(); + } + } + } + + public static void main(String[] args) { + int book; + String username; + String menuHint = "Type an action (total:1 add:2 get:3 more:4 less:5 quit:6): "; + String userRoleHint = "Type the user role (lender:1 borrower:2): "; + + Library library = new Library("UIC Library"); + while (true) { + switch (readPosInt(menuHint)) { + // total + case 1: + System.out.println("Total number of borrowed books: " + + library.totalBorrowedBooks()); + break; + // add + case 2: + switch (readPosInt(userRoleHint)) { + // lender + case 1: + username = readLine("Enter the name of the user: "); + book = readPosInt( + "Enter the initial number of borrowed books: "); + Lender lender = new Lender(username, book); + library.addUser(lender); + System.out.println("Lender \"" + username + "\" lending " + book + + " book(s) has been added."); + break; + // borrower + case 2: + username = readLine( + "Enter the name of the user: "); + book = readPosInt( + "Enter the initial number of borrowed books: "); + try { + library.addUser(new Borrower(username, book)); + System.out.println("Borrower \"" + + username + + "\" borrowing " + + book + + " book(s) has been added."); + } catch (NotALenderException e) { + System.out.println("BUG! This must never happen!"); + System.exit(1); + } + break; + default: + System.out.println("Unknown user role!"); + break; + } + break; + // get + case 3: + username = readLine("Enter the name of the user: "); + try { + System.out.println(username + " borrows " + + library.getBook(username) + " book(s)."); + } catch (UnknownUserException e) { + System.out.println("User " + username + " unknown."); + } + break; + case 4: + username = readLine("Enter the name of the user: "); + book = readPosInt("Enter the number of books: "); + try { + library.moreBook(username, book); + } catch (NotALenderException e) { + System.out.println("BUG! This must never happen!"); + } catch (UnknownUserException e) { + System.out.println("User " + username + " unknown."); + } + break; + case 5: + username = readLine("Enter the name of the user: "); + book = readPosInt("Enter the number of books: "); + try { + // decrease numbers + try { + library.moreBook(username, -book); + } catch (NotALenderException e) { + System.out.println("A borrower cannot lend " + + (-(library.getBook(username) - book)) + + " book(s)."); + } + } catch (UnknownUserException e) { + System.out.println("User " + username + " unknown."); + } + break; + case 6: + System.out.println("Goodbye!"); + System.exit(0); + break; + default: + System.out.println("Unknown action!"); + break; + } + } + } +} diff --git a/finalproject/Question11/Controller.java b/finalproject/Question11/Controller.java new file mode 100644 index 0000000..15bf145 --- /dev/null +++ b/finalproject/Question11/Controller.java @@ -0,0 +1,7 @@ +public class Controller { + protected Library m; + + public Controller(Library m) { + this.m = m; + } +} diff --git a/finalproject/Question11/ControllerCreate.java b/finalproject/Question11/ControllerCreate.java new file mode 100644 index 0000000..8c30ae5 --- /dev/null +++ b/finalproject/Question11/ControllerCreate.java @@ -0,0 +1,27 @@ +public class ControllerCreate extends Controller { + public ControllerCreate(Library m) { + super(m); + } + + /** + * @param name + * @param number + * @param type + * @return + */ + public String create(String name, String number, int type) { + try { + int num = Integer.parseInt(number); + if (type == 0) { + m.addUser(new Lender(name, num)); + } else { + m.addUser(new Borrower(name, num)); + } + return ""; + } catch (NotALenderException e) { + return e.getMessage(); + } catch (NumberFormatException e) { + return e.getMessage(); + } + } +} diff --git a/finalproject/Question11/ControllerGetBook.java b/finalproject/Question11/ControllerGetBook.java new file mode 100644 index 0000000..892b978 --- /dev/null +++ b/finalproject/Question11/ControllerGetBook.java @@ -0,0 +1,15 @@ +public class ControllerGetBook extends Controller { + public ControllerGetBook(Library m) { + super(m); + } + + // get book + public String getBook(String name) { + try { + return Integer.toString(m.getBook(name)); + } catch (UnknownUserException e) { + // return error message + return e.getMessage(); + } + } +} diff --git a/finalproject/Question11/ControllerMoreBook.java b/finalproject/Question11/ControllerMoreBook.java new file mode 100644 index 0000000..88db121 --- /dev/null +++ b/finalproject/Question11/ControllerMoreBook.java @@ -0,0 +1,27 @@ +public class ControllerMoreBook extends Controller { + // constr + public ControllerMoreBook(Library m) { + super(m); + } + + /** + * @param name the username + * @param number the book number + */ + public String moreBook(String name, String number) { + try { + int num = Integer.parseInt(number); + m.moreBook(name, num); + return ""; + + } catch (UnknownUserException e) { + return e.getMessage(); + + } catch (NotALenderException e) { + return e.getMessage(); + + } catch (NumberFormatException e) { + return e.getMessage(); + } + } +} diff --git a/finalproject/Question11/ControllerSimple.java b/finalproject/Question11/ControllerSimple.java new file mode 100644 index 0000000..4e4f8d5 --- /dev/null +++ b/finalproject/Question11/ControllerSimple.java @@ -0,0 +1,12 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-25 + */ + +public class ControllerSimple extends Controller { + private Library m; + + public ControllerSimple(Library m) { + super(m); + } +} diff --git a/finalproject/Question11/GUI.java b/finalproject/Question11/GUI.java new file mode 100644 index 0000000..873bc4f --- /dev/null +++ b/finalproject/Question11/GUI.java @@ -0,0 +1,21 @@ +public class GUI { + public static void main(String[] args) { + javax.swing.SwingUtilities.invokeLater(new Runnable() { + public void run() { + Library li = new Library("UIC Library"); + ControllerSimple cs = new ControllerSimple(li); + + ViewSimple vs = new ViewSimple(li, cs); + ControllerGetBook cgb = new ControllerGetBook(li); + + ViewGetBook vgb = new ViewGetBook(li, cgb); + ControllerMoreBook cmb = new ControllerMoreBook(li); + + ViewMoreBook vmb = new ViewMoreBook(li, cmb); + ControllerCreate cc = new ControllerCreate(li); + + ViewCreate vc = new ViewCreate(li, cc); + } + }); + } +} diff --git a/finalproject/Question11/IUser.java b/finalproject/Question11/IUser.java new file mode 100644 index 0000000..f2f1b19 --- /dev/null +++ b/finalproject/Question11/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/Question11/Lender.java b/finalproject/Question11/Lender.java new file mode 100644 index 0000000..8d719bf --- /dev/null +++ b/finalproject/Question11/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/Question11/Library.java b/finalproject/Question11/Library.java new file mode 100644 index 0000000..b6ad463 --- /dev/null +++ b/finalproject/Question11/Library.java @@ -0,0 +1,122 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-25 + * Description: This is the Library class. + */ + +import java.util.ArrayList; + +public class Library { + private String name; + private ArrayList users; + private ArrayList listeners; + + /** + * Constructor. + * + * @param name + */ + public Library(String name) { + this.name = name; + + // init array list + this.users = new ArrayList(); + this.listeners = new ArrayList(); + } + + // GUI Related + public void addListener(ModelListener listener) { + listeners.add(listener); + } + + // GUI Related + private void notifyListeners() { + for (ModelListener listener : listeners) { + listener.update(); + } + } + + /** + * Add the user to the array list. + * + * @param user The user to be added + */ + public void addUser(IUser user) { + users.add(user); + notifyListeners(); + } + + /** + * The total number of bookds borrowed by all users. + * + * @return the number of books. + */ + public int totalBorrowedBooks() { + int total = 0; + for (IUser user : users) { + total += user.getBook(); + } + return total; + } + + /** + * Get the number of books borrowed by this user. + * + * @param name the name of the query user + * @return the number of books + * @throws UnknownUserException if user not found + */ + public int getBook(String name) throws UnknownUserException { + for (IUser user : users) { + if (user.getName().equals(name)) { + return user.getBook(); + } + } + // user not found + throw new UnknownUserException("User " + name + " unknown."); + } + + /** + * Change the number of books currently borrowed by that user. + * + * @param name the user's name + * @param number the number of books + * @throws UnknownUserException user not found + * @throws NotALenderException can not lend book + */ + public void moreBook(String name, int number) throws UnknownUserException, NotALenderException { + for (IUser user : users) { + if (user.getName().equals(name)) { + user.moreBook(number); + notifyListeners(); + return; + } + } + // user not found + throw new UnknownUserException("User " + name + " unknown."); + } + + /** + * Test. + */ + public static void testLibrary() { + Library li = new Library("UIC Library"); + System.out.println(li.totalBorrowedBooks() == 0); + li.addUser(new Lender("L1", 10)); + try { + System.out.println(li.getBook("L1") == -10); + System.out.println(li.totalBorrowedBooks() == -10); + li.addUser(new Borrower("B1", 20)); + System.out.println(li.getBook("L1") == -10); + System.out.println(li.getBook("B1") == 20); + System.out.println(li.totalBorrowedBooks() == 10); + li.getBook("B2"); + } catch (UnknownUserException ex) { + System.out.println(ex.getMessage().equals("User B2 unknown.")); + } catch (NotALenderException ex) { + // This should never happen! + System.out.println(false); + } + // More test cases are needed + } +} diff --git a/finalproject/Question11/ModelListener.java b/finalproject/Question11/ModelListener.java new file mode 100644 index 0000000..664e6e5 --- /dev/null +++ b/finalproject/Question11/ModelListener.java @@ -0,0 +1,3 @@ +public interface ModelListener { + public void update(); +} diff --git a/finalproject/Question11/NotALenderException.java b/finalproject/Question11/NotALenderException.java new file mode 100644 index 0000000..377f323 --- /dev/null +++ b/finalproject/Question11/NotALenderException.java @@ -0,0 +1,5 @@ +public class NotALenderException extends Exception { + public NotALenderException(String message) { + super(message); + } +} diff --git a/finalproject/Question11/Test.java b/finalproject/Question11/Test.java new file mode 100644 index 0000000..d7fc547 --- /dev/null +++ b/finalproject/Question11/Test.java @@ -0,0 +1,8 @@ +public class Test { + public static void main(String[] args) { + User.testUser(); + Lender.testLender(); + Borrower.testBorrower(); + Library.testLibrary(); + } +} diff --git a/finalproject/Question11/UnknownUserException.java b/finalproject/Question11/UnknownUserException.java new file mode 100644 index 0000000..9f9f04a --- /dev/null +++ b/finalproject/Question11/UnknownUserException.java @@ -0,0 +1,5 @@ +public class UnknownUserException extends Exception { + public UnknownUserException(String message) { + super(message); + } +} diff --git a/finalproject/Question11/User.java b/finalproject/Question11/User.java new file mode 100644 index 0000000..6eec5a9 --- /dev/null +++ b/finalproject/Question11/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 + } +} diff --git a/finalproject/Question11/View.java b/finalproject/Question11/View.java new file mode 100644 index 0000000..ec657d8 --- /dev/null +++ b/finalproject/Question11/View.java @@ -0,0 +1,18 @@ +import javax.swing.JFrame; + +public abstract class View extends JFrame implements ModelListener { + protected Library m; + protected T c; + + public View(Library m, T c) { + this.m = m; + this.c = c; + + m.addListener(this); + + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + this.setLocationRelativeTo(null); + } + + public abstract void update(); +} diff --git a/finalproject/Question11/ViewCreate.java b/finalproject/Question11/ViewCreate.java new file mode 100644 index 0000000..27e5cdd --- /dev/null +++ b/finalproject/Question11/ViewCreate.java @@ -0,0 +1,60 @@ +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.JButton; +import javax.swing.JComboBox; +import javax.swing.JOptionPane; +import javax.swing.JTextField; + +public class ViewCreate extends View { + private JTextField tName; + private JTextField tBook; + private JComboBox cb; + + public ViewCreate(Library m, ControllerCreate c) { + super(m, c); + + // window + this.setTitle("View Create"); + this.setSize(300, 200); + + GridLayout layout = new GridLayout(4, 1); + this.setLayout(layout); + + // text field + tName = new JTextField(); + add(tName); + + // text field + tBook = new JTextField(); + add(tBook); + + // box + cb = new JComboBox(); + cb.addItem("Lender"); + cb.addItem("Borrower"); + add(cb); + + // button + JButton b = new JButton("Create"); + b.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + String name = tName.getText(); + String num = tBook.getText(); + String result = c.create(name, num, cb.getSelectedIndex()); + + // check + if (result != "") { + JOptionPane.showMessageDialog(null, result); + } + } + }); + add(b); + + // final + this.setVisible(true); + } + + public void update() { + } +} diff --git a/finalproject/Question11/ViewGetBook.java b/finalproject/Question11/ViewGetBook.java new file mode 100644 index 0000000..58a6292 --- /dev/null +++ b/finalproject/Question11/ViewGetBook.java @@ -0,0 +1,40 @@ +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.JButton; +import javax.swing.JOptionPane; +import javax.swing.JTextField; + +public class ViewGetBook extends View { + private JTextField t; + + public ViewGetBook(Library m, ControllerGetBook c) { + super(m, c); + + // win + this.setTitle("View GetBook"); + this.setSize(300, 200); + + GridLayout layout = new GridLayout(2, 1); + this.setLayout(layout); + + t = new JTextField(); + add(t); + + JButton b = new JButton("Tell me the book number"); + b.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + String result = c.getBook(t.getText()); + JOptionPane.showMessageDialog(null, result); + } + }); + add(b); + + // final + this.setVisible(true); + } + + public void update() { + // do nothing + } +} diff --git a/finalproject/Question11/ViewMoreBook.java b/finalproject/Question11/ViewMoreBook.java new file mode 100644 index 0000000..3662a61 --- /dev/null +++ b/finalproject/Question11/ViewMoreBook.java @@ -0,0 +1,52 @@ +import java.awt.GridLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.JButton; +import javax.swing.JOptionPane; +import javax.swing.JTextField; + +public class ViewMoreBook extends View { + private JTextField tName; + private JTextField tBook; + + public ViewMoreBook(Library m, ControllerMoreBook c) { + super(m, c); + + // window + this.setTitle("View More Book"); + this.setSize(300, 200); + + GridLayout layout = new GridLayout(3, 1); + this.setLayout(layout); + + // text field 1 + tName = new JTextField(); + add(tName); + + // text field 2 + tBook = new JTextField(); + add(tBook); + + // button + JButton b = new JButton("Tell me the book number"); + b.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + String name = tName.getText(); + String num = tBook.getText(); + String result = c.moreBook(name, num); + + // check + if (result != "") { + JOptionPane.showMessageDialog(null, result); + } + } + }); + add(b); + + // final + this.setVisible(true); + } + + public void update() { + } +} diff --git a/finalproject/Question11/ViewSimple.java b/finalproject/Question11/ViewSimple.java new file mode 100644 index 0000000..273efda --- /dev/null +++ b/finalproject/Question11/ViewSimple.java @@ -0,0 +1,29 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-25 + */ + +import javax.swing.JLabel; + +public class ViewSimple extends View { + private JLabel label; + + public ViewSimple(Library m, ControllerSimple c) { + super(m, c); + + // window + this.setTitle("View Simple"); + this.setSize(300, 200); + + // conponent + label = new JLabel("Total number of borrowed books: " + m.totalBorrowedBooks()); + add(label); + + // final + this.setVisible(true); + } + + public void update() { + label.setText("Total number of borrowed books: " + m.totalBorrowedBooks()); + } +}