Compare commits

...

1 Commits

Author SHA1 Message Date
625e4c1faf
unusable testing only 2020-12-02 08:18:04 +03:00
3 changed files with 65 additions and 31 deletions

View File

@ -26,6 +26,7 @@ public class MakkahCity {
private static final InputListener inputListener = new InputListener(); private static final InputListener inputListener = new InputListener();
private static final Thread t = new Thread(inputListener,"InputThread-Makkah"); private static final Thread t = new Thread(inputListener,"InputThread-Makkah");
private static final Thread[] threadPool = new Thread[4];
public static void main(String[] args) { public static void main(String[] args) {
@ -58,30 +59,28 @@ public class MakkahCity {
clearDoneCivilVehicles(); clearDoneCivilVehicles();
addCivilVehicleNoise(); addCivilVehicleNoise();
for (Vehicle vehicle : listOfVehicles) {
Route route = vehicle.getRoute();
double currentLocation = vehicle.getCurrentLocation();
if (vehicle.getCurrentStreet() == null &&
route.getStreets()[0].capcityPoint(0,1000) < 1) {
vehicle.setCurrentStreet(route.getStreets()[0]);
}
if (vehicle.getCurrentStreet() != null && vehicle.getCurrentStreet().capcityPoint(currentLocation,
currentLocation+1000) < 1 ) {
if (currentLocation >= vehicle.getCurrentStreet().getLength()) {
//Move to next street ArrayList<Vehicle> l = (ArrayList<Vehicle>) listOfVehicles.clone();
vehicle.moveToNextStreet(); while (l.size() > 0){
} for (Thread t : threadPool){
if (!vehicle.isArrivedToDest()) { if (t == null) {
double factor = 1-(vehicle.getCurrentStreet().capcityPoint(vehicle.getCurrentLocation(), t = new Thread();
vehicle.getCurrentLocation()+1000,vehicle)) ; if (!t.isAlive()) {
if (vehicle instanceof Bus) vehicle.move(Bus.MAX_FORWARD * factor ); t = new Thread(new StreetUpdater(l.get(0)));
else if (vehicle instanceof Sedan) vehicle.move(Sedan.MAX_FORWARD * factor ); t.start();
else if (vehicle instanceof SUV) vehicle.move(SUV.MAX_FORWARD * factor ); l.remove(l.get(0));
else if (vehicle instanceof Truck) vehicle.move(Bus.MAX_FORWARD * factor ); }
} }
} }
} }
for (Thread t : threadPool) {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (isAllArrived()) allArrivedToArafatTime = (Date)currenttimeManager.getCurrentTime().clone(); if (isAllArrived()) allArrivedToArafatTime = (Date)currenttimeManager.getCurrentTime().clone();
firstDayTimeMan.step(Calendar.MINUTE, 1); firstDayTimeMan.step(Calendar.MINUTE, 1);
} }

View File

@ -6,8 +6,8 @@ public class Street implements Travelable {
private double length; private double length;
private int numberOfLanes; private int numberOfLanes;
private ArrayList<Vehicle> vehicles; //Current private volatile ArrayList<Vehicle> vehicles; //Current
private HashMap<Vehicle, ArrayList<Date>> vehiclesHistory;//History of vehicles private volatile HashMap<Vehicle, ArrayList<Date>> vehiclesHistory;//History of vehicles
private StreetNames name; private StreetNames name;
@ -63,7 +63,7 @@ public class Street implements Travelable {
return name; return name;
} }
public double capcity() { public synchronized double capcity() {
double totalLength = length * numberOfLanes; double totalLength = length * numberOfLanes;
double totalLenthofCar=0; double totalLenthofCar=0;
for(int i=0;i<vehicles.size();i++) { for(int i=0;i<vehicles.size();i++) {
@ -106,7 +106,7 @@ public class Street implements Travelable {
vehiclesHistory.get(vehicle).add(MakkahCity.getTimeMan().getCurrentTime());//Add time vehiclesHistory.get(vehicle).add(MakkahCity.getTimeMan().getCurrentTime());//Add time
} }
public double capcityPoint(double min, double max) { public synchronized double capcityPoint(double min, double max) {
double totalLength = (max - min) * numberOfLanes; double totalLength = (max - min) * numberOfLanes;
double totalLenthofCar=0; double totalLenthofCar=0;
for(int i=0;i<vehicles.size();i++) { for(int i=0;i<vehicles.size();i++) {

View File

@ -1,3 +1,4 @@
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
@ -5,14 +6,14 @@ public abstract class Vehicle {
private double vehicleSize; private double vehicleSize;
private Route route; private Route route;
private Street currentStreet; private volatile Street currentStreet;
private double currentLocation; private volatile double currentLocation;
private double totalDistanceTraveled; private double totalDistanceTraveled;
private boolean arrivedToDest; private volatile boolean arrivedToDest;
private boolean moving; private volatile boolean moving;
private Date timeStartedMoving; private volatile Date timeStartedMoving;
private Date timeOfArrival; private volatile Date timeOfArrival;
private Date timeStartedOnCurrentStreet; private volatile Date timeStartedOnCurrentStreet;
//Map Street to Minutes //Map Street to Minutes
private HashMap<Street, Integer> routeTimeHistory = new HashMap<>(); private HashMap<Street, Integer> routeTimeHistory = new HashMap<>();
@ -180,4 +181,38 @@ public abstract class Vehicle {
} }
} }
class StreetUpdater implements Runnable {
Vehicle vehicle;
public StreetUpdater(Vehicle vehicle){
this.vehicle = vehicle;
}
@Override
public void run() {
Route route = vehicle.getRoute();
double currentLocation = vehicle.getCurrentLocation();
if (vehicle.getCurrentStreet() == null &&
route.getStreets()[0].capcityPoint(0,1000) < 1) {
vehicle.setCurrentStreet(route.getStreets()[0]);
}
if (vehicle.getCurrentStreet() != null && vehicle.getCurrentStreet().capcityPoint(currentLocation,
currentLocation+1000) < 1 ) {
if (currentLocation >= vehicle.getCurrentStreet().getLength()) {
//Move to next street
vehicle.moveToNextStreet();
}
if (!vehicle.isArrivedToDest()) {
double factor = 1-(vehicle.getCurrentStreet().capcityPoint(vehicle.getCurrentLocation(),
vehicle.getCurrentLocation()+1000,vehicle)) ;
if (vehicle instanceof Bus) vehicle.move(Bus.MAX_FORWARD * factor );
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 );
}
}
}
}