Merge pull request #9 from EngOsamah/master

This commit is contained in:
HeshamTB 2020-11-15 01:31:34 +03:00 committed by GitHub
commit 92bbb0c099
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 178 additions and 67 deletions

View File

@ -3,10 +3,9 @@ import java.util.*;
public class MakkahCity { public class MakkahCity {
private static final ArrayList<Campaign> listOfCampaigns = new ArrayList<>(); private static final ArrayList<Campaign> listOfCampaigns = new ArrayList<>();
private static final ArrayList<Vehicle> listOfVehicles = new ArrayList<>(); private static final ArrayList<Vehicle> listOfVehicles = new ArrayList<>();
private static final Route[] stdRoutes = new Route[6]; private static final Route[] stdRoutes = new Route[6];
private static final Street[] stdStreet = new Street[8]; private static final Street[] stdStreet = new Street[9];
private static final PDate timeManager = new PDate( private static final PDate timeManager = new PDate(
new GregorianCalendar(2020, Calendar.JANUARY, 1, 4, 0, 0), new GregorianCalendar(2020, Calendar.JANUARY, 1, 4, 0, 0),
@ -16,11 +15,11 @@ public class MakkahCity {
public static void main(String[] args) { public static void main(String[] args) {
//Gen Camp //Gen Camp
generateCamps(District.ALAZIZIYA, (int)getRandom(70, 100)); generateCamps(District.ALAZIZIYA, (int)getRandom(700, 1000));
generateCamps(District.ALMANSOOR, (int)getRandom(110, 160)); generateCamps(District.ALMANSOOR, (int)getRandom(1000, 1200));
generateCamps(District.ALHIJRA, (int)getRandom(80, 110)); generateCamps(District.ALHIJRA, (int)getRandom(850, 1100));
//fillBusesToList(); fillBusesToList();
//Make Streets //Make Streets
makeStreets(); makeStreets();
@ -37,7 +36,10 @@ public class MakkahCity {
while(!timeManager.isEnded()) { while(!timeManager.isEnded()) {
//Start of Every hour //Start of Every hour
if (timeManager.getCurrentCalendar().get(Calendar.MINUTE) == 0){ if (timeManager.getCurrentCalendar().get(Calendar.MINUTE) == 0){
if (isAllArrived()) {
System.out.println("\nAll campaigns have arrived befor " + timeManager.getCurrentTime());
break;
}
} }
//Start of Every half-hour //Start of Every half-hour
if (timeManager.getCurrentCalendar().get(Calendar.MINUTE) % 30 == 0){ if (timeManager.getCurrentCalendar().get(Calendar.MINUTE) % 30 == 0){
@ -46,6 +48,8 @@ public class MakkahCity {
//Start of every 10min //Start of every 10min
if (timeManager.getCurrentCalendar().get(Calendar.MINUTE) % 10 == 0){ if (timeManager.getCurrentCalendar().get(Calendar.MINUTE) % 10 == 0){
addCivilVehicleNoise(); addCivilVehicleNoise();
System.out.println("\n\n" + getStreetsReport());
printFinalRep();
} }
if (timeManager.getCurrentCalendar().get(Calendar.MINUTE) == getRandom(0,59) if (timeManager.getCurrentCalendar().get(Calendar.MINUTE) == getRandom(0,59)
@ -57,43 +61,45 @@ public class MakkahCity {
Route route = vehicle.getRoute(); Route route = vehicle.getRoute();
double currentLocation = vehicle.getCurrentLocation(); double currentLocation = vehicle.getCurrentLocation();
if (vehicle.getCurrentStreet() == null && if (vehicle.getCurrentStreet() == null &&
route.getStreets()[0].capcityPoint(0,1500) < 1) { route.getStreets()[0].capcityPoint(0,1000) < 1) {
vehicle.setCurrentStreet(route.getStreets()[0]); vehicle.setCurrentStreet(route.getStreets()[0]);
} }
else if (vehicle.getCurrentStreet() != null && vehicle.getCurrentStreet().capcityPoint(currentLocation+1500, if (vehicle.getCurrentStreet() != null && vehicle.getCurrentStreet().capcityPoint(currentLocation,
currentLocation+1500*2) < 1 ) { //May test diff values. currentLocation+1000) < 1 ) { //May test diff values.
if (currentLocation >= vehicle.getCurrentStreet().getLength()) { if (currentLocation >= vehicle.getCurrentStreet().getLength()) {
//Move to next street //Move to next street
vehicle.setCurrentLocation(0);
int nxtIndex = route.indexOf(vehicle.getCurrentStreet()) + 1; int nxtIndex = route.indexOf(vehicle.getCurrentStreet()) + 1;
if (nxtIndex <= route.getStreets().length - 1) if (nxtIndex <= route.getStreets().length - 1) {
if (vehicle.getRoute().getStreets()[nxtIndex].capcityPoint(0, 1000) < 1) {
vehicle.setCurrentStreet(route.getStreets()[nxtIndex]); vehicle.setCurrentStreet(route.getStreets()[nxtIndex]);
vehicle.setCurrentLocation(0);
}
}
else else
vehicle.arrive(); vehicle.arrive();
} }
if (!vehicle.isArrivedToDest()) { if (!vehicle.isArrivedToDest()) {
if (vehicle instanceof Bus) vehicle.move(Bus.MAX_FORWARD); double factor = 1-(vehicle.getCurrentStreet().capcityPoint(vehicle.getCurrentLocation(),
else if (vehicle instanceof Sedan) vehicle.move(Sedan.MAX_FORWARD); vehicle.getCurrentLocation()+1000)) ;
else if (vehicle instanceof SUV) vehicle.move(SUV.MAX_FORWARD); if (vehicle instanceof Bus) vehicle.move(Bus.MAX_FORWARD * factor );
else if (vehicle instanceof Truck) vehicle.move(Bus.MAX_FORWARD); else if (vehicle instanceof Sedan) vehicle.move(Sedan.MAX_FORWARD * factor );
else if (vehicle instanceof SUV) vehicle.move(SUV.MAX_FORWARD * factor );
else if (vehicle instanceof Truck) vehicle.move(Bus.MAX_FORWARD * factor );
} }
} }
} }
Vehicle v = listOfVehicles.get(10); // Vehicle v = listOfVehicles.get(150);
if (v.getCurrentStreet() != null) { // if (v.getCurrentStreet() != null) {
System.out.printf("St: %s distance: %f total: %f %s\n", // System.out.printf("St: %s distance: %f total: %f %s\n",
v.getCurrentStreet().getName(), // v.getCurrentStreet().getName(),
v.getCurrentLocation(), // v.getCurrentLocation(),
v.getTotalDistanceTraveled(), // v.getTotalDistanceTraveled(),
timeManager.getCurrentTime()); // timeManager.getCurrentTime());
} //}
//noise based on time of day (From PDate) //noise based on time of day (From PDate)
//TODO: [5]Streets move forward.
//TODO: Get real car values.
//TODO: Get real street lengths
//TODO: UID For all vehicles
/* /*
Output: Output:
@ -102,6 +108,7 @@ public class MakkahCity {
*/ */
timeManager.step(Calendar.MINUTE, 1); timeManager.step(Calendar.MINUTE, 1);
} }
//TODO: print final report
} }
private static void setRoutesForCampaigns() { private static void setRoutesForCampaigns() {
@ -110,18 +117,6 @@ public class MakkahCity {
} }
} }
/*
This is not used. The campaign object sets the routes for the busses
*/
@Deprecated
private static void setUpCampaginRoute(Campaign camp, int routeName) {
Route route = stdRoutes[routeName];
camp.setDestToHousingRoute(route);
//For now set all busses to one route
for(Vehicle vehicle : camp.getVehicles()){
vehicle.setRoute(route);
}
}
private static double getRandom(double min, double max) { private static double getRandom(double min, double max) {
return (Math.random() * (max - min) + min); return (Math.random() * (max - min) + min);
@ -135,14 +130,15 @@ public class MakkahCity {
} }
private static void makeStreets(){ private static void makeStreets(){
stdStreet[StreetNames.KA_STREET.ordinal()] = new Street(22700,3, StreetNames.KA_STREET); stdStreet[StreetNames.KA_STREET.ordinal()] = new Street(4700,3, StreetNames.KA_STREET);
stdStreet[StreetNames.FOURTH_HIGHWAY.ordinal()] = new Street(24600,4, StreetNames.FOURTH_HIGHWAY); stdStreet[StreetNames.FOURTH_HIGHWAY.ordinal()] = new Street(9700,4, StreetNames.FOURTH_HIGHWAY);
stdStreet[StreetNames.KUDAY.ordinal()] = new Street(22000,3, StreetNames.KUDAY); stdStreet[StreetNames.THIRD_HIGHWAY.ordinal()] = new Street(8200,3, StreetNames.THIRD_HIGHWAY);
stdStreet[StreetNames.STREET1.ordinal()] = new Street(4000,2, StreetNames.STREET1); stdStreet[StreetNames.STREET1.ordinal()] = new Street(7800,3, StreetNames.STREET1);
stdStreet[StreetNames.STREET2.ordinal()] = new Street(7000,2,StreetNames.STREET2); stdStreet[StreetNames.STREET2.ordinal()] = new Street(2400,3,StreetNames.STREET2);
stdStreet[StreetNames.STREET3.ordinal()] = new Street(400,2, StreetNames.STREET3); stdStreet[StreetNames.STREET3.ordinal()] = new Street(4800,2, StreetNames.STREET3);
stdStreet[StreetNames.STREET4.ordinal()] = new Street(8200,2,StreetNames.STREET4); stdStreet[StreetNames.STREET4.ordinal()] = new Street(3800,3,StreetNames.STREET4);
stdStreet[StreetNames.IBRAHIM_ALKHALIL.ordinal()] = new Street(100,2, StreetNames.IBRAHIM_ALKHALIL); //TODO: [7]Change numbers stdStreet[StreetNames.STREET5.ordinal()] = new Street(3200,2, StreetNames.STREET5);
stdStreet[StreetNames.IBRAHIM_ALKHALIL.ordinal()] = new Street(4500,3, StreetNames.IBRAHIM_ALKHALIL);
} }
private static void makeRoutes() { private static void makeRoutes() {
@ -151,7 +147,7 @@ public class MakkahCity {
new Street[]{ new Street[]{
stdStreet[StreetNames.STREET1.ordinal()], stdStreet[StreetNames.STREET1.ordinal()],
stdStreet[StreetNames.STREET2.ordinal()], stdStreet[StreetNames.STREET2.ordinal()],
stdStreet[StreetNames.KUDAY.ordinal()]}, stdStreet[StreetNames.THIRD_HIGHWAY.ordinal()]},
District.ALHIJRA, Mashier.ARAFAT); District.ALHIJRA, Mashier.ARAFAT);
stdRoutes[RouteName.mashierToAlHijra2.ordinal()] = new Route(new Street[]{ stdRoutes[RouteName.mashierToAlHijra2.ordinal()] = new Route(new Street[]{
@ -172,8 +168,8 @@ public class MakkahCity {
new Street[]{ new Street[]{
stdStreet[StreetNames.STREET1.ordinal()], stdStreet[StreetNames.STREET1.ordinal()],
stdStreet[StreetNames.STREET2.ordinal()], stdStreet[StreetNames.STREET2.ordinal()],
stdStreet[StreetNames.KUDAY.ordinal()], stdStreet[StreetNames.THIRD_HIGHWAY.ordinal()],
stdStreet[StreetNames.IBRAHIM_ALKHALIL.ordinal()]//TODO: [8]is actually half of ibrahim khalil. stdStreet[StreetNames.STREET5.ordinal()]//TODO: [8]is actually half of ibrahim khalil.
},District.ALMANSOOR, Mashier.ARAFAT); },District.ALMANSOOR, Mashier.ARAFAT);
//Optimal for Almansoor //Optimal for Almansoor
@ -207,7 +203,8 @@ public class MakkahCity {
int numOfTruck = (int)getRandom(0, 2); int numOfTruck = (int)getRandom(0, 2);
if (street.getName() == StreetNames.FOURTH_HIGHWAY) numOfSedan = (int) (numOfSedan * 0.5); if (street.getName() == StreetNames.FOURTH_HIGHWAY) numOfSedan = (int) (numOfSedan * 0.5);
if (street.getName() == StreetNames.STREET1) numOfSedan = (int) (numOfSedan * 1.5); //add more streets if (street.getName() == StreetNames.STREET3) numOfSedan = (int) (numOfSedan * 1.5);
if (street.getName() == StreetNames.STREET5) numOfSedan = (int) (numOfSedan * 1.5);
for (int x = 0; x < numOfSedan; x++) { for (int x = 0; x < numOfSedan; x++) {
Sedan car = new Sedan(getRandom(4, 5)); Sedan car = new Sedan(getRandom(4, 5));
double pointOfEntry = getRandom(0, street.getLength()); double pointOfEntry = getRandom(0, street.getLength());
@ -221,7 +218,8 @@ public class MakkahCity {
} }
if (street.getName() == StreetNames.FOURTH_HIGHWAY) numOfTruck = (int) (numOfTruck * 0.5); if (street.getName() == StreetNames.FOURTH_HIGHWAY) numOfTruck = (int) (numOfTruck * 0.5);
if (street.getName() == StreetNames.STREET1) numOfTruck = (int) (numOfTruck * 1.5); //add more streets if (street.getName() == StreetNames.STREET3) numOfTruck = (int) (numOfTruck * 1.5);
if (street.getName() == StreetNames.STREET5) numOfSedan = (int) (numOfSedan * 1.5);
for (int x = 0; x < numOfTruck; x++) { for (int x = 0; x < numOfTruck; x++) {
Truck car = new Truck(getRandom(4, 5)); Truck car = new Truck(getRandom(4, 5));
double pointOfEntry = getRandom(0, street.getLength()); double pointOfEntry = getRandom(0, street.getLength());
@ -234,7 +232,8 @@ public class MakkahCity {
} }
if (street.getName() == StreetNames.FOURTH_HIGHWAY) numOfSUV = (int) (numOfSUV * 0.5); if (street.getName() == StreetNames.FOURTH_HIGHWAY) numOfSUV = (int) (numOfSUV * 0.5);
if (street.getName() == StreetNames.STREET1) numOfSUV = (int) (numOfSUV * 1.5); //add more streets if (street.getName() == StreetNames.STREET3) numOfSUV = (int) (numOfSUV * 1.5);
if (street.getName() == StreetNames.STREET5) numOfSedan = (int) (numOfSedan * 1.5);
for (int x = 0; x < numOfSUV; x++) { for (int x = 0; x < numOfSUV; x++) {
SUV car = new SUV(getRandom(4, 5)); SUV car = new SUV(getRandom(4, 5));
double pointOfEntry = getRandom(0, street.getLength()); double pointOfEntry = getRandom(0, street.getLength());
@ -312,5 +311,69 @@ public class MakkahCity {
Route[] routesArray = new Route[routes.size()]; Route[] routesArray = new Route[routes.size()];
return routes.toArray(routesArray); return routes.toArray(routesArray);
} }
private static void printReport() {
for(Street street : stdStreet) {
System.out.printf("StreetName: %s NumberOfVheciles : %d Capcity: %f\n",
street.getName().name(),street.getVehicles().size(), street.capcity());
int qurter = (int) street.getLength()/4;
double capcity = 0;
for(int i = 0; i < 4; i++) {
capcity = street.capcityPoint(i * qurter, qurter * (i+1));
System.out.printf("qurter%d: %.2f ", (i+1) , capcity );
}
System.out.println("\n");
}
}
private static String getStreetsReport() {
String headerFormat = "******Streets report*****\n" +
"Time: %s\n";
String report = "";
report = report + String.format(headerFormat, timeManager.getCurrentTime());
String entryFormat = "Street name: %-9s | remaining capacity: %%%-4s | cars: %d\n";
for (Street street : stdStreet) {
int cap = street.getPercentRemainingCapacity();
report = report + String.format(entryFormat,
street.getName().name(),
cap,
street.getVehicles().size());
}
return report;
}
private static void printFinalRep() {
int numberOfBusses = 0;
int numberOfArrivedBuses = getNumberOfArrivedBusses();
//Redundant loops slow down execution. find better sol.
for (Campaign campaign : listOfCampaigns) {
numberOfBusses += campaign.getNumberOfBusses();
}
System.out.printf("Buses: %d Buses done: %d\n",
numberOfBusses, numberOfArrivedBuses);
}
private static int getNumberOfArrivedBusses() {
int num = 0;
for (Campaign campaign : listOfCampaigns) {
for (Vehicle vehicle : campaign.getVehicles()){
if (vehicle instanceof Bus &&
vehicle.isArrivedToDest()) num++;
}
}
return num;
}
private static boolean isAllArrived() {
for (Campaign campaign : listOfCampaigns)
for (Vehicle vehicle : campaign.getVehicles())
if (!vehicle.isArrivedToDest())
return false;
return true;
}
} }

View File

@ -1,16 +1,29 @@
public class SUV extends CivilVehicle { public class SUV extends CivilVehicle {
private String UID;
private static int numeberOfSUV;
private final int TIME_TO_FIX = 15; //in minutes private final int TIME_TO_FIX = 15; //in minutes
public static final int MAX_FORWARD = 1300; public static final int MAX_FORWARD = 1300;
public SUV(double vehicleSize){ public SUV(double vehicleSize){
super(vehicleSize); super(vehicleSize);
generateUID();
} }
public int getTimeToFix(){ public int getTimeToFix(){
return TIME_TO_FIX; return TIME_TO_FIX;
} }
private void generateUID() {
numeberOfSUV++;
this.UID = String.format("SUV%04d", numeberOfSUV);
}
public String getUID(){
return this.UID;
}
} }

View File

@ -1,14 +1,30 @@
public class Sedan extends CivilVehicle { public class Sedan extends CivilVehicle {
private String UID;
private static int numeberOfSedan;
private final int TIME_TO_FIX = 15; //in minutes private final int TIME_TO_FIX = 15; //in minutes
public static final int MAX_FORWARD = 1500; // Meter/Min public static final int MAX_FORWARD = 1500; // Meter/Min
public Sedan(double vehicleSize){ public Sedan(double vehicleSize){
super(vehicleSize); super(vehicleSize);
generateUID();
} }
@Override
public int getTimeToFix(){ return TIME_TO_FIX; } public int getTimeToFix(){ return TIME_TO_FIX;
}
private void generateUID() {
numeberOfSedan++;
this.UID = String.format("Sedan%04d", numeberOfSedan);
}
public String getUID(){
return this.UID;
}
} }

View File

@ -56,15 +56,15 @@ public class Street {
for(int i=0;i<vehicles.size();i++) { for(int i=0;i<vehicles.size();i++) {
totalLenthofCar+=vehicles.get(i).getVehicleSize(); totalLenthofCar+=vehicles.get(i).getVehicleSize();
} }
return totalLength -(totalLenthofCar + 0.5*(vehicles.size() - 2)); return totalLength -totalLenthofCar;
} }
public int getPercentRemainingCapacity() { public int getPercentRemainingCapacity() {
return (int) (capcity()/(this.length*this.numberOfLanes))*100; return (int) (capcity()/(this.length*this.numberOfLanes)*100);
} }
public boolean canTakeVehicles( Vehicle vehicle ) { public boolean canTakeVehicles( Vehicle vehicle ) {
if ( vehicle.getVehicleSize() + 0.5 < capcity() ) if ( vehicle.getVehicleSize() < capcity() )
return true; return true;
else else
return false; return false;
@ -85,6 +85,12 @@ public class Street {
vehicles.get(i).getCurrentLocation() <= max) vehicles.get(i).getCurrentLocation() <= max)
totalLenthofCar+=vehicles.get(i).getVehicleSize(); totalLenthofCar+=vehicles.get(i).getVehicleSize();
} }
return totalLenthofCar / totalLength; double capcity = totalLenthofCar / totalLength;
if (capcity > 1)
return 1;
else if (capcity < 0 )
return 0;
else
return capcity;
} }
} }

View File

@ -4,12 +4,15 @@
public enum StreetNames { public enum StreetNames {
KA_STREET, KA_STREET,
KUDAY, THIRD_HIGHWAY,
FOURTH_HIGHWAY, FOURTH_HIGHWAY,
STREET1, STREET1,
STREET2, STREET2,
STREET3, STREET3,
STREET4, STREET4,
IBRAHIM_ALKHALIL STREET5,
IBRAHIM_ALKHALIL,
} }

View File

@ -1,15 +1,25 @@
public class Truck extends CivilVehicle { public class Truck extends CivilVehicle {
private String UID;
private static int numeberOfTruck;
private final int TIME_TO_FIX = 20; //in minutes private final int TIME_TO_FIX = 20; //in minutes
// public static final int MAX_FORWARD = ????; //TODO what is the speed here ??
public Truck(double vehicleSize){ public Truck(double vehicleSize){
super(vehicleSize); super(vehicleSize);
generateUID();
} }
public int getTimeToFix(){ public int getTimeToFix(){
return TIME_TO_FIX; return TIME_TO_FIX;
} }
private void generateUID() {
numeberOfTruck++;
this.UID = String.format("Truck%04d", numeberOfTruck);
}
public String getUID(){
return this.UID;
}
}
}