Street history

- Save time of entry for each vehicle

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2020-11-30 22:36:37 +03:00
parent 3a91f2f0f1
commit fbd39f4081
Signed by: Hesham
GPG Key ID: 74876157D199B09E

View File

@ -1,21 +1,33 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
public class Street {
private double length;
private int numberOfLanes;
private ArrayList<Vehicle> vehicles;
private ArrayList<Vehicle> vehicles; //Current
private HashMap<Vehicle, ArrayList<Date>> vehiclesHistory;//History of vehicles
private StreetNames name;
public Street(double length, int numberOfLanes, StreetNames name) {
vehicles = new ArrayList<>();
vehiclesHistory = new HashMap<>();
setLength(length);
setNumberOfLanes(numberOfLanes);
this.name = name;
}
public Date[] getHistoryForVehicle(Vehicle vehicle){
if (vehiclesHistory.containsKey(vehicle)){
Date[] hist = new Date[vehiclesHistory.get(vehicle).size()];
return vehiclesHistory.get(vehicle).toArray(hist);
}
return null;
}
private void setLength(double length) {
if (length >= 0) this.length = length;
else throw new IllegalArgumentException("Can not make a negative length street");
@ -83,10 +95,15 @@ public class Street {
}
public void addVehicle(Vehicle vehicle) {
//if(capcity() > vehicle.getVehicleSize() + 0.5) {
//adds incoming vehicle in last.
vehicles.add(vehicle);
//}
vehicles.add(vehicle);
addHistoryEntry(vehicle);
}
private void addHistoryEntry(Vehicle vehicle) {
if (!vehiclesHistory.containsKey(vehicle)) {
vehiclesHistory.put(vehicle, new ArrayList<>());
}
vehiclesHistory.get(vehicle).add(MakkahCity.getTimeMan().getCurrentTime());//Add time
}
public double capcityPoint(double min, double max) {