Merge branch 'menu'

This commit is contained in:
HeshamTB 2020-12-02 02:03:29 +03:00
commit 177e22ef13
Signed by: Hesham
GPG Key ID: 74876157D199B09E
7 changed files with 227 additions and 24 deletions

View File

@ -29,7 +29,15 @@ public class Bus extends CivilVehicle {
public int getTimeToFix() {
return TIME_TO_FIX;
}
@Override
public String toString() {
StringBuilder s = new StringBuilder();
s.append(super.toString());
s.append(String.format("ID: %s, Campaign ID: %s\n",this.getUID() , getCampaign().getUID()));
return s.toString();
}
private void generateUID() {
numeberOfBuses++;
this.UID = String.format("BUS%04d", numeberOfBuses);

46
src/InputListener.java Normal file
View File

@ -0,0 +1,46 @@
import java.util.Scanner;
public class InputListener implements Runnable {
private volatile String input = "";
private volatile boolean hasNew;
private volatile boolean pause;
private final Scanner in;
private boolean stop;
public InputListener() {
in = new Scanner(System.in);
}
@Override
public void run() {
while(!stop) {
if (!pause){
input = in.next();
hasNew = true;
}
}
System.out.println("Stopped input listener");
}
public void stop() {
stop = true;
}
public String getInput() {
hasNew = false;
return input;
}
public boolean hasNew() {
return hasNew;
}
public void pause() {
pause = true;
}
public void unpause() {
pause = false;
}
}

View File

@ -24,8 +24,12 @@ public class MakkahCity {
private static PDate currenttimeManager = firstDayTimeMan;
private static final InputListener inputListener = new InputListener();
private static final Thread t = new Thread(inputListener,"InputThread-Makkah");
public static void main(String[] args) {
t.start();
//Gen Camp
campPerDistrict[District.ALMANSOOR.ordinal()] = new ArrayList<>();
campPerDistrict[District.ALAZIZIYA.ordinal()] = new ArrayList<>();
@ -45,6 +49,7 @@ public class MakkahCity {
//Set Routes for Campaigns
setRoutesForCampaigns(Mashier.ARAFAT);
while(!firstDayTimeMan.isEnded()) {
checkInput();
//Start of Every hour
if (firstDayTimeMan.getCurrentCalendar().get(Calendar.MINUTE) == 0){
System.out.println("\n\n" + getStreetsReport());
@ -89,6 +94,7 @@ public class MakkahCity {
}
System.out.println("***************STARTING LAST DAY***************");
while(!lastDayTimeMan.isEnded()) {
checkInput();
//Start of Every hour
if (lastDayTimeMan.getCurrentCalendar().get(Calendar.MINUTE) == 0){
System.out.println("\n\n" + getStreetsReport());
@ -124,9 +130,103 @@ public class MakkahCity {
if (isAllArrived()) allArrivedToHotelsTime = (Date)currenttimeManager.getCurrentTime().clone();
lastDayTimeMan.step(Calendar.MINUTE, 1);
}
inputListener.stop();
t.interrupt();
//TODO: print final report
}
private static void checkInput() {
String input = "";
if (inputListener.hasNew()){
input = inputListener.getInput();
if (input.equals("m")){
System.out.println("PAUSED");
inputListener.pause();
startMenu();
inputListener.unpause();
}
}
}
private static void startMenu() {
//TODO: add used by (District) in street menu as option
//TODO: add capacity to street list output avg time too?
Scanner in = new Scanner(System.in);
System.out.println("\n"+currenttimeManager.getCurrentTime()+"\n"+
"---------------------------\n" +
"[1] View Vehicles\n" +
"[2] View Streets\n" +
"[3] View Campaigns\n" +
"[4] View Routes\n" +
"[5] Print report\n" +
"[6] Continue\n" +
"[7] Exit");
String choice = in.next();
//Split into methods?
if (choice.equals("1")){
System.out.printf("choose from 0 to %d\n", listOfVehicles.size()-1);
String c = in.next();
Vehicle v = listOfVehicles.get(Integer.parseInt(c));
showVehicle(v);
//meybe add option here to go to members (Campaign, Street ...)
}
if (choice.equals("2")){
for (int i = 0; i < stdStreet.length; i++) {
System.out.printf("[%d] %s\n",i, stdStreet[i].getName().name());
}
String input = in.next();
int index = Integer.parseInt(input);//TODO: unhandled ex
showStreet(stdStreet[index]);
}
if (choice.equals("4")){
for (int i = 0; i < stdRoutes.length; i++){
int count = 0;
for (Campaign campaign : listOfCampaigns)
if (campaign.getRoute() == stdRoutes[i])
count += campaign.getVehicles().size();
System.out.printf("[%d] %sUsed By %d buses\n\n", i, stdRoutes[i], count);
}
}
if (choice.equals("5")) System.out.println(getStreetsReport());
if (choice.equals("6")) return;
if (choice.equals("7")) {
inputListener.stop();
t.interrupt();
System.exit(0);
}
startMenu();
}
private static void showVehicle(Vehicle vehicle) {
System.out.print("\n\n"+vehicle.getUID()+"\n"+vehicle.toString()+"\n\n");
//TODO: Continue here for more options or return
}
private static void showCampaign(Campaign campaign){
}
private static void showStreet(Street street) {
String choice = "";
Scanner in = new Scanner(System.in);
System.out.println("\n"+street.getName().name());
System.out.println("---------------------------\n"+
"[1] Details\n" +
"[2] Select vehicle\n" +
"[3] Get history for vehicle (NOT IMPLEMENTED YET)\n" +
"[4] Return");
choice = in.next();
if (choice.equals("1")) System.out.println("\n\n"+street.toString());
if (choice.equals("2")){
System.out.printf("Select from 0 to %d\n", street.getVehicles().size());
choice = in.next();
showVehicle(street.getVehicles().get(Integer.parseInt(choice)));
}
if (choice.equals("4")) return;
showStreet(street);
}
private static void clearDoneCivilVehicles() {
//Clear civil cars from list
for (int i = 0; i < listOfVehicles.size();){
@ -418,7 +518,7 @@ public class MakkahCity {
report.append("\n");
}
report.append("\n").append(getFinalRep()).append("\n");
report.append(preSimulationReport());
report.append(preSimulationReport()).append("Type m+Enter to view menu");
return report.toString();
}

View File

@ -42,6 +42,21 @@ public class Route implements Travelable {
return result;
}
@Override
public String toString() {
StringBuilder s = new StringBuilder();
s.append(super.toString())
.append("\n").append(String.format("%s:%s",getHotelArea(),getMashier()))
.append("\n")
.append("Length: ").append(getTotalLength())
.append("\n")
.append("Streets: ");
for (Street street : this.getStreets())
s.append(street.getName().name()).append(" ");
s.append("\nBest Time: ").append(getFastestTimeOfTravel(new Bus())).append("\n");
return s.toString();
}
public District getHotelArea() {
return hotelArea;
}

View File

@ -1,21 +1,33 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
public class Street implements Travelable {
private double length;
private int numberOfLanes;
private ArrayList<Vehicle> vehicles;
private ArrayList<Vehicle> vehicles; //Current
private HashMap<Vehicle, ArrayList<Date>> vehiclesHistory;//History of vehicles
private StreetNames name;
public Street(double length, int numberOfLanes, StreetNames name) {
vehicles = new ArrayList<>();
vehiclesHistory = new HashMap<>();
setLength(length);
setNumberOfLanes(numberOfLanes);
this.name = name;
}
public Date[] getHistoryForVehicle(Vehicle vehicle){
if (vehiclesHistory.containsKey(vehicle)){
Date[] hist = new Date[vehiclesHistory.get(vehicle).size()];
return vehiclesHistory.get(vehicle).toArray(hist);
}
return null;
}
private void setLength(double length) {
if (length >= 0) this.length = length;
else throw new IllegalArgumentException("Can not make a negative length street");
@ -83,10 +95,15 @@ public class Street implements Travelable {
}
public void addVehicle(Vehicle vehicle) {
//if(capcity() > vehicle.getVehicleSize() + 0.5) {
//adds incoming vehicle in last.
vehicles.add(vehicle);
//}
vehicles.add(vehicle);
addHistoryEntry(vehicle);
}
private void addHistoryEntry(Vehicle vehicle) {
if (!vehiclesHistory.containsKey(vehicle)) {
vehiclesHistory.put(vehicle, new ArrayList<>());
}
vehiclesHistory.get(vehicle).add(MakkahCity.getTimeMan().getCurrentTime());//Add time
}
public double capcityPoint(double min, double max) {
@ -154,21 +171,6 @@ public class Street implements Travelable {
return number;
}
public boolean isContainsBuses() {
for (Vehicle vehicle : this.vehicles) {
if (vehicle instanceof Bus)
return true;
}
return false;
}
public String getFastestTimeOfTravel(Vehicle vehicle) {
double totalLength = length;
int maxSpeed = vehicle.getMaxSpeed();
int totalTime = (int) (totalLength/maxSpeed);
return String.format("%02d:%02d",totalTime / 60, totalTime % 60);
}
public String toString(){
StringBuilder printedStreet = new StringBuilder();
//Imagine steert is 32 units in lengths (scale down)
@ -191,11 +193,27 @@ public class Street implements Travelable {
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",
return String.format("Street name: %s, Length: %.2f, Lanes: %d, Vehicles: %d, Capacity: %%%s\nDensity:\n%s\n",
name.name(),
length,
numberOfLanes,
vehicles.size(),
getPercentRemainingCapacity(),
printedStreet.toString());
}
public boolean isContainsBuses() {
for (Vehicle vehicle : this.vehicles) {
if (vehicle instanceof Bus)
return true;
}
return false;
}
public String getFastestTimeOfTravel(Vehicle vehicle) {
double totalLength = length;
int maxSpeed = vehicle.getMaxSpeed();
int totalTime = (int) (totalLength/maxSpeed);
return String.format("%02d:%02d",totalTime / 60, totalTime % 60);
}
}

View File

@ -28,4 +28,9 @@ public class TrafficPoliceCar extends Vehicle implements CanBeGovtCar, CanFixAcc
public int getMaxSpeed() {
return Sedan.MAX_FORWARD;
}
@Override
public String getUID() {
return TPC_UID;
}
}

View File

@ -18,6 +18,7 @@ public abstract class Vehicle {
private HashMap<Street, Integer> routeTimeHistory = new HashMap<>();
public abstract int getMaxSpeed();
public abstract String getUID();
public Vehicle(double vehicleSize){
setVehicleSize(vehicleSize);
@ -166,7 +167,17 @@ public abstract class Vehicle {
public boolean hasCrossedStreet(Street street) {
return routeTimeHistory.containsKey(street);
}
public String toString() {
Street st = this.currentStreet;
String streetString = "null";
if (st != null) streetString = st.getName().name();
return String.format("%s\nRoute: %s\nStreet: %s, Location: %.1f\n" +
"Arrived: %s Starting time: %s Arrive Time: %s\n",
super.toString(), this.route, streetString,
this.getCurrentLocation(), this.isArrivedToDest(),
this.getTimeStartedMoving(), this.getTimeOfArrival());
}
}