Files
oop/assignment5/Question3/Vehicle.java
2022-04-24 13:24:47 +08:00

181 lines
5.1 KiB
Java

/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-24
* Description: This is the abstract Vehicle class.
*/
public abstract class Vehicle implements Movable {
private int speedLimit;
private int speed;
/**
* Constructor.
*
* @param speedLimit the speed limit of the vehicle
* @param speed the speed of the vehicle
* @throws BadSpeedSetting if speedLimit or speed is negative. Or if the speed
* is greater than speed limit.
*/
public Vehicle(int speedLimit, int speed) throws BadSpeedSetting {
if (speedLimit < 0 || speed < 0) {
throw new BadSpeedSetting("Speed cannot be negative!");
}
if (speed > speedLimit) {
throw new BadSpeedSetting("Speed cannot be greater than speed limit!");
}
this.speedLimit = speedLimit;
this.speed = speed;
}
/**
* Getter of speed.
*
* @return the speed of the vehicle
*/
public int getSpeed() {
return speed;
}
/**
* Accelerate the vehicle.
*
* @param amount the amount of acceleration.
* @throws ExceedSpeedLimit if the speed is greater than speed limit after
* increased.
* @thorws NotEnoughSpeed if the speed is less than 0 after decreased.
* @return the new speed of the vehicle
*/
public int accelerate(int amount) throws ExceedSpeedLimit, NotEnoughSpeed {
int newSpeed = speed + amount;
if (newSpeed > speedLimit) {
throw new ExceedSpeedLimit(
String.format("Current speed is %d. Accelerate %d will exceed the speed limit!",
speed, amount));
}
if (newSpeed < 0) {
throw new NotEnoughSpeed(
String.format("Current speed is %d, not enough speed to decelerate!", speed));
}
speed = newSpeed;
return speed;
}
/**
* Whether the vehicle can fly or not.
*
* @return whether the vehicle can fly.
*/
public abstract boolean canFly();
/**
* Test.
*/
public static void testVehicle() {
try {
Vehicle car;
try {
car = new Vehicle(-10, -20) {
@Override
public boolean canFly() {
return false;
}
};
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
}
try {
car = new Vehicle(0, 50) {
@Override
public boolean canFly() {
return false;
}
};
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
}
car = new Vehicle(100, 50) {
@Override
public boolean canFly() {
return false;
}
};
System.out.println(car.getSpeed() == 50);
System.out.println(car.canFly() == false);
System.out.println(car.accelerate(10) == 60);
System.out.println(car.accelerate(10) == 70);
System.out.println(car.accelerate(10) == 80);
System.out.println(car.accelerate(10) == 90);
System.out.println(car.accelerate(10) == 100);
try {
System.out.println(car.accelerate(10) == 110);
System.out.println("Error: ExceedSpeedLimit should be thrown!");
} catch (ExceedSpeedLimit e) {
System.out.println(e.getMessage().equals(
"Current speed is 100. Accelerate 10 will exceed the speed limit!"));
}
System.out.println(car.accelerate(-50) == 50);
System.out.println(car.accelerate(-50) == 0);
try {
System.out.println(car.accelerate(-50) == -50);
System.out.println("Error: NotEnoughSpeed should be thrown!");
} catch (NotEnoughSpeed e) {
System.out.println(e.getMessage()
.equals("Current speed is 0, not enough speed to decelerate!"));
}
Vehicle plane;
try {
plane = new Vehicle(-10, -20) {
@Override
public boolean canFly() {
return true;
}
};
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be negative!"));
}
try {
plane = new Vehicle(0, 50) {
@Override
public boolean canFly() {
return true;
}
};
} catch (BadSpeedSetting e) {
System.out.println(e.getMessage().equals("Speed cannot be greater than speed limit!"));
}
plane = new Vehicle(100, 50) {
@Override
public boolean canFly() {
return true;
}
};
System.out.println(plane.getSpeed() == 50);
System.out.println(plane.canFly() == true);
System.out.println(plane.accelerate(10) == 60);
System.out.println(plane.accelerate(10) == 70);
System.out.println(plane.accelerate(10) == 80);
System.out.println(plane.accelerate(10) == 90);
System.out.println(plane.accelerate(10) == 100);
try {
System.out.println(plane.accelerate(10) == 110);
System.out.println("Error: ExceedSpeedLimit should be thrown!");
} catch (ExceedSpeedLimit e) {
System.out.println(e.getMessage().equals(
"Current speed is 100. Accelerate 10 will exceed the speed limit!"));
}
System.out.println(plane.accelerate(-50) == 50);
System.out.println(plane.accelerate(-50) == 0);
try {
System.out.println(plane.accelerate(-50) == -50);
System.out.println("Error: NotEnoughSpeed should be thrown!");
} catch (NotEnoughSpeed e) {
System.out.println(e.getMessage()
.equals("Current speed is 0, not enough speed to decelerate!"));
}
} catch (Exception e) {
System.out.println("Error: Uncatched exception!");
}
}
}