Average time for each street in report

This commit is contained in:
HeshamTB 2020-11-26 23:50:26 +03:00
parent 51534a5d2b
commit 538c4a09a4
Signed by: Hesham
GPG Key ID: 74876157D199B09E
2 changed files with 42 additions and 5 deletions

View File

@ -468,11 +468,11 @@ public class MakkahCity {
else status = " Status: Heading to hotels";
String headerFormat = "******Streets report*****\n" +
"Time: %s%s\n" +
" Street name |Street Load| Total | Buses | Local Vehicles |" +
" Street name |Street Load| Total | Buses | Local Vehicles | Avg. Time" +
"*********| District | Average Arrival | Avg. time\n";
StringBuilder report = new StringBuilder();
report.append(String.format(headerFormat, currenttimeManager.getCurrentTime(), status));
String streetFormat = "%-18s | %%%-8s | %5d | %5d | %14d |";
String streetFormat = "%-18s | %%%-8s | %5d | %5d | %14d | %-9s";
String districtForamt = " | %-9s | %%%-14d | %s";
for (int i = 0; i < stdStreet.length; i++) {
int cap = stdStreet[i].getPercentRemainingCapacity();
@ -481,7 +481,8 @@ public class MakkahCity {
cap,
stdStreet[i].getVehicles().size(),
stdStreet[i].getNumberOfBuses(),
stdStreet[i].getNumberOfLocalCars()));
stdStreet[i].getNumberOfLocalCars(),
avgTimeOnStreet(stdStreet[i])));
if (i < 3){
report.append(String.format(districtForamt, District.values()[i], getPercentArrival(District.values()[i]),getAvgTimeOfTrip(District.values()[i])));
}
@ -606,6 +607,24 @@ public class MakkahCity {
return report.toString();
}
//TODO: Bug: values are too low for second day (8, 9, 12 minutes?)
//This is for ALL vehicles, should make it for last hour to be consistent with the report.
public static String avgTimeOnStreet(Street street) {
int sum = 0;
int counter = 1;
for (Campaign campaign : listOfCampaigns)
for (Vehicle vehicle : campaign.getArrivedVehicles())
if (vehicle.hasCrossedStreet(street)){
sum += vehicle.getTimeOnStreet(street);
counter++;
}
sum /= counter;
int hours = sum / 60;
int minutes = sum % 60;
if (hours == 0 && minutes == 0) return "n/a";
return String.format("%02d:%02d", hours, minutes);
}
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";

View File

@ -15,7 +15,7 @@ public abstract class Vehicle {
private Date timeStartedOnCurrentStreet;
//Map Street to Minutes
private HashMap<Street, Long> routeTimeHistory = new HashMap<>();
private HashMap<Street, Integer> routeTimeHistory = new HashMap<>();
public abstract int getMaxSpeed();
@ -51,6 +51,7 @@ public abstract class Vehicle {
public void setRoute(Route route) {
this.route = route;
this.arrivedToDest = false;
this.routeTimeHistory.clear();
}
private void setVehicleSize(double vehicleSize) {
@ -137,7 +138,8 @@ public abstract class Vehicle {
}
public void moveToNextStreet() {
this.routeTimeHistory.put(this.currentStreet, MakkahCity.getTimeMan().getCurrentTime().getTime() - timeStartedOnCurrentStreet.getTime());
this.routeTimeHistory.put(this.currentStreet,
(int) (MakkahCity.getTimeMan().getCurrentTime().getTime() - timeStartedOnCurrentStreet.getTime())/60000);
int nxtIndex = route.indexOf(this.getCurrentStreet()) + 1;
if (nxtIndex <= route.getStreets().length - 1) {
if (this.getRoute().getStreets()[nxtIndex].capcityPoint(0, 1000) < 1) {
@ -148,6 +150,22 @@ public abstract class Vehicle {
else
this.arrive();
}
/**
* Get value in minutes of time spent travling on street. If did not, return 0.
* @param street
* @return Time in minutes
*/
public int getTimeOnStreet(Street street) {
if (routeTimeHistory.containsKey(street)) {
return routeTimeHistory.get(street);
}
return 0;
}
public boolean hasCrossedStreet(Street street) {
return routeTimeHistory.containsKey(street);
}
}