Compare commits

...

21 Commits

Author SHA1 Message Date
5dfbf0b8ae a6 2022-05-25 13:14:19 +08:00
a9f7967976 lab 13 2022-05-25 13:14:19 +08:00
d0416b02bd lab12 2022-05-25 13:14:16 +08:00
1357f94bfb Merge branch 'finalproject' 2022-05-25 13:11:26 +08:00
88a587102f final project q12 giveup 2022-05-24 23:54:02 +08:00
252d0c67bb final project q11 2022-05-24 23:51:13 +08:00
621b5c355d final project q10 2022-05-24 23:46:41 +08:00
16eacad6a1 final project q9 2022-05-24 23:41:21 +08:00
3dc6689804 final project q8 2022-05-24 23:35:04 +08:00
d3cf0c7a7c final project q7 2022-05-24 23:19:13 +08:00
07ed06c619 final project q6 2022-05-24 23:07:43 +08:00
707750c9c8 final project q5 2022-05-24 22:40:38 +08:00
e2bd453330 final project q4 2022-05-24 22:32:29 +08:00
1c6ea20c0a final project q3 2022-05-24 22:23:20 +08:00
6347507ede final project q2 2022-05-24 22:08:20 +08:00
7adb69e3a1 rename to test 2022-05-24 22:07:48 +08:00
fa9befb1b1 format code 2022-05-24 17:56:17 +08:00
243a44f7ed add lab11 2022-05-11 23:36:36 +08:00
855bc8cc3c Fix: a5 testcode close #11 2022-05-02 01:09:47 +08:00
053113577f lab10 2022-05-01 23:24:00 +08:00
c1acb5b92e finalproject q1 2022-04-25 10:20:31 +08:00
230 changed files with 7999 additions and 46 deletions

View File

