Improved output
Added Breakdown of car types on each street. Calculate avg trip time in last 10 min Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
parent
14272a6834
commit
200acf9a98
@ -325,16 +325,18 @@ public class MakkahCity {
|
||||
private static String getStreetsReport() {
|
||||
String headerFormat = "******Streets report*****\n" +
|
||||
"Time: %s\n" +
|
||||
"Street name |remaining capacity| cars\n";
|
||||
" Street name |remaining capacity| Total | Buses | Local Vehicles |\n";
|
||||
String report = "";
|
||||
report = report + String.format(headerFormat, timeManager.getCurrentTime());
|
||||
String entryFormat = "%-16s | %%%-15s | %d\n";
|
||||
String entryFormat = "%-16s | %%%-15s | %5d | %5d | %14d |\n";
|
||||
for (Street street : stdStreet) {
|
||||
int cap = street.getPercentRemainingCapacity();
|
||||
report = report + String.format(entryFormat,
|
||||
street.getName().name(),
|
||||
cap,
|
||||
street.getVehicles().size());
|
||||
street.getVehicles().size(),
|
||||
getNumnberOfBuses(street),
|
||||
getNumberOfCivilCars(street));
|
||||
}
|
||||
return report;
|
||||
}
|
||||
@ -346,10 +348,37 @@ public class MakkahCity {
|
||||
for (Campaign campaign : listOfCampaigns) {
|
||||
numberOfBusses += campaign.getNumberOfBusses();
|
||||
}
|
||||
System.out.printf("Buses: %d Buses done: %d\n",
|
||||
numberOfBusses, numberOfArrivedBuses);
|
||||
System.out.printf("Buses: %d Buses done: %d Average trip time: %s\n",
|
||||
numberOfBusses, numberOfArrivedBuses, avgTimeOfTrip());
|
||||
avgTimeOfTrip();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Calculate average trip time for last 10 minutes
|
||||
* @return "hh:mm"
|
||||
*/
|
||||
private static String avgTimeOfTrip() {
|
||||
Calendar now = timeManager.getCurrentCalendar();
|
||||
Calendar from = (GregorianCalendar)now.clone();
|
||||
from.roll(Calendar.MINUTE, -10);
|
||||
int counter = 1;
|
||||
int sum = 0;
|
||||
for (Campaign campaign : listOfCampaigns){
|
||||
for (Vehicle bus : campaign.getVehicles()){
|
||||
if (bus.isArrivedToDest() && bus.getTimeOfArrival().before(now.getTime())
|
||||
&& bus.getTimeOfArrival().after(from.getTime())) {
|
||||
long minutes = (bus.getTimeOfArrival().getTime() - bus.getTimeStartedMoving().getTime())/60000;
|
||||
sum+= minutes;
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
}
|
||||
sum = sum /counter;
|
||||
int hours = sum / 60;
|
||||
int minutes = sum % 60;
|
||||
return String.format("%2d:%02d", hours,minutes);
|
||||
}
|
||||
|
||||
private static int getNumberOfArrivedBusses() {
|
||||
int num = 0;
|
||||
for (Campaign campaign : listOfCampaigns) {
|
||||
@ -369,4 +398,20 @@ public class MakkahCity {
|
||||
return true;
|
||||
}
|
||||
|
||||
private static int getNumnberOfBuses(Street street) {
|
||||
int number = 0;
|
||||
for (Vehicle vehicle : street.getVehicles()) {
|
||||
if (vehicle instanceof Bus) number++;
|
||||
}
|
||||
return number;
|
||||
}
|
||||
|
||||
private static int getNumberOfCivilCars(Street street) {
|
||||
int number = 0;
|
||||
for (Vehicle vehicle : street.getVehicles()) {
|
||||
if (vehicle instanceof CivilVehicle && !(vehicle instanceof Bus)) number++;
|
||||
}
|
||||
return number;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -57,17 +57,18 @@ public class Street {
|
||||
for(int i=0;i<vehicles.size();i++) {
|
||||
totalLenthofCar+=vehicles.get(i).getVehicleSize();
|
||||
}
|
||||
int percent = 0;
|
||||
if (totalLenthofCar > totalLength){
|
||||
percent = (int) (totalLength/totalLenthofCar);
|
||||
percent *= 100;
|
||||
percent += totalLength - (totalLength % totalLenthofCar);
|
||||
}
|
||||
else {
|
||||
percent = (int)(totalLength - totalLenthofCar);
|
||||
}
|
||||
|
||||
return percent;
|
||||
// int percent = 0;
|
||||
// if (totalLenthofCar > totalLength){
|
||||
// percent = (int) (totalLength/totalLenthofCar);
|
||||
// percent *= 100;
|
||||
// percent += totalLength - (totalLength % totalLenthofCar);
|
||||
// }
|
||||
// else {
|
||||
// percent = (int)(totalLength - totalLenthofCar);
|
||||
// }
|
||||
//
|
||||
// return percent;
|
||||
return totalLength - totalLenthofCar;
|
||||
}
|
||||
|
||||
public int getPercentRemainingCapacity() {
|
||||
|
Loading…
Reference in New Issue
Block a user