Remake Vehicle classes

- Abstract class CivilVehicle that implements Breakable and
	contains all common fields of vehicles. Now for example,
	Sedan <- CivilVehicle <- Vehicle
		      ^
		  Breakable
	The breakable methods are mostly implemented in CivilVehicle
	Except getTimeToFix() which is defined in subclasses of
	CivilVehicle.

	- Breakable interface:
	The method collide() now returns instance of Accident instead of
	void

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2020-10-21 11:35:19 +03:00
parent 1b8b05cbb5
commit eb94de2754
4 changed files with 61 additions and 51 deletions

View File

@ -5,7 +5,7 @@ public interface Breakable {
int getTimeToFix();
boolean isBroken();
boolean isInAccident();
void collide(Breakable car, Date time);
Accident collide(Breakable car, Date time);
void _break(Date time);
void fix();

View File

@ -1,11 +1,8 @@
import java.util.Date;
public class Bus extends Vehicle implements Breakable {
public class Bus extends CivilVehicle {
private final int TIME_TO_FIX = 20; //in minutes
private boolean broken;
private boolean inAccident;
private Date breakDownTime;//TODO: PDate
public Bus(double vehicleSize) {
super(vehicleSize);
@ -15,32 +12,4 @@ public class Bus extends Vehicle implements Breakable {
public int getTimeToFix() {
return TIME_TO_FIX;
}
@Override
public boolean isBroken() {
return broken;
}
@Override
public boolean isInAccident() {
return inAccident;
}
@Override
public void collide(Breakable car, Date time) {
//Make Accident here and change return type?
// or make reference as property
}
@Override
public void _break(Date time) {
broken = true;
breakDownTime = time;
}
@Override
public void fix() {
broken = false;
inAccident = false;
}
}

58
src/CivilVehicle.java Normal file
View File

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

View File

@ -1,31 +1,14 @@
import java.util.Date;
public class Sedan extends Vehicle implements Breakable {
public class Sedan extends CivilVehicle {
private final int TIME_TO_FIX = 15; //in minutes
private boolean broken;
private boolean accident;
public Sedan(double vehicleSize){
super(vehicleSize);
}
public boolean isBroken(){ return broken; }
public boolean isInAccident(){ return accident; }
@Override
public void collide(Breakable car, Date time) {
}
@Override
public void _break(Date time) {
}
public int getTimeToFix(){ return TIME_TO_FIX; }
public void fix() { this.broken = false; this.accident = false; }
}