@@ -4,34 +4,32 @@ public class StartGrading {
//The Vehicle class is abstract. We cannot create objects from this class. Therefore, we cannot test anything
}
public static void testCar() {
// test case for constructors
try {
// exception
// exception
Car c1 = new Car(70, 80);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
}
try {
// exception
// exception
Car c2 = new Car(70, -10);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
}
// test case for accelerate, canFly and getSpeed methods
// test case for accelerate, canFly and getSpeed methods
try {
Car c3 = new Car(100, 80);
// test the canFly method
// test the canFly method
System.out.println(c3.canFly() == false);
c3.accelerate(10);
System.out.println(c3.getSpeed() == 90);
// test the accelerate with positive amount
// test the accelerate with positive amount
c3.accelerate(-20);
System.out.println(c3.getSpeed() == 70);
// test the accelerate with negative amount
c3.accelerate(35);
// exception
// test the accelerate with negative amount
c3.accelerate(35); // exception
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (ExceedSpeedLimit e) {
@@ -42,7 +40,7 @@ public class StartGrading {
}
try {
Car c4 = new Car(100, 80);
// exception
// exception
c4.accelerate(-90);
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");

View File

@@ -7,30 +7,29 @@ public class StartGrading {
public static void testCar() {
// test case for constructors
try {
// exception
// exception
Car c1 = new Car(70, 80);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
}
try {
// exception
// exception
Car c2 = new Car(70, -10);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
}
// test case for accelerate, canFly and getSpeed methods
// test case for accelerate, canFly and getSpeed methods
try {
Car c3 = new Car(100, 80);
// test the canFly method
// test the canFly method
System.out.println(c3.canFly() == false);
c3.accelerate(10);
System.out.println(c3.getSpeed() == 90);
// test the accelerate with positive amount
// test the accelerate with positive amount
c3.accelerate(-20);
System.out.println(c3.getSpeed() == 70);
// test the accelerate with negative amount
c3.accelerate(35);
// exception
// test the accelerate with negative amount
c3.accelerate(35); // exception
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (ExceedSpeedLimit e) {
@@ -41,7 +40,7 @@ public class StartGrading {
}
try {
Car c4 = new Car(100, 80);
// exception
// exception
c4.accelerate(-90);
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");

View File

@@ -1,3 +1,4 @@
//Q4
public class StartGrading {
public static void testVehicle() {
//The Vehicle class is abstract. We cannot create objects from this class. Therefore, we cannot test anything
@@ -6,30 +7,29 @@ public class StartGrading {
public static void testCar() {
// test case for constructors
try {
// exception
// exception
Car c1 = new Car(70, 80);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
}
try {
// exception
// exception
Car c2 = new Car(70, -10);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
}
// test case for accelerate, canFly and getSpeed methods
// test case for accelerate, canFly and getSpeed methods
try {
Car c3 = new Car(100, 80);
// test the canFly method
// test the canFly method
System.out.println(c3.canFly() == false);
c3.accelerate(10);
System.out.println(c3.getSpeed() == 90);
// test the accelerate with positive amount
// test the accelerate with positive amount
c3.accelerate(-20);
System.out.println(c3.getSpeed() == 70);
// test the accelerate with negative amount
c3.accelerate(35);
// exception
// test the accelerate with negative amount
c3.accelerate(35); // exception
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (ExceedSpeedLimit e) {
@@ -37,10 +37,12 @@ public class StartGrading {
.println(e.getMessage().equals("Current speed is 70. Accelerate 35 will exceed the speed limit!"));
} catch (NotEnoughSpeed e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
try {
Car c4 = new Car(100, 80);
// exception
// exception
c4.accelerate(-90);
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
@@ -48,6 +50,8 @@ public class StartGrading {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (NotEnoughSpeed e) {
System.out.println(e.getMessage().equals("Current speed is " + 80 + ", not enough speed to decelerate!"));
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
}
@@ -101,6 +105,8 @@ public class StartGrading {
e.getMessage().equals("Current speed is 100. Accelerate 650 will exceed the speed limit!"));
} catch (NotEnoughSpeed e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
try {
Aircraft a5 = new Aircraft(700, 200, 100);
@@ -114,6 +120,8 @@ public class StartGrading {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (NotEnoughSpeed e) {
System.out.println(e.getMessage().equals("Current speed is " + 200 + ", not enough speed to decelerate!"));
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
}
@@ -123,7 +131,7 @@ public class StartGrading {
// exception
Train t1 = new Train(350, 400);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
}
try {
// exception
@@ -150,7 +158,7 @@ public class StartGrading {
public static void main(String[] args) {
Vehicle.testVehicle();
Car.testCar();
AirCraft.testAirCraft();
Aircraft.testAircraft();
Train.testTrain();
}
}

View File

@@ -31,7 +31,7 @@ public class ManyVehicles {
*
* @return the average speed of all vehicles
*/
public double averageSpeed() {
public double calcAvgSpeed() {
double totalSpeed = 0;
for (Vehicle v : vehicles) {
totalSpeed += v.getSpeed();
@@ -50,7 +50,7 @@ public class ManyVehicles {
mv.addVehicle(new Aircraft(100, 70, 3900));
mv.addVehicle(new Train(100, 60));
System.out.println(mv.averageSpeed() == 80.0);
System.out.println(mv.calcAvgSpeed() == 80.0);
} catch (Exception e) {
System.out.println("Error uncaught exception: " + e.getClass().getName() + e.getMessage());
}

View File

@@ -1,3 +1,4 @@
//Q5
public class StartGrading {
public static void testVehicle() {
//The Vehicle class is abstract. We cannot create objects from this class. Therefore, we cannot test anything
@@ -6,30 +7,29 @@ public class StartGrading {
public static void testCar() {
// test case for constructors
try {
// exception
// exception
Car c1 = new Car(70, 80);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
}
try {
// exception
// exception
Car c2 = new Car(70, -10);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
}
// test case for accelerate, canFly and getSpeed methods
// test case for accelerate, canFly and getSpeed methods
try {
Car c3 = new Car(100, 80);
// test the canFly method
// test the canFly method
System.out.println(c3.canFly() == false);
c3.accelerate(10);
System.out.println(c3.getSpeed() == 90);
// test the accelerate with positive amount
// test the accelerate with positive amount
c3.accelerate(-20);
System.out.println(c3.getSpeed() == 70);
// test the accelerate with negative amount
c3.accelerate(35);
// exception
// test the accelerate with negative amount
c3.accelerate(35); // exception
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (ExceedSpeedLimit e) {
@@ -37,10 +37,12 @@ public class StartGrading {
.println(e.getMessage().equals("Current speed is 70. Accelerate 35 will exceed the speed limit!"));
} catch (NotEnoughSpeed e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
try {
Car c4 = new Car(100, 80);
// exception
// exception
c4.accelerate(-90);
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
@@ -48,6 +50,8 @@ public class StartGrading {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (NotEnoughSpeed e) {
System.out.println(e.getMessage().equals("Current speed is " + 80 + ", not enough speed to decelerate!"));
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
}
@@ -101,6 +105,8 @@ public class StartGrading {
e.getMessage().equals("Current speed is 100. Accelerate 650 will exceed the speed limit!"));
} catch (NotEnoughSpeed e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
try {
Aircraft a5 = new Aircraft(700, 200, 100);
@@ -114,6 +120,8 @@ public class StartGrading {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
} catch (NotEnoughSpeed e) {
System.out.println(e.getMessage().equals("Current speed is " + 200 + ", not enough speed to decelerate!"));
} catch (TrainSpeedChange e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
}
@@ -123,7 +131,7 @@ public class StartGrading {
// exception
Train t1 = new Train(350, 400);
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
}
try {
// exception
@@ -149,7 +157,7 @@ public class StartGrading {
public static void testManyVehicles() {
ManyVehicles mv = new ManyVehicles();
// Add any kind of vehicles to the arraylist.
// Add any kind of vehicles to the arraylist.
try {
Train t = new Train(350, 300);
mv.addVehicle(t);
@@ -163,7 +171,7 @@ public class StartGrading {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
}
try {
AirCraft a = new AirCraft(700, 200, 100);
Aircraft a = new Aircraft(700, 200, 100);
mv.addVehicle(a);
} catch (BadSpeedSetting e) {
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
@@ -176,8 +184,7 @@ public class StartGrading {
public static void main(String[] args) {
Vehicle.testVehicle();
Car.testCar();
AirCraft.testAirCraft();
Aircraft.testAircraft();
Train.testTrain();
ManyVehicles.testVehicles();
}
}

View File

@@ -0,0 +1,17 @@
import java.awt.Graphics;
public interface IShape {
public int getX();
public int getY();
public void setX(int x);
public void setY(int y);
public boolean isVisible(int w, int h);
public boolean isIn(int x, int y);
public void draw(Graphics g);
}

View File

@@ -0,0 +1,51 @@
import java.awt.Color;
import java.awt.Graphics;
public abstract class Shape {
private int x;
private int y;
private Color color;
/**
* Constructor for Shape
*
* @param x
* the x coordinate of the shape
* @param y
* the y coordinate of the shape
*/
public Shape(int x, int y) {
this.x = x;
this.y = y;
color = new Color((float) Math.random(), (float) Math.random(), (float) Math.random());
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public abstract boolean isVisible(int w, int h);
public abstract boolean isIn(int x, int y);
public void draw(Graphics g) {
g.setColor(color);
}
public static void testShape() {
// TODO: implement this method
// Because this is the abstract so we don't test anything here.
}
}

View File

@@ -0,0 +1,5 @@
public class Start {
public static void main(String[] args) {
Shape.testShape();
}
}

View File

@@ -0,0 +1,89 @@
import java.awt.Graphics;
public class Bubble extends Shape {
private double radius;
/**
* Constructor for the Bubble class.
*
* @param x the x coordinate of the center of the bubble
* @param y the y coordinate of the center of the bubble
*/
public Bubble(int x, int y) {
super(x, y);
this.radius = 10;
}
// Find the point (wx, wy) inside the window which is closest to the
// center (x, y) of the circle. In other words, find the wx in the
// interval [0, w - 1] which is closest to x, and find the wy in the
// interval [0, h - 1] which is closest to y.
// If the distance between (wx, wy) and (x, y) is less than the radius
// of the circle (using Pythagoras's theorem) then at least part of
// the circle is visible in the window.
// Note: if the center of the circle is inside the window, then (wx, wy)
// is the same as (x, y), and the distance is 0.
@Override
public boolean isVisible(int w, int h) {
double x = getX();
double y = getY();
double wx = (x < 0 ? 0 : (x > w - 1 ? w - 1 : x));
double wy = (y < 0 ? 0 : (y > h - 1 ? h - 1 : y));
double dx = wx - x;
double dy = wy - y;
return dx * dx + dy * dy <= radius * radius;
}
/**
* Whether the point at xy is currently inside the bubble
*
* @param x the x coordinate of the point
* @param y the y coordinate of the point
* @return true if the point is inside the bubble, false otherwise
*/
@Override
public boolean isIn(int x, int y) {
double dx = x - getX();
double dy = y - getY();
return dx * dx + dy * dy < radius * radius;
}
/**
* Draw the bubble.
*
* @param g the graphics context
*/
@Override
public void draw(Graphics g) {
super.draw(g);
g.drawOval(
getX() - (int) radius,
getY() - (int) radius,
(int) radius * 2,
(int) radius * 2);
}
public static void testBubble() {
Bubble b = new Bubble(20, 30);
System.out.println(b.getX() == 20);
System.out.println(b.getY() == 30);
b.setX(40);
System.out.println(b.getX() == 40);
System.out.println(b.getY() == 30);
b.setY(60);
System.out.println(b.getX() == 40);
System.out.println(b.getY() == 60);
System.out.println(b.isVisible(100, 100) == true);
b.setX(50);
b.setY(0);
System.out.println(b.isVisible(100, 100) == true);
b.setX(99);
b.setY(50);
System.out.println(b.isVisible(100, 100) == true);
}
}

View File

@@ -0,0 +1,17 @@
import java.awt.Graphics;
public interface IShape {
public int getX();
public int getY();
public void setX(int x);
public void setY(int y);
public boolean isVisible(int w, int h);
public boolean isIn(int x, int y);
public void draw(Graphics g);
}

View File

@@ -0,0 +1,42 @@
import java.awt.Color;
import java.awt.Graphics;
public abstract class Shape {
private int x;
private int y;
private Color color;
public Shape(int x, int y) {
this.x = x;
this.y = y;
color = new Color((float) Math.random(), (float) Math.random(), (float) Math.random());
}
// getters and setters
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public abstract boolean isVisible(int w, int h);
public abstract boolean isIn(int x, int y);
public void draw(Graphics g) {
g.setColor(color);
}
public static void testShape() {
}
}

View File

@@ -0,0 +1,6 @@
public class Start {
public static void main(String[] args) {
Shape.testShape();
Bubble.testBubble();
}
}

View File

@@ -0,0 +1,65 @@
import java.awt.Graphics;
public class Bubble extends Shape implements IShape {
// instance variables
private double radius;
// constructor
public Bubble(int x, int y) {
super(x, y);
this.radius = 10;
}
// Find the point (wx, wy) inside the window which is closest to the
// center (x, y) of the circle. In other words, find the wx in the
// interval [0, w - 1] which is closest to x, and find the wy in the
// interval [0, h - 1] which is closest to y.
// If the distance between (wx, wy) and (x, y) is less than the radius
// of the circle (using Pythagoras's theorem) then at least part of
// the circle is visible in the window.
// Note: if the center of the circle is inside the window, then (wx, wy)
// is the same as (x, y), and the distance is 0.
@Override
public boolean isVisible(int w, int h) {
double x = getX();
double y = getY();
double wx = (x < 0 ? 0 : (x > w - 1 ? w - 1 : x));
double wy = (y < 0 ? 0 : (y > h - 1 ? h - 1 : y));
double dx = wx - x;
double dy = wy - y;
return dx * dx + dy * dy <= radius * radius;
}
@Override
public boolean isIn(int x, int y) {
double dx = x - getX();
double dy = y - getY();
return dx * dx + dy * dy < radius * radius;
}
@Override
public void draw(Graphics g) {
super.draw(g);
g.drawOval(
getX() - (int) radius,
getY() - (int) radius,
(int) radius * 2,
(int) radius * 2);
}
public static void testBubble() {
Bubble b = new Bubble(20, 30);
System.out.println(b.getX() == 20);
System.out.println(b.getY() == 30);
b.setX(40);
System.out.println(b.getX() == 40);
System.out.println(b.getY() == 30);
b.setY(60);
System.out.println(b.getX() == 40);
System.out.println(b.getY() == 60);
System.out.println(b.isVisible(100, 100) == true);
b.setX(50);
b.setY(0);
System.out.println(b.isVisible(100, 100) == true);
}
}

View File

@@ -0,0 +1,17 @@
import java.awt.Graphics;
public interface IShape {
public int getX();
public int getY();
public void setX(int x);
public void setY(int y);
public boolean isVisible(int w, int h);
public boolean isIn(int x, int y);
public void draw(Graphics g);
}

View File

@@ -0,0 +1,105 @@
import java.awt.Graphics;
import java.util.ArrayList;
public class Model {
// instance variables
private int score;
private ArrayList<IShape> bubbles;
// constructor
public Model() {
score = 0;
bubbles = new ArrayList<IShape>();
}
// getters and setters
public int getScore() {
return score;
}
/**
* @param w
* @param h
*/
public void addBubble(int w, int h) {
bubbles.add(new Bubble((int) (w * Math.random()), (int) (h * Math.random())));
}
/**
* @param dx
* @param dy
*/
public void moveAll(int dx, int dy) {
for (int i = 0; i < bubbles.size(); i++) {
bubbles.get(i).setX(bubbles.get(i).getX() + dx);
bubbles.get(i).setY(bubbles.get(i).getY() + dy);
}
}
/**
* clear all bubbles
*
* @param w
* @param h
*/
public void clearInvisibles(int w, int h) {
for (int i = bubbles.size() - 1; i >= 0; i--) {
if (!bubbles.get(i).isVisible(w, h)) {
bubbles.remove(i);
score--;
}
}
}
/**
* delete bubbles that are hit by the mouse
*
* @param x
* @param y
*/
public void deleteBubblesAtPoint(int x, int y) {
for (int i = bubbles.size() - 1; i >= 0; i--) {
if (bubbles.get(i).isIn(x, y)) {
bubbles.remove(i);
score++;
}
}
}
/**
* draw all bubbles
*
* @param g
*/
public void drawAll(Graphics g) {
for (IShape bubble : bubbles) {
bubble.draw(g);
}
}
public static void testModel() {
Model m = new Model();
System.out.println(m.getScore() == 0);
System.out.println(m.bubbles.size() == 0);
m.addBubble(100, 100);
m.addBubble(100, 100);
System.out.println(m.getScore() == 0);
System.out.println(m.bubbles.size() == 2);
m.clearInvisibles(200, 200);
System.out.println(m.getScore() == 0);
System.out.println(m.bubbles.size() == 2);
m.clearInvisibles(0, 0);
System.out.println(m.getScore() == -2);
System.out.println(m.bubbles.size() == 0);
m.addBubble(100, 100);
m.addBubble(100, 100);
System.out.println(m.getScore() == -2);
System.out.println(m.bubbles.size() == 2);
m.moveAll(200, 200);
System.out.println(m.getScore() == -2);
System.out.println(m.bubbles.size() == 2);
m.clearInvisibles(200, 200);
System.out.println(m.getScore() == -4);
System.out.println(m.bubbles.size() == 0);
}
}

View File

@@ -0,0 +1,41 @@
import java.awt.Color;
import java.awt.Graphics;
public abstract class Shape {
private int x;
private int y;
private Color color;
public Shape(int x, int y) {
this.x = x;
this.y = y;
color = new Color((float) Math.random(), (float) Math.random(), (float) Math.random());
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public abstract boolean isVisible(int w, int h);
public abstract boolean isIn(int x, int y);
public void draw(Graphics g) {
g.setColor(color);
}
public static void testShape() {
}
}

View File

@@ -0,0 +1,7 @@
public class Start {
public static void main(String[] args) {
Shape.testShape();
Bubble.testBubble();
Model.testModel();
}
}

View File

@@ -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);
}

View File

@@ -0,0 +1,5 @@
public class Test {
public static void main(String[] args) {
User.testUser();
}
}

View File

@@ -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);
}
}

View File

@@ -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)."));
}
}
}

View File

@@ -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;
}
}
}
}

View File

@@ -0,0 +1,7 @@
public class Controller {
protected Library m;
public Controller(Library m) {
this.m = m;
}
}

View File

@@ -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();
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,20 @@
public class GUI {
public static void main(String[] args) {
// anonymous class
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);
}
});
}
}

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -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<IUser> users;
private ArrayList<ModelListener> listeners;
/**
* Constructor.
*
* @param name
*/
public Library(String name) {
this.name = name;
// init array list
this.users = new ArrayList<IUser>();
this.listeners = new ArrayList<ModelListener>();
}
// 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
}
}

View File

@@ -0,0 +1,3 @@
public interface ModelListener {
public void update();
}

View File

@@ -0,0 +1,5 @@
public class NotALenderException extends Exception {
public NotALenderException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,8 @@
public class Test {
public static void main(String[] args) {
User.testUser();
Lender.testLender();
Borrower.testBorrower();
Library.testLibrary();
}
}

View File

@@ -0,0 +1,5 @@
public class UnknownUserException extends Exception {
public UnknownUserException(String message) {
super(message);
}
}

View File

@@ -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
}
}

View File

@@ -0,0 +1,18 @@
import javax.swing.JFrame;
public abstract class View<T extends Controller> 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();
}

View File

@@ -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<ControllerGetBook> {
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
}
}

View File

@@ -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<ControllerMoreBook> {
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() {
}
}

View File

@@ -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<ControllerSimple> {
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());
}
}

View File

@@ -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)."));
}
}
}

View File

@@ -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;
}
}
}
}

View File

@@ -0,0 +1,7 @@
public class Controller {
protected Library m;
public Controller(Library m) {
this.m = m;
}
}

View File

@@ -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();
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
});
}
}

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -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<IUser> users;
private ArrayList<ModelListener> listeners;
/**
* Constructor.
*
* @param name
*/
public Library(String name) {
this.name = name;
// init array list
this.users = new ArrayList<IUser>();
this.listeners = new ArrayList<ModelListener>();
}
// 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
}
}

View File

@@ -0,0 +1,3 @@
public interface ModelListener {
public void update();
}

View File

@@ -0,0 +1,5 @@
public class NotALenderException extends Exception {
public NotALenderException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,8 @@
public class Test {
public static void main(String[] args) {
User.testUser();
Lender.testLender();
Borrower.testBorrower();
Library.testLibrary();
}
}

View File

@@ -0,0 +1,5 @@
public class UnknownUserException extends Exception {
public UnknownUserException(String message) {
super(message);
}
}

View File

@@ -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
}
}

View File

@@ -0,0 +1,18 @@
import javax.swing.JFrame;
public abstract class View<T extends Controller> 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();
}

View File

@@ -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<ControllerCreate> {
private JTextField tName;
private JTextField tBook;
private JComboBox<String> 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<String>();
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() {
}
}

View File

@@ -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<ControllerGetBook> {
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
}
}

View File

@@ -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<ControllerMoreBook> {
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() {
}
}

View File

@@ -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<ControllerSimple> {
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());
}
}

