Hajj-simulation/src/CivilVehicle.java
HeshamTB b70bad851e Use encapsulation principal in CivilVehicle
Since every occurrence of collide should set
	currentAccident and broken for all cars, use
	setCurrentAccident() for both cars in collide()
	which sets both values.

Signed-off-by: HeshamTB <hishaminv@gmail.com>
2020-11-07 12:35:35 +03:00

60 lines
1.4 KiB
Java

import java.util.Date;
public abstract class CivilVehicle extends Vehicle implements Breakable {
private boolean broken;
private Accident currentAccident;
private Date breakDownTime;
public CivilVehicle(double vehicleSize) {
super(vehicleSize);
}
@Override
public boolean isBroken() {
return broken;
}
@Override
public Accident collide(Breakable car, Date time, Street location) {
if (currentAccident == null) {
Breakable[] cars = new Breakable[2];
cars[0] = this;
cars[1] = car;
Accident accident = new Accident(time, cars, location);
this.setCurrentAccident(accident);
if (car instanceof CivilVehicle)
((CivilVehicle)car).setCurrentAccident(accident);
return accident;
}
return null;
}
@Override
public void _break(Date time) {
broken = true;
this.breakDownTime = time;
}
@Override
public void fix() {
broken = false;
this.currentAccident = null;
}
public Date getBreakDownTime() {
return breakDownTime;
}
public Accident getCurrentAccident() {
return currentAccident;
}
public void setCurrentAccident(Accident accident){
if (accident != null) {
this.currentAccident = accident;
this._break(accident.getDate());
}
}
}