2020-10-21 02:15:03 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
public class Street {
|
|
|
|
|
|
|
|
private double length;
|
|
|
|
private int numberOfLanes;
|
|
|
|
private ArrayList<Vehicle> vehicles;
|
2020-11-11 22:12:59 +01:00
|
|
|
private StreetNames name;
|
2020-11-08 14:03:44 +01:00
|
|
|
|
2020-11-17 15:45:21 +01:00
|
|
|
|
2020-10-21 02:15:03 +02:00
|
|
|
|
2020-11-11 22:12:59 +01:00
|
|
|
public Street(double length, int numberOfLanes, StreetNames name) {
|
2020-11-10 18:15:11 +01:00
|
|
|
vehicles = new ArrayList<>();
|
2020-10-21 02:15:03 +02:00
|
|
|
setLength(length);
|
|
|
|
setNumberOfLanes(numberOfLanes);
|
2020-11-11 22:12:59 +01:00
|
|
|
this.name = name;
|
2020-10-21 02:15:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void setLength(double length) {
|
|
|
|
if (length >= 0) this.length = length;
|
|
|
|
else throw new IllegalArgumentException("Can not make a negative length street");
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setNumberOfLanes(int numberOfLanes) {
|
|
|
|
if (numberOfLanes >= 1) this.numberOfLanes = numberOfLanes;
|
|
|
|
else throw new IllegalArgumentException("Street can not have less then 1 lane");
|
|
|
|
}
|
|
|
|
|
|
|
|
public double getLength() {
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
2020-11-09 22:03:29 +01:00
|
|
|
/**
|
|
|
|
* Gets total length of one lane * number of lanes
|
|
|
|
* As if it is a sigle longer lane.
|
|
|
|
* @return Total length of all lanes combined
|
|
|
|
*/
|
|
|
|
public double getCombinedLength() {
|
|
|
|
return this.length * this.numberOfLanes;
|
|
|
|
}
|
|
|
|
|
2020-10-21 02:15:03 +02:00
|
|
|
public int getNumberOfLanes() {
|
|
|
|
return numberOfLanes;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ArrayList<Vehicle> getVehicles() {
|
|
|
|
return vehicles;
|
|
|
|
}
|
2020-11-09 10:24:51 +01:00
|
|
|
|
2020-11-11 22:12:59 +01:00
|
|
|
public StreetNames getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2020-11-08 14:03:44 +01:00
|
|
|
public double capcity() {
|
2020-11-09 10:24:51 +01:00
|
|
|
double totalLength = length * numberOfLanes;
|
|
|
|
double totalLenthofCar=0;
|
|
|
|
for(int i=0;i<vehicles.size();i++) {
|
|
|
|
totalLenthofCar+=vehicles.get(i).getVehicleSize();
|
|
|
|
}
|
2020-11-17 18:18:17 +01:00
|
|
|
// int percent = 0;
|
|
|
|
// if (totalLenthofCar > totalLength){
|
|
|
|
// percent = (int) (totalLength/totalLenthofCar);
|
|
|
|
// percent *= 100;
|
|
|
|
// percent += totalLength - (totalLength % totalLenthofCar);
|
|
|
|
// }
|
|
|
|
// else {
|
|
|
|
// percent = (int)(totalLength - totalLenthofCar);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return percent;
|
|
|
|
return totalLength - totalLenthofCar;
|
2020-11-08 14:03:44 +01:00
|
|
|
}
|
2020-11-09 22:54:31 +01:00
|
|
|
|
|
|
|
public int getPercentRemainingCapacity() {
|
2020-11-18 17:28:38 +01:00
|
|
|
return (int) (100 - (capcity()/(this.length*this.numberOfLanes)*100));
|
2020-11-09 22:54:31 +01:00
|
|
|
}
|
2020-11-08 14:03:44 +01:00
|
|
|
|
|
|
|
public boolean canTakeVehicles( Vehicle vehicle ) {
|
2020-11-14 23:21:15 +01:00
|
|
|
if ( vehicle.getVehicleSize() < capcity() )
|
2020-11-08 14:03:44 +01:00
|
|
|
return true;
|
2020-11-10 18:15:11 +01:00
|
|
|
else
|
|
|
|
return false;
|
2020-11-08 14:03:44 +01:00
|
|
|
}
|
2020-11-09 10:24:51 +01:00
|
|
|
|
2020-11-09 10:30:18 +01:00
|
|
|
public void addVehicle(Vehicle vehicle) {
|
2020-11-13 20:35:52 +01:00
|
|
|
//if(capcity() > vehicle.getVehicleSize() + 0.5) {
|
2020-11-09 10:30:18 +01:00
|
|
|
//adds incoming vehicle in last.
|
|
|
|
vehicles.add(vehicle);
|
2020-11-13 20:35:52 +01:00
|
|
|
//}
|
2020-11-08 14:03:44 +01:00
|
|
|
}
|
2020-11-13 23:30:20 +01:00
|
|
|
|
|
|
|
public double capcityPoint(double min, double max) {
|
|
|
|
double totalLength = (max - min) * numberOfLanes;
|
|
|
|
double totalLenthofCar=0;
|
|
|
|
for(int i=0;i<vehicles.size();i++) {
|
|
|
|
if (vehicles.get(i).getCurrentLocation() >= min &&
|
|
|
|
vehicles.get(i).getCurrentLocation() <= max)
|
|
|
|
totalLenthofCar+=vehicles.get(i).getVehicleSize();
|
|
|
|
}
|
2020-11-14 23:21:15 +01:00
|
|
|
double capcity = totalLenthofCar / totalLength;
|
|
|
|
if (capcity > 1)
|
|
|
|
return 1;
|
|
|
|
else if (capcity < 0 )
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return capcity;
|
2020-11-13 23:30:20 +01:00
|
|
|
}
|
2020-11-17 15:45:21 +01:00
|
|
|
|
|
|
|
public double capcityPoint(double min, double max, Vehicle vehicle) {
|
|
|
|
|
|
|
|
if(max > getLength() && getNextStreet(vehicle) != null) {
|
|
|
|
double reamingLength = max - getLength();
|
|
|
|
double y = capcityPoint(min, getLength(),vehicle);
|
|
|
|
double z = getNextStreet(vehicle).capcityPoint(0, reamingLength, vehicle);
|
|
|
|
return ((y + z)/2);
|
|
|
|
}
|
|
|
|
double totalLength = (max - min) * numberOfLanes;
|
|
|
|
double totalLenthofCar=0;
|
|
|
|
for(int i=0;i<vehicles.size();i++) {
|
|
|
|
if (vehicles.get(i).getCurrentLocation() >= min &&
|
|
|
|
vehicles.get(i).getCurrentLocation() <= max)
|
|
|
|
totalLenthofCar+=vehicles.get(i).getVehicleSize();
|
|
|
|
}
|
|
|
|
double capcity = totalLenthofCar / totalLength;
|
|
|
|
if (capcity > 1)
|
|
|
|
return 1;
|
|
|
|
else if (capcity < 0 )
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return capcity;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Street getNextStreet(Vehicle vehicle) {
|
|
|
|
int nextIndex = vehicle.getRoute().indexOf(vehicle.getCurrentStreet()) +1 ;
|
|
|
|
if(vehicle.getRoute().getStreets().length > nextIndex)
|
|
|
|
return (vehicle.getRoute().getStreets()[nextIndex]);
|
|
|
|
else
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2020-11-17 18:44:10 +01:00
|
|
|
|
|
|
|
public int getNumberOfBuses() {
|
|
|
|
int number = 0;
|
|
|
|
for (Vehicle vehicle : this.getVehicles()) {
|
|
|
|
if (vehicle instanceof Bus) number++;
|
|
|
|
}
|
|
|
|
return number;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int getNumberOfLocalCars() {
|
|
|
|
int number = 0;
|
|
|
|
for (Vehicle vehicle : this.getVehicles()) {
|
|
|
|
if (vehicle instanceof CivilVehicle && !(vehicle instanceof Bus)) number++;
|
|
|
|
}
|
|
|
|
return number;
|
|
|
|
}
|
2020-10-21 02:15:03 +02:00
|
|
|
}
|