From fbd39f408187b9a38ec32150c340f2379a8e67f3 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Mon, 30 Nov 2020 22:36:37 +0300 Subject: [PATCH] Street history - Save time of entry for each vehicle Signed-off-by: HeshamTB --- src/Street.java | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/Street.java b/src/Street.java index 9b4f3bd..36bdb04 100644 --- a/src/Street.java +++ b/src/Street.java @@ -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 vehicles; + private ArrayList vehicles; //Current + private HashMap> 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) {