aboutsummaryrefslogtreecommitdiff
path: root/Week14 Route 66/src/com/camilstaps/route66/Overviewer.java
diff options
context:
space:
mode:
Diffstat (limited to 'Week14 Route 66/src/com/camilstaps/route66/Overviewer.java')
-rw-r--r--Week14 Route 66/src/com/camilstaps/route66/Overviewer.java54
1 files changed, 54 insertions, 0 deletions
diff --git a/Week14 Route 66/src/com/camilstaps/route66/Overviewer.java b/Week14 Route 66/src/com/camilstaps/route66/Overviewer.java
new file mode 100644
index 0000000..762ded1
--- /dev/null
+++ b/Week14 Route 66/src/com/camilstaps/route66/Overviewer.java
@@ -0,0 +1,54 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package com.camilstaps.route66;
+
+import OO14route66.Car;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ *
+ * @author pieterkoopman
+ */
+public class Overviewer {
+
+ private static Overviewer instance;
+
+ Map<Car, Integer> locations;
+
+ protected Overviewer() {
+ locations = new HashMap<>();
+ }
+
+ public static Overviewer getInstance() {
+ if (instance == null) {
+ instance = new Overviewer();
+ }
+ return instance;
+ }
+
+ public synchronized void setLocation(Car car, int location) {
+ if (locations.containsKey(car)) {
+ locations.remove(car);
+ }
+ locations.put(car, location);
+ }
+
+ public synchronized boolean isSafeLocation(Car car, int requested_location) {
+ for (Map.Entry<Car, Integer> location : locations.entrySet()) {
+ Car that_car = location.getKey();
+ if (that_car != car &&
+ that_car.getDirection() == car.getDirection() &&
+ that_car.getLocation() > requested_location &&
+ that_car.getLocation() < requested_location + Car.CARLENGTH + Car.MINCARSPACE) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+}