View File

@@ -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)."));
}
}
}

View File

@@ -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;
}
}
}
}

View File

@@ -0,0 +1,7 @@
public class Controller {
protected Library m;
public Controller(Library m) {
this.m = m;
}
}

View File

@@ -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();
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
});
}
}

View File

@@ -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;
}

View File

@@ -0,0 +1 @@
giveup

View File

@@ -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);
}
}

View File

@@ -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<IUser> users;
private ArrayList<ModelListener> listeners;
/**
* Constructor.
*
* @param name
*/
public Library(String name) {
this.name = name;
// init array list
this.users = new ArrayList<IUser>();
this.listeners = new ArrayList<ModelListener>();
}
// 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
}
}

View File

@@ -0,0 +1,3 @@
public interface ModelListener {
public void update();
}

View File

@@ -0,0 +1,5 @@
public class NotALenderException extends Exception {
public NotALenderException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,8 @@
public class Test {
public static void main(String[] args) {
User.testUser();
Lender.testLender();
Borrower.testBorrower();
Library.testLibrary();
}
}

View File

@@ -0,0 +1,5 @@
public class UnknownUserException extends Exception {
public UnknownUserException(String message) {
super(message);
}
}

View File

@@ -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
}
}

View File

@@ -0,0 +1,18 @@
import javax.swing.JFrame;
public abstract class View<T extends Controller> 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();
}

