/* * 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 OO14route66.Model; /** * The Overviewer (always only one instance) tells Drivers when they can drive * * @author Camil Staps */ public class Overviewer { /** * This is a singleton */ private static Overviewer instance; Car[] cars = new Car[Model.NUMBEROFCARS]; /** * This is a singleton * @return the instance */ public static Overviewer getInstance() { if (instance == null) { instance = new Overviewer(); } return instance; } /** * Add a car to the list of cars * @param car */ public synchronized void hello(Car car) { cars[car.getNumber()] = car; } /** * Check if a location is safe for a car to go to. This should always be * checked by a driver before actually driving. * @param car * @param requested_location * @return */ public synchronized boolean isSafeLocation(Car car, int requested_location) { for (Car that_car : cars) { if (that_car != null && 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; } }