Street toString()
- Print street details and fields. - Print scaled street density.
This commit is contained in:
parent
86651b3a91
commit
336106c7ed
@ -161,4 +161,34 @@ public class Street {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String toString(){
|
||||||
|
StringBuilder printedStreet = new StringBuilder();
|
||||||
|
//Imagine steert is 32 units in lengths (scale down)
|
||||||
|
for (int i = 0; i < 32; i++){
|
||||||
|
printedStreet.append("----");
|
||||||
|
}
|
||||||
|
printedStreet.append("\n");
|
||||||
|
//This is vary bad. Should copy vehicles list and reduce when a vehicle is counted;
|
||||||
|
for (int i = 0; i < 32; i++){
|
||||||
|
int cars = 0;
|
||||||
|
for (Vehicle vehicle : vehicles){
|
||||||
|
double realLocation = vehicle.getCurrentLocation();
|
||||||
|
int scaledLocation = (int)((realLocation*32) / this.length);
|
||||||
|
if (scaledLocation == i) cars++;
|
||||||
|
}
|
||||||
|
//if 'cars' is 4 digits or more it will distort the output
|
||||||
|
printedStreet.append(String.format("%03d ", cars));
|
||||||
|
}
|
||||||
|
printedStreet.append("\n");
|
||||||
|
for (int i = 0; i < 32; i++){
|
||||||
|
printedStreet.append("----");
|
||||||
|
}
|
||||||
|
return String.format("Street name: %s, Length: %f, Lanes: %d, Vehicles: %d\nDensity:\n%s\n",
|
||||||
|
name.name(),
|
||||||
|
length,
|
||||||
|
numberOfLanes,
|
||||||
|
vehicles.size(),
|
||||||
|
printedStreet.toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user