Restructer and add CanFixAccident
- Move Capacity field to Vehicle class - Improve Breakable interface
This commit is contained in:
parent
58dfb37437
commit
de0331088f
@ -1,7 +1,11 @@
|
||||
import java.util.Date;
|
||||
|
||||
public interface Breakable {
|
||||
|
||||
public int getTimeToFix();
|
||||
public boolean isBroken();
|
||||
public boolean isInAccident();
|
||||
int getTimeToFix();
|
||||
boolean isBroken();
|
||||
boolean isInAccident();
|
||||
void collide(Breakable car, Date time);
|
||||
void _break(Date time);
|
||||
|
||||
}
|
||||
|
3
src/CanFixAccident.java
Normal file
3
src/CanFixAccident.java
Normal file
@ -0,0 +1,3 @@
|
||||
public interface CanFixAccident {
|
||||
//void attendToAccident(Accident accident);
|
||||
}
|
@ -1,22 +1,21 @@
|
||||
import java.util.Date;
|
||||
|
||||
public class Sedan extends Vehicle implements Breakable {
|
||||
|
||||
private final int TIME_TO_FIX = 15; //in minutes
|
||||
private boolean broken;
|
||||
private boolean accident;
|
||||
private int capacity;
|
||||
|
||||
public Sedan(double vehicleSize, boolean govtCar){
|
||||
super(vehicleSize, govtCar);
|
||||
capacity = 4; //Should make this attr. in vehicle.
|
||||
super(vehicleSize, govtCar, 4);
|
||||
}
|
||||
|
||||
public boolean isBroken(){ return broken; }
|
||||
public boolean isInAccident(){ return accident; }
|
||||
public void _break(Date time) { this.broken = true; } //TODO
|
||||
public void collide(Breakable car, Date time) { this.accident = true; }//TODO //Maybe add time of accident and other breakable args
|
||||
public int getTimeToFix(){ return TIME_TO_FIX; }
|
||||
public int getCapacity() { return capacity; }
|
||||
|
||||
public void collide() { this.accident = true; }
|
||||
public void _break() { this.broken = true; }
|
||||
|
||||
public void fixed() { this.broken = false; this.accident = false; }
|
||||
|
||||
|
@ -1,9 +1,21 @@
|
||||
public class TrafficPoliceCar extends Vehicle {
|
||||
public class TrafficPoliceCar extends Vehicle implements CanFixAccident {
|
||||
|
||||
private final double ADDED_EFFICIENCY = 0.05; // 5%
|
||||
public final double ADDED_EFFICIENCY = 0.05; // 5%
|
||||
//private Street str_location or name
|
||||
private static int totalTrafficCars;
|
||||
|
||||
public TrafficPoliceCar(double vehicleSize, boolean govtCar){
|
||||
super(vehicleSize, govtCar);
|
||||
super(vehicleSize, govtCar, 2); //Capacity is irrelevant.
|
||||
totalTrafficCars++;
|
||||
}
|
||||
|
||||
public static int getTotalTrafficCars() {
|
||||
return totalTrafficCars;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void finalize() throws Throwable {
|
||||
super.finalize();
|
||||
totalTrafficCars--;
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,12 @@ public abstract class Vehicle {
|
||||
|
||||
private double vehicleSize;
|
||||
private boolean govtCar;
|
||||
private int capacity;
|
||||
|
||||
public Vehicle(double vehicleSize, boolean govtCar){
|
||||
public Vehicle(double vehicleSize, boolean govtCar, int capacity){
|
||||
this.vehicleSize = vehicleSize;
|
||||
this.govtCar = govtCar;
|
||||
setCapacity(capacity);
|
||||
}
|
||||
public double getVehicleSize() {
|
||||
return vehicleSize;
|
||||
@ -14,4 +16,15 @@ public abstract class Vehicle {
|
||||
public boolean isGovtCar() {
|
||||
return govtCar;
|
||||
}
|
||||
|
||||
public int getCapacity() {
|
||||
return capacity;
|
||||
}
|
||||
|
||||
private void setCapacity(int capacity) throws IllegalArgumentException {
|
||||
if (capacity > 0 && capacity < 50){
|
||||
this.capacity = capacity;
|
||||
}
|
||||
else throw new IllegalArgumentException(String.format("Invalid vehicle capacity %d", capacity));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user