This commit is contained in:
HeshamTB 2020-12-18 14:01:52 +03:00
parent f30a64a5b5
commit c07dc8a87c
2 changed files with 191 additions and 36 deletions

View File

@ -1,8 +1,6 @@
import java.awt.Color;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
@ -39,6 +37,8 @@ public class GUI_History {
private Object[][] streetData;
private String[] streetColNames = {"Street Name", "Street Load %", "Total", "Buses",
"Local Vehicles","Avg. Time"};
private String[] districtColNames = {"District", "Campaigns", "Busses", "Arrival %",
"Avg. Time", "Best time to Arafat", "Best time to District"};
public GUI_History(List<State> states) {
this.states = states;
@ -69,8 +69,7 @@ public class GUI_History {
}
//District data
Object[][] districtData = new Object[campPerDistrict.length][7];
String[] districtColNames = {"District", "Campaigns", "Busses", "Arrival %",
"Avg. Time", "Best time to Arafat", "Best time to District"};
for (int i = 0; i < campPerDistrict.length; i++) {
districtData[i][0] = campPerDistrict[i].get(0).getHotelDistrict().name();
@ -305,7 +304,7 @@ public class GUI_History {
currenttimeManager = state.getStateTime();
}
public void updateFrame() {
public void updateFrame() {
streetData = new Object[stdStreet.length][6];
for (int i = 0; i < stdStreet.length; i++) {
streetData[i][0] = stdStreet[i].getName().name();
@ -313,7 +312,7 @@ public class GUI_History {
streetData[i][2] = stdStreet[i].getVehicles().size();
streetData[i][3] = stdStreet[i].getNumberOfBuses();
streetData[i][4] = stdStreet[i].getNumberOfLocalCars();
streetData[i][5] = MakkahCity.avgTimeOnStreet(stdStreet[i]);
streetData[i][5] = avgTimeOnStreet(stdStreet[i]);
}
streetTable.setModel(new DefaultTableModel(streetData ,streetColNames));
@ -321,17 +320,13 @@ public class GUI_History {
for (int i = 0; i < campPerDistrict.length; i++) {
districtData[i][0] = campPerDistrict[i].get(0).getHotelDistrict().name();
districtData[i][1] = campPerDistrict[i].size();
districtData[i][2] = MakkahCity.busesInDistrict(District.values()[i]);
districtData[i][3] = MakkahCity.getPercentArrival(District.values()[i]);
districtData[i][4] = MakkahCity.getAvgTimeOfTrip(District.values()[i]);
districtData[i][5] = MakkahCity.getShortestRoute(campPerDistrict[i].get(0), Mashier.ARAFAT).getFastestTimeOfTravel(new Bus());
districtData[i][6] = MakkahCity.getShortestRoute(campPerDistrict[i].get(0), Mashier.MINA).getFastestTimeOfTravel(new Bus());
}
for (int i = 0; i < districtData.length; i++) {
for (int j = 0; j < districtData[i].length; j++) {
districtTable.setValueAt(districtData[i][j], i, j);
}
districtData[i][2] = busesInDistrict(District.values()[i]);
districtData[i][3] = getPercentArrival(District.values()[i]);
districtData[i][4] = getAvgTimeOfTrip(District.values()[i]);
districtData[i][5] = getShortestRoute(campPerDistrict[i].get(0), Mashier.ARAFAT).getFastestTimeOfTravel(new Bus());
districtData[i][6] = getShortestRoute(campPerDistrict[i].get(0), Mashier.MINA).getFastestTimeOfTravel(new Bus());
}
districtTable.setModel(new DefaultTableModel(districtData, districtColNames));
lblDate.setText(currenttimeManager.toString());
String status = "";
@ -350,7 +345,7 @@ public class GUI_History {
String bus = String.format("%d", numberOfBusses);
lblNumOfBuses.setText(bus);
String numOfdoneBuses = String.format("%d",MakkahCity.getNumberOfArrivedBusses());
String numOfdoneBuses = String.format("%d", getNumberOfArrivedBusses());
lblNumOfDonebuses.setText(numOfdoneBuses);
if (Vehicle.getMaxArrived() != null && Vehicle.getMinArrived() != null) {
@ -358,11 +353,160 @@ public class GUI_History {
lblMinimumTripValue.setText(Vehicle.getMinArrived().getTripTime().toString());
}
String NumberOfBussPerHour = String.format("%d", MakkahCity.getNumberOfArrivedBussesPerHour());
String NumberOfBussPerHour = String.format("%d", getNumberOfArrivedBussesPerHour());
lblBusesArrivedInTheLastHourValue.setText(NumberOfBussPerHour);
lblAverageTripForLastHourValue.setText(MakkahCity.avgTimeOfTrip());
lblAverageTimeForTheTrip.setText(MakkahCity.getAvgTripForAllDis());
lblAverageTripForLastHourValue.setText(avgTimeOfTrip());
lblAverageTimeForTheTrip.setText(getAvgTripForAllDis());
}
//Methods
public 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 street.getFastestTimeOfTravel(new Bus());
return String.format("%02d:%02d", hours, minutes);
}
private int busesInDistrict(District district){
int buses = 0;
for (Campaign campaign : campPerDistrict[district.ordinal()]){
buses += campaign.getNumberOfBusses();
}
return buses;
}
private int getPercentArrival(District district) {
int sum = 0;
for (Campaign campaign : campPerDistrict[district.ordinal()]) {
sum += campaign.getPercentArrived();
}
return sum/campPerDistrict[district.ordinal()].size();
}
private String getAvgTimeOfTrip(District district){
int sum = 0;
int counter = 1;
for (Campaign campaign : campPerDistrict[district.ordinal()]) {
for (Vehicle vehicle : campaign.getVehicles()) {
if (vehicle.isArrivedToDest()) {
long minutes = (vehicle.getTimeOfArrival().getTime() - vehicle.getTimeStartedMoving().getTime())/60000;
sum+= minutes;
counter++;
}
}
}
sum = sum /counter;
int hours = sum / 60;
int minutes = sum % 60;
if (hours == 0 && minutes == 0) return " n/a";
return String.format("%2d:%02d", hours,minutes);
}
private Route getShortestRoute(Campaign campaign, Mashier mashier) {
Route[] routes = getRoutesToDistrict(campaign.getHotelDistrict());
Route route = null;
double min = Double.MAX_VALUE;
for (Route r : routes) {
if (r.getMashier() == mashier){
if (r.getTotalLength() < min) {
min = r.getTotalLength();
route = r;
}
}
}
return route;
}
private Route[] getRoutesToDistrict(District district) {
ArrayList<Route> routes = new ArrayList<>();
for (Route route : stdRoutes) {
if (route.getHotelArea() == district) {
routes.add(route);
}
}
Route[] routesArray = new Route[routes.size()];
return routes.toArray(routesArray);
}
public int getNumberOfArrivedBusses() {
int num = 0;
for (Campaign campaign : listOfCampaigns) {
for (Vehicle vehicle : campaign.getVehicles()){
if (vehicle instanceof Bus &&
vehicle.isArrivedToDest()) num++;
}
}
return num;
}
public int getNumberOfArrivedBussesPerHour() {
Calendar now = new GregorianCalendar();
now.setTime(currenttimeManager);
Calendar from = (GregorianCalendar)now.clone();
from.roll(Calendar.HOUR, -1);
int num = 0;
for (Campaign campaign : listOfCampaigns){
for (Vehicle bus : campaign.getVehicles()){
if (bus.isArrivedToDest() && bus.getTimeOfArrival().before(now.getTime())
&& bus.getTimeOfArrival().after(from.getTime())) {
num++;
}
}
}
return num;
}
public String avgTimeOfTrip() {
//TODO: does output diff value even after all have arrived.
Calendar now = new GregorianCalendar();
now.setTime(currenttimeManager);
Calendar from = (GregorianCalendar)now.clone();
from.roll(Calendar.HOUR, -1);
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;
if (hours == 0 && minutes == 0) return "(No Arrivals) In Last Hour";
return String.format("%2d:%02d", hours, minutes);
}
public String getAvgTripForAllDis() {
int sum = 0;
int counter = 1;
for (Campaign campaign : listOfCampaigns) {
for (Vehicle vehicle : campaign.getVehicles()) {
if (vehicle.isArrivedToDest()) {
long minutes = (vehicle.getTimeOfArrival().getTime() - vehicle.getTimeStartedMoving().getTime())/60000;
sum+= minutes;
counter++;
}
}
}//Make the following a method since it is the same other method
sum = sum /counter;
int hours = sum / 60;
int minutes = sum % 60;
if (hours == 0 && minutes == 0) return "-:--";
return String.format("%2d:%02d", hours,minutes);
}
}

View File

@ -419,6 +419,11 @@ public class MakkahCity {
//Set Routes for Campaigns
while(!firstDayTimeMan.isEnded()) {
checkInput();
if (!isAllRoutSet) {
isAllRoutSet = true;
setRoutesForCampaigns(Mashier.ARAFAT);
}
//Start of Every hour
if (firstDayTimeMan.getCurrentCalendar().get(Calendar.MINUTE) == 0){
System.out.println("\n\n" + getStreetsReport());
@ -426,11 +431,7 @@ public class MakkahCity {
saveState();
}
else System.out.print(".");
if (!isAllRoutSet) {
isAllRoutSet = true;
setRoutesForCampaigns(Mashier.ARAFAT);
}
clearDoneCivilVehicles();
addCivilVehicleNoise();
for (Vehicle vehicle : listOfVehicles) {
@ -463,7 +464,7 @@ public class MakkahCity {
if (isAllArrived() && allArrivedToArafatTime == null) allArrivedToArafatTime = (Date)currenttimeManager.getCurrentTime().clone();
firstDayTimeMan.step(Calendar.MINUTE, 1);
lblDate.setText(currenttimeManager.getCurrentTime().toString());
lblArrivedToArafatTime.setText(getDistTimeForLbl());
//lblArrivedToArafatTime.setText(getDistTimeForLbl());
}
currenttimeManager = lastDayTimeMan;
@ -481,6 +482,12 @@ public class MakkahCity {
setRoutesForCampaigns(Mashier.MINA);
while(!lastDayTimeMan.isEnded()) {
checkInput();
if (!isAllRoutSet) {
isAllRoutSet = true;
setRoutesForCampaigns(Mashier.MINA);
}
//Start of Every hour
if (lastDayTimeMan.getCurrentCalendar().get(Calendar.MINUTE) == 0){
System.out.println("\n\n" + getStreetsReport());
@ -488,12 +495,7 @@ public class MakkahCity {
saveState();
}
else System.out.print(".");
if (!isAllRoutSet) {
isAllRoutSet = true;
setRoutesForCampaigns(Mashier.MINA);
}
clearDoneCivilVehicles();
addCivilVehicleNoise();
for (Vehicle vehicle : listOfVehicles) {
@ -525,7 +527,7 @@ public class MakkahCity {
if (isAllArrived() && allArrivedToHotelsTime == null) allArrivedToHotelsTime = (Date)currenttimeManager.getCurrentTime().clone();
lastDayTimeMan.step(Calendar.MINUTE, 1);
lblDate.setText(currenttimeManager.getCurrentTime().toString());
lblArrivedToHotelsTime.setText(getDistTimeForLbl());
//lblArrivedToHotelsTime.setText(getDistTimeForLbl());
}
//When done show menu to analyze. Exit from menu too.
inputListener.pause();
@ -1261,7 +1263,16 @@ public class MakkahCity {
}
}
lblDate.setText(currenttimeManager.getCurrentTime().toString());
if (lblArrivedToArafatTime.getText().equalsIgnoreCase("N/A"))
if (allArrivedToArafatTime != null) {
lblArrivedToArafatTime.setText(allArrivedToArafatTime.toString());
}
if (lblArrivedToHotelsTime.getText().equalsIgnoreCase("N/A"))
if (allArrivedToHotelsTime != null) {
lblArrivedToHotelsTime.setText(allArrivedToHotelsTime.toString());
}
String status = "";
if (currenttimeManager == firstDayTimeMan) {
status = "Heading to Arafat";