Compare commits
21 Commits
0b354c7561
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
5dfbf0b8ae
|
|||
|
a9f7967976
|
|||
|
d0416b02bd
|
|||
|
1357f94bfb
|
|||
|
88a587102f
|
|||
|
252d0c67bb
|
|||
|
621b5c355d
|
|||
|
16eacad6a1
|
|||
|
3dc6689804
|
|||
|
d3cf0c7a7c
|
|||
|
07ed06c619
|
|||
|
707750c9c8
|
|||
|
e2bd453330
|
|||
|
1c6ea20c0a
|
|||
|
6347507ede
|
|||
|
7adb69e3a1
|
|||
|
fa9befb1b1
|
|||
|
243a44f7ed
|
|||
|
855bc8cc3c
|
|||
|
053113577f
|
|||
|
c1acb5b92e
|
@@ -4,34 +4,32 @@ public class StartGrading {
|
|||||||
//The Vehicle class is abstract. We cannot create objects from this class. Therefore, we cannot test anything
|
//The Vehicle class is abstract. We cannot create objects from this class. Therefore, we cannot test anything
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void testCar() {
|
public static void testCar() {
|
||||||
// test case for constructors
|
// test case for constructors
|
||||||
try {
|
try {
|
||||||
// exception
|
// exception
|
||||||
Car c1 = new Car(70, 80);
|
Car c1 = new Car(70, 80);
|
||||||
} catch (BadSpeedSetting e) {
|
} 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 {
|
try {
|
||||||
// exception
|
// exception
|
||||||
Car c2 = new Car(70, -10);
|
Car c2 = new Car(70, -10);
|
||||||
} catch (BadSpeedSetting e) {
|
} catch (BadSpeedSetting e) {
|
||||||
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
|
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 {
|
try {
|
||||||
Car c3 = new Car(100, 80);
|
Car c3 = new Car(100, 80);
|
||||||
// test the canFly method
|
// test the canFly method
|
||||||
System.out.println(c3.canFly() == false);
|
System.out.println(c3.canFly() == false);
|
||||||
c3.accelerate(10);
|
c3.accelerate(10);
|
||||||
System.out.println(c3.getSpeed() == 90);
|
System.out.println(c3.getSpeed() == 90);
|
||||||
// test the accelerate with positive amount
|
// test the accelerate with positive amount
|
||||||
c3.accelerate(-20);
|
c3.accelerate(-20);
|
||||||
System.out.println(c3.getSpeed() == 70);
|
System.out.println(c3.getSpeed() == 70);
|
||||||
// test the accelerate with negative amount
|
// test the accelerate with negative amount
|
||||||
c3.accelerate(35);
|
c3.accelerate(35); // exception
|
||||||
// exception
|
|
||||||
} catch (BadSpeedSetting e) {
|
} catch (BadSpeedSetting e) {
|
||||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
} catch (ExceedSpeedLimit e) {
|
} catch (ExceedSpeedLimit e) {
|
||||||
@@ -42,7 +40,7 @@ public class StartGrading {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Car c4 = new Car(100, 80);
|
Car c4 = new Car(100, 80);
|
||||||
// exception
|
// exception
|
||||||
c4.accelerate(-90);
|
c4.accelerate(-90);
|
||||||
} catch (BadSpeedSetting e) {
|
} catch (BadSpeedSetting e) {
|
||||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
|
|||||||
@@ -7,30 +7,29 @@ public class StartGrading {
|
|||||||
public static void testCar() {
|
public static void testCar() {
|
||||||
// test case for constructors
|
// test case for constructors
|
||||||
try {
|
try {
|
||||||
// exception
|
// exception
|
||||||
Car c1 = new Car(70, 80);
|
Car c1 = new Car(70, 80);
|
||||||
} catch (BadSpeedSetting e) {
|
} 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 {
|
try {
|
||||||
// exception
|
// exception
|
||||||
Car c2 = new Car(70, -10);
|
Car c2 = new Car(70, -10);
|
||||||
} catch (BadSpeedSetting e) {
|
} catch (BadSpeedSetting e) {
|
||||||
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
|
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 {
|
try {
|
||||||
Car c3 = new Car(100, 80);
|
Car c3 = new Car(100, 80);
|
||||||
// test the canFly method
|
// test the canFly method
|
||||||
System.out.println(c3.canFly() == false);
|
System.out.println(c3.canFly() == false);
|
||||||
c3.accelerate(10);
|
c3.accelerate(10);
|
||||||
System.out.println(c3.getSpeed() == 90);
|
System.out.println(c3.getSpeed() == 90);
|
||||||
// test the accelerate with positive amount
|
// test the accelerate with positive amount
|
||||||
c3.accelerate(-20);
|
c3.accelerate(-20);
|
||||||
System.out.println(c3.getSpeed() == 70);
|
System.out.println(c3.getSpeed() == 70);
|
||||||
// test the accelerate with negative amount
|
// test the accelerate with negative amount
|
||||||
c3.accelerate(35);
|
c3.accelerate(35); // exception
|
||||||
// exception
|
|
||||||
} catch (BadSpeedSetting e) {
|
} catch (BadSpeedSetting e) {
|
||||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
} catch (ExceedSpeedLimit e) {
|
} catch (ExceedSpeedLimit e) {
|
||||||
@@ -41,7 +40,7 @@ public class StartGrading {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Car c4 = new Car(100, 80);
|
Car c4 = new Car(100, 80);
|
||||||
// exception
|
// exception
|
||||||
c4.accelerate(-90);
|
c4.accelerate(-90);
|
||||||
} catch (BadSpeedSetting e) {
|
} catch (BadSpeedSetting e) {
|
||||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
//Q4
|
||||||
public class StartGrading {
|
public class StartGrading {
|
||||||
public static void testVehicle() {
|
public static void testVehicle() {
|
||||||
//The Vehicle class is abstract. We cannot create objects from this class. Therefore, we cannot test anything
|
//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() {
|
public static void testCar() {
|
||||||
// test case for constructors
|
// test case for constructors
|
||||||
try {
|
try {
|
||||||
// exception
|
// exception
|
||||||
Car c1 = new Car(70, 80);
|
Car c1 = new Car(70, 80);
|
||||||
} catch (BadSpeedSetting e) {
|
} 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 {
|
try {
|
||||||
// exception
|
// exception
|
||||||
Car c2 = new Car(70, -10);
|
Car c2 = new Car(70, -10);
|
||||||
} catch (BadSpeedSetting e) {
|
} catch (BadSpeedSetting e) {
|
||||||
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
|
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 {
|
try {
|
||||||
Car c3 = new Car(100, 80);
|
Car c3 = new Car(100, 80);
|
||||||
// test the canFly method
|
// test the canFly method
|
||||||
System.out.println(c3.canFly() == false);
|
System.out.println(c3.canFly() == false);
|
||||||
c3.accelerate(10);
|
c3.accelerate(10);
|
||||||
System.out.println(c3.getSpeed() == 90);
|
System.out.println(c3.getSpeed() == 90);
|
||||||
// test the accelerate with positive amount
|
// test the accelerate with positive amount
|
||||||
c3.accelerate(-20);
|
c3.accelerate(-20);
|
||||||
System.out.println(c3.getSpeed() == 70);
|
System.out.println(c3.getSpeed() == 70);
|
||||||
// test the accelerate with negative amount
|
// test the accelerate with negative amount
|
||||||
c3.accelerate(35);
|
c3.accelerate(35); // exception
|
||||||
// exception
|
|
||||||
} catch (BadSpeedSetting e) {
|
} catch (BadSpeedSetting e) {
|
||||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
} catch (ExceedSpeedLimit e) {
|
} 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!"));
|
.println(e.getMessage().equals("Current speed is 70. Accelerate 35 will exceed the speed limit!"));
|
||||||
} catch (NotEnoughSpeed e) {
|
} catch (NotEnoughSpeed e) {
|
||||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
|
} catch (TrainSpeedChange e) {
|
||||||
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Car c4 = new Car(100, 80);
|
Car c4 = new Car(100, 80);
|
||||||
// exception
|
// exception
|
||||||
c4.accelerate(-90);
|
c4.accelerate(-90);
|
||||||
} catch (BadSpeedSetting e) {
|
} catch (BadSpeedSetting e) {
|
||||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
@@ -48,6 +50,8 @@ public class StartGrading {
|
|||||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
} catch (NotEnoughSpeed e) {
|
} catch (NotEnoughSpeed e) {
|
||||||
System.out.println(e.getMessage().equals("Current speed is " + 80 + ", not enough speed to decelerate!"));
|
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!"));
|
e.getMessage().equals("Current speed is 100. Accelerate 650 will exceed the speed limit!"));
|
||||||
} catch (NotEnoughSpeed e) {
|
} catch (NotEnoughSpeed e) {
|
||||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
|
} catch (TrainSpeedChange e) {
|
||||||
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Aircraft a5 = new Aircraft(700, 200, 100);
|
Aircraft a5 = new Aircraft(700, 200, 100);
|
||||||
@@ -114,6 +120,8 @@ public class StartGrading {
|
|||||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
} catch (NotEnoughSpeed e) {
|
} catch (NotEnoughSpeed e) {
|
||||||
System.out.println(e.getMessage().equals("Current speed is " + 200 + ", not enough speed to decelerate!"));
|
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
|
// exception
|
||||||
Train t1 = new Train(350, 400);
|
Train t1 = new Train(350, 400);
|
||||||
} catch (BadSpeedSetting e) {
|
} 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 {
|
try {
|
||||||
// exception
|
// exception
|
||||||
@@ -150,7 +158,7 @@ public class StartGrading {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Vehicle.testVehicle();
|
Vehicle.testVehicle();
|
||||||
Car.testCar();
|
Car.testCar();
|
||||||
AirCraft.testAirCraft();
|
Aircraft.testAircraft();
|
||||||
Train.testTrain();
|
Train.testTrain();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ public class ManyVehicles {
|
|||||||
*
|
*
|
||||||
* @return the average speed of all vehicles
|
* @return the average speed of all vehicles
|
||||||
*/
|
*/
|
||||||
public double averageSpeed() {
|
public double calcAvgSpeed() {
|
||||||
double totalSpeed = 0;
|
double totalSpeed = 0;
|
||||||
for (Vehicle v : vehicles) {
|
for (Vehicle v : vehicles) {
|
||||||
totalSpeed += v.getSpeed();
|
totalSpeed += v.getSpeed();
|
||||||
@@ -50,7 +50,7 @@ public class ManyVehicles {
|
|||||||
mv.addVehicle(new Aircraft(100, 70, 3900));
|
mv.addVehicle(new Aircraft(100, 70, 3900));
|
||||||
mv.addVehicle(new Train(100, 60));
|
mv.addVehicle(new Train(100, 60));
|
||||||
|
|
||||||
System.out.println(mv.averageSpeed() == 80.0);
|
System.out.println(mv.calcAvgSpeed() == 80.0);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println("Error uncaught exception: " + e.getClass().getName() + e.getMessage());
|
System.out.println("Error uncaught exception: " + e.getClass().getName() + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
//Q5
|
||||||
public class StartGrading {
|
public class StartGrading {
|
||||||
public static void testVehicle() {
|
public static void testVehicle() {
|
||||||
//The Vehicle class is abstract. We cannot create objects from this class. Therefore, we cannot test anything
|
//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() {
|
public static void testCar() {
|
||||||
// test case for constructors
|
// test case for constructors
|
||||||
try {
|
try {
|
||||||
// exception
|
// exception
|
||||||
Car c1 = new Car(70, 80);
|
Car c1 = new Car(70, 80);
|
||||||
} catch (BadSpeedSetting e) {
|
} 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 {
|
try {
|
||||||
// exception
|
// exception
|
||||||
Car c2 = new Car(70, -10);
|
Car c2 = new Car(70, -10);
|
||||||
} catch (BadSpeedSetting e) {
|
} catch (BadSpeedSetting e) {
|
||||||
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
|
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 {
|
try {
|
||||||
Car c3 = new Car(100, 80);
|
Car c3 = new Car(100, 80);
|
||||||
// test the canFly method
|
// test the canFly method
|
||||||
System.out.println(c3.canFly() == false);
|
System.out.println(c3.canFly() == false);
|
||||||
c3.accelerate(10);
|
c3.accelerate(10);
|
||||||
System.out.println(c3.getSpeed() == 90);
|
System.out.println(c3.getSpeed() == 90);
|
||||||
// test the accelerate with positive amount
|
// test the accelerate with positive amount
|
||||||
c3.accelerate(-20);
|
c3.accelerate(-20);
|
||||||
System.out.println(c3.getSpeed() == 70);
|
System.out.println(c3.getSpeed() == 70);
|
||||||
// test the accelerate with negative amount
|
// test the accelerate with negative amount
|
||||||
c3.accelerate(35);
|
c3.accelerate(35); // exception
|
||||||
// exception
|
|
||||||
} catch (BadSpeedSetting e) {
|
} catch (BadSpeedSetting e) {
|
||||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
} catch (ExceedSpeedLimit e) {
|
} 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!"));
|
.println(e.getMessage().equals("Current speed is 70. Accelerate 35 will exceed the speed limit!"));
|
||||||
} catch (NotEnoughSpeed e) {
|
} catch (NotEnoughSpeed e) {
|
||||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
|
} catch (TrainSpeedChange e) {
|
||||||
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Car c4 = new Car(100, 80);
|
Car c4 = new Car(100, 80);
|
||||||
// exception
|
// exception
|
||||||
c4.accelerate(-90);
|
c4.accelerate(-90);
|
||||||
} catch (BadSpeedSetting e) {
|
} catch (BadSpeedSetting e) {
|
||||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
@@ -48,6 +50,8 @@ public class StartGrading {
|
|||||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
} catch (NotEnoughSpeed e) {
|
} catch (NotEnoughSpeed e) {
|
||||||
System.out.println(e.getMessage().equals("Current speed is " + 80 + ", not enough speed to decelerate!"));
|
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!"));
|
e.getMessage().equals("Current speed is 100. Accelerate 650 will exceed the speed limit!"));
|
||||||
} catch (NotEnoughSpeed e) {
|
} catch (NotEnoughSpeed e) {
|
||||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
|
} catch (TrainSpeedChange e) {
|
||||||
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
Aircraft a5 = new Aircraft(700, 200, 100);
|
Aircraft a5 = new Aircraft(700, 200, 100);
|
||||||
@@ -114,6 +120,8 @@ public class StartGrading {
|
|||||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
} catch (NotEnoughSpeed e) {
|
} catch (NotEnoughSpeed e) {
|
||||||
System.out.println(e.getMessage().equals("Current speed is " + 200 + ", not enough speed to decelerate!"));
|
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
|
// exception
|
||||||
Train t1 = new Train(350, 400);
|
Train t1 = new Train(350, 400);
|
||||||
} catch (BadSpeedSetting e) {
|
} 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 {
|
try {
|
||||||
// exception
|
// exception
|
||||||
@@ -149,7 +157,7 @@ public class StartGrading {
|
|||||||
|
|
||||||
public static void testManyVehicles() {
|
public static void testManyVehicles() {
|
||||||
ManyVehicles mv = new ManyVehicles();
|
ManyVehicles mv = new ManyVehicles();
|
||||||
// Add any kind of vehicles to the arraylist.
|
// Add any kind of vehicles to the arraylist.
|
||||||
try {
|
try {
|
||||||
Train t = new Train(350, 300);
|
Train t = new Train(350, 300);
|
||||||
mv.addVehicle(t);
|
mv.addVehicle(t);
|
||||||
@@ -163,7 +171,7 @@ public class StartGrading {
|
|||||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
AirCraft a = new AirCraft(700, 200, 100);
|
Aircraft a = new Aircraft(700, 200, 100);
|
||||||
mv.addVehicle(a);
|
mv.addVehicle(a);
|
||||||
} catch (BadSpeedSetting e) {
|
} catch (BadSpeedSetting e) {
|
||||||
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
System.out.println("BUG! THIS MUST NEVER HAPPEN!");
|
||||||
@@ -176,8 +184,7 @@ public class StartGrading {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Vehicle.testVehicle();
|
Vehicle.testVehicle();
|
||||||
Car.testCar();
|
Car.testCar();
|
||||||
AirCraft.testAirCraft();
|
Aircraft.testAircraft();
|
||||||
Train.testTrain();
|
Train.testTrain();
|
||||||
ManyVehicles.testVehicles();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
17
assignment6/Question1/IShape.java
Normal file
17
assignment6/Question1/IShape.java
Normal 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);
|
||||||
|
}
|
||||||
51
assignment6/Question1/Shape.java
Normal file
51
assignment6/Question1/Shape.java
Normal 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.
|
||||||
|
}
|
||||||
|
}
|
||||||
5
assignment6/Question1/Start.java
Normal file
5
assignment6/Question1/Start.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
public class Start {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Shape.testShape();
|
||||||
|
}
|
||||||
|
}
|
||||||
89
assignment6/Question2/Bubble.java
Normal file
89
assignment6/Question2/Bubble.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
17
assignment6/Question2/IShape.java
Normal file
17
assignment6/Question2/IShape.java
Normal 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);
|
||||||
|
}
|
||||||
42
assignment6/Question2/Shape.java
Normal file
42
assignment6/Question2/Shape.java
Normal 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() {
|
||||||
|
}
|
||||||
|
}
|
||||||
6
assignment6/Question2/Start.java
Normal file
6
assignment6/Question2/Start.java
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
public class Start {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Shape.testShape();
|
||||||
|
Bubble.testBubble();
|
||||||
|
}
|
||||||
|
}
|
||||||
65
assignment6/Question3/Bubble.java
Normal file
65
assignment6/Question3/Bubble.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
17
assignment6/Question3/IShape.java
Normal file
17
assignment6/Question3/IShape.java
Normal 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);
|
||||||
|
}
|
||||||
105
assignment6/Question3/Model.java
Normal file
105
assignment6/Question3/Model.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
41
assignment6/Question3/Shape.java
Normal file
41
assignment6/Question3/Shape.java
Normal 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() {
|
||||||
|
}
|
||||||
|
}
|
||||||
7
assignment6/Question3/Start.java
Normal file
7
assignment6/Question3/Start.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
public class Start {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Shape.testShape();
|
||||||
|
Bubble.testBubble();
|
||||||
|
Model.testModel();
|
||||||
|
}
|
||||||
|
}
|
||||||
13
finalproject/Question1/IUser.java
Normal file
13
finalproject/Question1/IUser.java
Normal 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);
|
||||||
|
}
|
||||||
5
finalproject/Question1/Test.java
Normal file
5
finalproject/Question1/Test.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
public class Test {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
User.testUser();
|
||||||
|
}
|
||||||
|
}
|
||||||
78
finalproject/Question1/User.java
Normal file
78
finalproject/Question1/User.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
63
finalproject/Question10/Borrower.java
Normal file
63
finalproject/Question10/Borrower.java
Normal 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)."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
145
finalproject/Question10/CLI.java
Normal file
145
finalproject/Question10/CLI.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
7
finalproject/Question10/Controller.java
Normal file
7
finalproject/Question10/Controller.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
public class Controller {
|
||||||
|
protected Library m;
|
||||||
|
|
||||||
|
public Controller(Library m) {
|
||||||
|
this.m = m;
|
||||||
|
}
|
||||||
|
}
|
||||||
15
finalproject/Question10/ControllerGetBook.java
Normal file
15
finalproject/Question10/ControllerGetBook.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
27
finalproject/Question10/ControllerMoreBook.java
Normal file
27
finalproject/Question10/ControllerMoreBook.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
12
finalproject/Question10/ControllerSimple.java
Normal file
12
finalproject/Question10/ControllerSimple.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
20
finalproject/Question10/GUI.java
Normal file
20
finalproject/Question10/GUI.java
Normal 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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
13
finalproject/Question10/IUser.java
Normal file
13
finalproject/Question10/IUser.java
Normal 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;
|
||||||
|
}
|
||||||
42
finalproject/Question10/Lender.java
Normal file
42
finalproject/Question10/Lender.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
122
finalproject/Question10/Library.java
Normal file
122
finalproject/Question10/Library.java
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
3
finalproject/Question10/ModelListener.java
Normal file
3
finalproject/Question10/ModelListener.java
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
public interface ModelListener {
|
||||||
|
public void update();
|
||||||
|
}
|
||||||
5
finalproject/Question10/NotALenderException.java
Normal file
5
finalproject/Question10/NotALenderException.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
public class NotALenderException extends Exception {
|
||||||
|
public NotALenderException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
8
finalproject/Question10/Test.java
Normal file
8
finalproject/Question10/Test.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
public class Test {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
User.testUser();
|
||||||
|
Lender.testLender();
|
||||||
|
Borrower.testBorrower();
|
||||||
|
Library.testLibrary();
|
||||||
|
}
|
||||||
|
}
|
||||||
5
finalproject/Question10/UnknownUserException.java
Normal file
5
finalproject/Question10/UnknownUserException.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
public class UnknownUserException extends Exception {
|
||||||
|
public UnknownUserException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
69
finalproject/Question10/User.java
Normal file
69
finalproject/Question10/User.java
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
18
finalproject/Question10/View.java
Normal file
18
finalproject/Question10/View.java
Normal 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();
|
||||||
|
}
|
||||||
40
finalproject/Question10/ViewGetBook.java
Normal file
40
finalproject/Question10/ViewGetBook.java
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
52
finalproject/Question10/ViewMoreBook.java
Normal file
52
finalproject/Question10/ViewMoreBook.java
Normal 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() {
|
||||||
|
}
|
||||||
|
}
|
||||||
29
finalproject/Question10/ViewSimple.java
Normal file
29
finalproject/Question10/ViewSimple.java
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
63
finalproject/Question11/Borrower.java
Normal file
63
finalproject/Question11/Borrower.java
Normal 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)."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
145
finalproject/Question11/CLI.java
Normal file
145
finalproject/Question11/CLI.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
7
finalproject/Question11/Controller.java
Normal file
7
finalproject/Question11/Controller.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
public class Controller {
|
||||||
|
protected Library m;
|
||||||
|
|
||||||
|
public Controller(Library m) {
|
||||||
|
this.m = m;
|
||||||
|
}
|
||||||
|
}
|
||||||
27
finalproject/Question11/ControllerCreate.java
Normal file
27
finalproject/Question11/ControllerCreate.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
15
finalproject/Question11/ControllerGetBook.java
Normal file
15
finalproject/Question11/ControllerGetBook.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
27
finalproject/Question11/ControllerMoreBook.java
Normal file
27
finalproject/Question11/ControllerMoreBook.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
12
finalproject/Question11/ControllerSimple.java
Normal file
12
finalproject/Question11/ControllerSimple.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
21
finalproject/Question11/GUI.java
Normal file
21
finalproject/Question11/GUI.java
Normal 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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
13
finalproject/Question11/IUser.java
Normal file
13
finalproject/Question11/IUser.java
Normal 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;
|
||||||
|
}
|
||||||
42
finalproject/Question11/Lender.java
Normal file
42
finalproject/Question11/Lender.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
122
finalproject/Question11/Library.java
Normal file
122
finalproject/Question11/Library.java
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
3
finalproject/Question11/ModelListener.java
Normal file
3
finalproject/Question11/ModelListener.java
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
public interface ModelListener {
|
||||||
|
public void update();
|
||||||
|
}
|
||||||
5
finalproject/Question11/NotALenderException.java
Normal file
5
finalproject/Question11/NotALenderException.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
public class NotALenderException extends Exception {
|
||||||
|
public NotALenderException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
8
finalproject/Question11/Test.java
Normal file
8
finalproject/Question11/Test.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
public class Test {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
User.testUser();
|
||||||
|
Lender.testLender();
|
||||||
|
Borrower.testBorrower();
|
||||||
|
Library.testLibrary();
|
||||||
|
}
|
||||||
|
}
|
||||||
5
finalproject/Question11/UnknownUserException.java
Normal file
5
finalproject/Question11/UnknownUserException.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
public class UnknownUserException extends Exception {
|
||||||
|
public UnknownUserException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
69
finalproject/Question11/User.java
Normal file
69
finalproject/Question11/User.java
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
18
finalproject/Question11/View.java
Normal file
18
finalproject/Question11/View.java
Normal 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();
|
||||||
|
}
|
||||||
60
finalproject/Question11/ViewCreate.java
Normal file
60
finalproject/Question11/ViewCreate.java
Normal 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() {
|
||||||
|
}
|
||||||
|
}
|
||||||
40
finalproject/Question11/ViewGetBook.java
Normal file
40
finalproject/Question11/ViewGetBook.java
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
52
finalproject/Question11/ViewMoreBook.java
Normal file
52
finalproject/Question11/ViewMoreBook.java
Normal 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() {
|
||||||
|
}
|
||||||
|
}
|
||||||
29
finalproject/Question11/ViewSimple.java
Normal file
29
finalproject/Question11/ViewSimple.java
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
63
finalproject/Question12/Borrower.java
Normal file
63
finalproject/Question12/Borrower.java
Normal 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)."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
145
finalproject/Question12/CLI.java
Normal file
145
finalproject/Question12/CLI.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
7
finalproject/Question12/Controller.java
Normal file
7
finalproject/Question12/Controller.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
public class Controller {
|
||||||
|
protected Library m;
|
||||||
|
|
||||||
|
public Controller(Library m) {
|
||||||
|
this.m = m;
|
||||||
|
}
|
||||||
|
}
|
||||||
27
finalproject/Question12/ControllerCreate.java
Normal file
27
finalproject/Question12/ControllerCreate.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
15
finalproject/Question12/ControllerGetBook.java
Normal file
15
finalproject/Question12/ControllerGetBook.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
27
finalproject/Question12/ControllerMoreBook.java
Normal file
27
finalproject/Question12/ControllerMoreBook.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
12
finalproject/Question12/ControllerSimple.java
Normal file
12
finalproject/Question12/ControllerSimple.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
21
finalproject/Question12/GUI.java
Normal file
21
finalproject/Question12/GUI.java
Normal 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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
13
finalproject/Question12/IUser.java
Normal file
13
finalproject/Question12/IUser.java
Normal 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;
|
||||||
|
}
|
||||||
1
finalproject/Question12/I_GIVE_UP.LOG
Normal file
1
finalproject/Question12/I_GIVE_UP.LOG
Normal file
@@ -0,0 +1 @@
|
|||||||
|
giveup
|
||||||
42
finalproject/Question12/Lender.java
Normal file
42
finalproject/Question12/Lender.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
122
finalproject/Question12/Library.java
Normal file
122
finalproject/Question12/Library.java
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
3
finalproject/Question12/ModelListener.java
Normal file
3
finalproject/Question12/ModelListener.java
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
public interface ModelListener {
|
||||||
|
public void update();
|
||||||
|
}
|
||||||
5
finalproject/Question12/NotALenderException.java
Normal file
5
finalproject/Question12/NotALenderException.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
public class NotALenderException extends Exception {
|
||||||
|
public NotALenderException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
8
finalproject/Question12/Test.java
Normal file
8
finalproject/Question12/Test.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
public class Test {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
User.testUser();
|
||||||
|
Lender.testLender();
|
||||||
|
Borrower.testBorrower();
|
||||||
|
Library.testLibrary();
|
||||||
|
}
|
||||||
|
}
|
||||||
5
finalproject/Question12/UnknownUserException.java
Normal file
5
finalproject/Question12/UnknownUserException.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
public class UnknownUserException extends Exception {
|
||||||
|
public UnknownUserException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
69
finalproject/Question12/User.java
Normal file
69
finalproject/Question12/User.java
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
18
finalproject/Question12/View.java
Normal file
18
finalproject/Question12/View.java
Normal 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();
|
||||||
|
}
|
||||||
60
finalproject/Question12/ViewCreate.java
Normal file
60
finalproject/Question12/ViewCreate.java
Normal 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() {
|
||||||
|
}
|
||||||
|
}
|
||||||
40
finalproject/Question12/ViewGetBook.java
Normal file
40
finalproject/Question12/ViewGetBook.java
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
52
finalproject/Question12/ViewMoreBook.java
Normal file
52
finalproject/Question12/ViewMoreBook.java
Normal 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() {
|
||||||
|
}
|
||||||
|
}
|
||||||
29
finalproject/Question12/ViewSimple.java
Normal file
29
finalproject/Question12/ViewSimple.java
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
13
finalproject/Question2/IUser.java
Normal file
13
finalproject/Question2/IUser.java
Normal 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);
|
||||||
|
}
|
||||||
42
finalproject/Question2/Lender.java
Normal file
42
finalproject/Question2/Lender.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
6
finalproject/Question2/Test.java
Normal file
6
finalproject/Question2/Test.java
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
public class Test {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
User.testUser();
|
||||||
|
Lender.testLender();
|
||||||
|
}
|
||||||
|
}
|
||||||
78
finalproject/Question2/User.java
Normal file
78
finalproject/Question2/User.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
63
finalproject/Question3/Borrower.java
Normal file
63
finalproject/Question3/Borrower.java
Normal 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)."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
13
finalproject/Question3/IUser.java
Normal file
13
finalproject/Question3/IUser.java
Normal 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;
|
||||||
|
}
|
||||||
42
finalproject/Question3/Lender.java
Normal file
42
finalproject/Question3/Lender.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
5
finalproject/Question3/NotALenderException.java
Normal file
5
finalproject/Question3/NotALenderException.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
public class NotALenderException extends Exception {
|
||||||
|
public NotALenderException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
7
finalproject/Question3/Test.java
Normal file
7
finalproject/Question3/Test.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
public class Test {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
User.testUser();
|
||||||
|
Lender.testLender();
|
||||||
|
Borrower.testBorrower();
|
||||||
|
}
|
||||||
|
}
|
||||||
69
finalproject/Question3/User.java
Normal file
69
finalproject/Question3/User.java
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
63
finalproject/Question4/Borrower.java
Normal file
63
finalproject/Question4/Borrower.java
Normal 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)."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
13
finalproject/Question4/IUser.java
Normal file
13
finalproject/Question4/IUser.java
Normal 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;
|
||||||
|
}
|
||||||
42
finalproject/Question4/Lender.java
Normal file
42
finalproject/Question4/Lender.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
100
finalproject/Question4/Library.java
Normal file
100
finalproject/Question4/Library.java
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
5
finalproject/Question4/NotALenderException.java
Normal file
5
finalproject/Question4/NotALenderException.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
public class NotALenderException extends Exception {
|
||||||
|
public NotALenderException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
8
finalproject/Question4/Test.java
Normal file
8
finalproject/Question4/Test.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
public class Test {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
User.testUser();
|
||||||
|
Lender.testLender();
|
||||||
|
Borrower.testBorrower();
|
||||||
|
Library.testLibrary();
|
||||||
|
}
|
||||||
|
}
|
||||||
5
finalproject/Question4/UnknownUserException.java
Normal file
5
finalproject/Question4/UnknownUserException.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
public class UnknownUserException extends Exception {
|
||||||
|
public UnknownUserException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
69
finalproject/Question4/User.java
Normal file
69
finalproject/Question4/User.java
Normal 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
Reference in New Issue
Block a user