Vehicle toString():

- Print route
    - Check if street is null before getting
        name.

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2020-11-30 21:34:12 +03:00
parent d7e3fd7a68
commit 3a91f2f0f1
Signed by: Hesham
GPG Key ID: 74876157D199B09E
4 changed files with 22 additions and 4 deletions

View File

@ -29,7 +29,15 @@ public class Bus extends CivilVehicle {
public int getTimeToFix() {
return TIME_TO_FIX;
}
@Override
public String toString() {
StringBuilder s = new StringBuilder();
s.append(super.toString());
s.append(String.format("ID: %s, Campaign ID: %s\n",this.getUID() , getCampaign().getUID()));
return s.toString();
}
private void generateUID() {
numeberOfBuses++;
this.UID = String.format("BUS%04d", numeberOfBuses);

View File

@ -185,7 +185,8 @@ public class MakkahCity {
String c = in.next();
Vehicle v = listOfVehicles.get(Integer.parseInt(c));
//TODO: override toString() in vehicle then Bus. This will throw cast ex.
System.out.print(v.toString());
System.out.print("\n\n"+v.toString()+"\n\n");
//meybe add option here to go to members (Campaign, Street ...)
}
if (choice.equals("2")){
for (int i = 0; i < stdStreet.length; i++) {

View File

@ -28,4 +28,9 @@ public class TrafficPoliceCar extends Vehicle implements CanBeGovtCar, CanFixAcc
public int getMaxSpeed() {
return Sedan.MAX_FORWARD;
}
@Override
public String getUID() {
return TPC_UID;
}
}

View File

@ -18,6 +18,7 @@ public abstract class Vehicle {
private HashMap<Street, Integer> routeTimeHistory = new HashMap<>();
public abstract int getMaxSpeed();
public abstract String getUID();
public Vehicle(double vehicleSize){
setVehicleSize(vehicleSize);
@ -168,9 +169,12 @@ public abstract class Vehicle {
}
public String toString() {
return String.format("%s\nStreet: %s Location: %.1f\n" +
Street st = this.currentStreet;
String streetString = "null";
if (st != null) streetString = st.getName().name();
return String.format("%s\nRoute: %s\nStreet: %s, Location: %.1f\n" +
"Arrived: %s Starting time: %s Arrive Time: %s\n",
super.toString(), this.getCurrentStreet().getName().name(),
super.toString(), this.route, streetString,
this.getCurrentLocation(), this.isArrivedToDest(),
this.getTimeStartedMoving(), this.getTimeOfArrival());
}