diff --git a/src/UE21_290425_Bruecke/Car.java b/src/UE21_290425_Bruecke/Car.java index dd69a01..d4b1371 100644 --- a/src/UE21_290425_Bruecke/Car.java +++ b/src/UE21_290425_Bruecke/Car.java @@ -1,22 +1,20 @@ package UE21_290425_Bruecke; -import java.time.LocalTime; - public class Car extends Thread { public Long index; - public LocalTime arrivingTime; + public long arrivingTime; public Guard guard; public Car(Guard guard, long index) { this.guard = guard; - this.arrivingTime = LocalTime.now(); + this.arrivingTime = System.currentTimeMillis(); this.index = index; } @Override public void run() { try { - Thread.sleep(Variables.CROSSING_TIME); + Thread.sleep(Variables.CROSSING_TIME * 1000); guard.leavingBridge(); } catch (InterruptedException e) { throw new RuntimeException(e); diff --git a/src/UE21_290425_Bruecke/Generator.java b/src/UE21_290425_Bruecke/Generator.java index d695bb5..7bfd342 100644 --- a/src/UE21_290425_Bruecke/Generator.java +++ b/src/UE21_290425_Bruecke/Generator.java @@ -9,7 +9,6 @@ public class Generator extends Thread { System.out.println("Statistics:"); generator.start(); guard.start(); - if (Variables.SHOW_CARS) Bridge.main(args); } @Override diff --git a/src/UE21_290425_Bruecke/Guard.java b/src/UE21_290425_Bruecke/Guard.java index 83911f3..389893d 100644 --- a/src/UE21_290425_Bruecke/Guard.java +++ b/src/UE21_290425_Bruecke/Guard.java @@ -1,14 +1,13 @@ package UE21_290425_Bruecke; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Set; +import java.util.TreeMap; public class Guard extends Thread { private static int carsOnBridge = 0; public final List waitingCars = new ArrayList<>(); - public final Set waitingTimes = new HashSet<>(); + public final TreeMap waitingTimes = new TreeMap<>(); public synchronized void leavingBridge() { carsOnBridge--; @@ -22,18 +21,17 @@ public class Guard extends Thread { public synchronized void crossingBridge(Car car) { carsOnBridge++; + this.waitingTimes.put(System.currentTimeMillis(), System.currentTimeMillis() - car.arrivingTime); this.waitingCars.remove(car); car.start(); } @Override public synchronized String toString() { - StringBuilder output = new StringBuilder("Cars waiting:\n"); - if (waitingCars.isEmpty()) output = new StringBuilder("No Cars waiting:"); - for (Car car : waitingCars) - output.append(String.format("Car%d:\t%02d:%02d:%02d\t", car.index, car.arrivingTime.getHour(), car.arrivingTime.getMinute(), car.arrivingTime.getSecond())); - output.append("\nCars on Bridge: ").append(carsOnBridge).append("\n"); - return output.toString(); + StringBuilder output = new StringBuilder("\nAmount of cars on Bridge: "); + output.append(carsOnBridge).append("\n").append("Size of queue: ").append(waitingCars.size()).append("\nAverage waiting time of the last ").append(Variables.AVG_PRINT_MINUTES).append(" Minutes: "); + waitingTimes.headMap(System.currentTimeMillis() - Variables.AVG_PRINT_MINUTES * 60_000, false).clear(); + return output.append((waitingTimes.values().stream().mapToDouble(Long::longValue).average().orElse(0)) / 1000).append(" Seconds").toString(); } @@ -45,7 +43,7 @@ public class Guard extends Thread { if (carsOnBridge < Variables.MAX_CARS_ON_BRIDGE && !this.waitingCars.isEmpty()) crossingBridge(this.waitingCars.getFirst()); } - if (System.currentTimeMillis() - lastPrint >= Variables.PRINT_INTERVAL) { + if (System.currentTimeMillis() - lastPrint >= Variables.PRINT_INTERVAL * 1000) { System.out.println(this); lastPrint = System.currentTimeMillis(); } diff --git a/src/UE21_290425_Bruecke/Variables.java b/src/UE21_290425_Bruecke/Variables.java index 8740fe1..431d024 100644 --- a/src/UE21_290425_Bruecke/Variables.java +++ b/src/UE21_290425_Bruecke/Variables.java @@ -3,8 +3,8 @@ package UE21_290425_Bruecke; public class Variables { public static final int MAX_WAITING_CARS = 999_999_999; // Can be used so that the programm doesn't run infinite public static final int MAX_CAR_INTERVAL = 5; // Maximal Intervall in witch the cars can arrive (1 - X seconds) - public static final int CROSSING_TIME = 30_000; // The time a car needs to cross the bridge - public static final int MAX_CARS_ON_BRIDGE = 3; // Maximal amount of cars on the bridge at the same time - public static final int PRINT_INTERVAL = 2_000; // Amount of seconds between every Statistics output - public static final boolean SHOW_CARS = true; // Decide if you want a graphical visualisation + public static final int CROSSING_TIME = 50; // The time a car needs to cross the bridge (Seconds) + public static final int MAX_CARS_ON_BRIDGE = 10; // Maximal amount of cars on the bridge at the same time + public static final int PRINT_INTERVAL = 10; // Amount of seconds between every Statistics output + public static final int AVG_PRINT_MINUTES = 10; // Last X minutes AVG waiting time of the cars (Minutes) }