public class Road {
  double limit, len;
  boolean loopRoad = false;
  Car[] cars = new Car[100];
  
  public double speedLimit(double where){
    if(where > (len*.7))
      return (limit/5);
    else
      return limit;
  }
  
  //
  public Car nextCar(double where){
    Car closest=null;
    for(int ct=0;ct<100;ct++){
      if(cars[ct] == null)
	continue;
      if(cars[ct].position == where)
	continue;
      if(closest == null)
	closest = cars[ct];
      if( ((cars[ct].position-where) > 0) &&
	       ((cars[ct].position-where) < (closest.position-where)))
	closest = cars[ct];
      else if((((cars[ct].position-where+len)%len) < ((closest.position-where+len)%len)) && loopRoad)
	closest = cars[ct];
    }
    return closest;
  }

  public void tick(long tn){
    for(int ct=0;ct<100;ct++){
      if(cars[ct] != null){
	cars[ct].tick(tn);
	if(cars[ct].position > len)
	  if(loopRoad)
	    cars[ct].position = cars[ct].position%len;
	  else{
	    System.out.println("Car "+ct+" is off the road...");
	    cars[ct] = null;
	  }
      }
    }
  }
}