View File

@@ -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<ControllerCreate> {
private JTextField tName;
private JTextField tBook;
private JComboBox<String> 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<String>();
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() {
}
}

View File

@@ -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<ControllerGetBook> {
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
}
}

View File

@@ -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<ControllerMoreBook> {
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() {
}
}

View File

@@ -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<ControllerSimple> {
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());
}
}

View File

@@ -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);
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,6 @@
public class Test {
public static void main(String[] args) {
User.testUser();
Lender.testLender();
}
}

View File

@@ -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);
}
}

View File

@@ -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)."));
}
}
}

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,5 @@
public class NotALenderException extends Exception {
public NotALenderException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,7 @@
public class Test {
public static void main(String[] args) {
User.testUser();
Lender.testLender();
Borrower.testBorrower();
}
}

View File

@@ -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
}
}

View File

@@ -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)."));
}
}
}

View File

@@ -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;
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,100 @@
import java.util.ArrayList;
public class Library {
private String name;
private ArrayList<IUser> users;
/**
* Constructor.
*
* @param name
*/
public Library(String name) {
this.name = name;
// init array list
users = new ArrayList<IUser>();
}
/**
* Add the user to the array list.
*
* @param user The user to be added
*/
public void addUser(IUser user) {
users.add(user);
}
/**
* 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);
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
}
}

View File

@@ -0,0 +1,5 @@
public class NotALenderException extends Exception {
public NotALenderException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,8 @@
public class Test {
public static void main(String[] args) {
User.testUser();
Lender.testLender();
Borrower.testBorrower();
Library.testLibrary();
}
}

View File

@@ -0,0 +1,5 @@
public class UnknownUserException extends Exception {
public UnknownUserException(String message) {
super(message);
}
}

View File

@@ -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
}
}

Some files were not shown because too many files have changed in this diff Show More