So I am working on a Traffic Simulator and I am having a fair bit of trouble with it. I need the menu to display, user chooses the option enters a value for the option and then either it loops with the menu again and they can set other options. Or should I have pop up dialog windows that allow show up immediately starting with menu option 1 and progressing from there. Anyway have a look and see where and what is wrong and what I need to do to fix it, so I can compile it and run it. When compiled it just needs to run from DOS and use ASCII characters for the grid. - is for horizontal lanes, | is for vertical lanes and c is used to represent the cars.
package TrafficSim;
/**
*
* @author Simes
*/
public class Main {
/**
* This initializes my program
* @param args command line parameters
*/
public static void main(String[] args) {
new MainUI().run();
}
}
package TrafficSim;
import java.util.Scanner;
/**
*
* @author Simes
*/
public class MainUI {
private GridModel project;
/**
*
*/
public MainUI() {
project = new GridModel();
}
/**
*
*/
public void showMenu() {
System.out.println("=== MENU ===\n"
+ "1. Set Number of horizontal lanes (H-street) [min 1, max 3]\n"
+ "2. Set Number of vertical lanes (V-street) [min 1, max 4]\n"
+ "3. Set Probability of a car entering H-street [min 0, max 1]\n"
+ "4. Set Probability of a car entering V-street [min 0, max 1]\n"
+ "5. Run one simulation cycle\n"
+ "6. Set and run number of simulation cycles [min 1, max 10]\n"
+ "7. Exit the program \n"
+ "Enter your choice> ");
}
void run() {
GridView gv = new GridView(project);
while (true) {
gv.show();
boolean exit = true;
do {
showMenu();
Scanner in = new Scanner(System.in);
int menuChoice = in.nextInt();
if (menuChoice == 1) {
while (true) {
System.out.println("Please set the number of horizontal lanes [min 1, max 3]");
try {
int choice = in.nextInt();
if (choice > 0 && choice <= 3) { // reprints the above sout statement if the choice is outside of the range in the if statement
project.sethorizontalLaneNum(in.nextInt());
break;
}
} catch (Exception e) { // reprints above sout statement if anything else besides 1, 2 or 3 is entered as the input choice
}
//in.nextLine();
}
}
else if (menuChoice == 2) {
while (true) {
System.out.println("Please set the number of vertical lanes [min 1, max 4]");
try {
int choice = in.nextInt();
if (choice > 0 && choice < 5) { // reprints the above sout statement if the choice is outside of the range in the if statement
project.setverticalLaneNum(in.nextInt());
break;
}
} catch (Exception e) { // reprints above sout statement if anything else besides 1, 2, 3 or 4 is entered as the input choice
}
if (menuChoice == 3) {
while (true) {
System.out.println("Please set the probability of a car entering H-street [min 0, max 1]");
try {
int choice = in.nextInt();
if (choice > 0 && choice < 2) {
project.sethorizontalCarProbability(in.nextInt());
break;
}
} catch (Exception e) {
}
if (menuChoice == 4) {
while (true) {
System.out.println("Please set the probability of a car entering V-street [min 0, max 1]");
try {
int choice = in.nextInt();
if (choice > 0 && choice < 2) {
project.setverticalCarProbability(in.nextInt());
break;
}
} catch (Exception e) {
}
if (menuChoice == 5) {
while (true) {
System.out.println("Run one simulation cycle");
try {
int choice = in.nextInt();
if (choice > 0 && choice < 5) {
project.setrunCycle(in.nextInt());
break;
}
} catch (Exception e) {
}
if (menuChoice == 6) {
while (true) {
System.out.println("Set and run number of simulation cycles [min 1, max 10]");
try {
int choice = in.nextInt();
if (choice > 0 && choice < 11) {
project.setsimulationCycle(in.nextInt());
break;
}
} catch (Exception e) {
}
if (menuChoice == 7) {
while (true) {
System.out.println("Thank you for using the Traffic Simulator");
try {
int choice = in.nextInt();
if (choice == 7) {
project.setExit(true);
break;
}
} catch (Exception e) {
}
}
}
}
}
}
}
}
}
}
}
}
}
} while (exit != true);
}
}
}
package TrafficSim;
import TrafficSim.GridModel;
/**
*
* @author Simes
*/
public class Cars {
private int Car;
private char[][] grid;
private final Cars project;
/**
*
* @param project
*/
public Cars(Cars project) {
this.project = project;
int gs = project.getCar();
grid = new char[gs][gs];
setCar('c');
buildhorizontalCars(project.gethorizontalCar());
buildverticalCars(project.getverticalCar());
}
/**
*
* @return
*/
public char[][] getGrid() {
return grid;
}
/**
*
* @param grid
*/
public void setGrid(char[][] grid) {
this.grid = grid;
}
/**
*
* @return
*/
public int getCar() {
return Car;
}
/**
*
* @param Car
*/
public void setCar(int Car) {
this.Car = Car;
}
/**
*
* @return
*/
public int gethorizontalCar() {
return Car;
}
/**
*
* @param Car
*/
public void sethorizontalCar(int Car) {
this.Car = Car;
}
/**
*
* @return
*/
public int getverticalCar() {
return Car;
}
/**
*
* @param Car
*/
public void setverticalCar(int Car) {
this.Car = Car;
}
private void setCar(char c) {
}
void show() {
for (int r = 0; r < grid.length; r++) {
char[] tempRow = grid[r];
for (int column = 0; column < tempRow.length; column++) {
char character = grid[r][column];
System.out.print(character);
}
System.out.println("");
}
}
private void buildhorizontalCar(int horizontalCar) {
int nr = grid.length;
int middle = nr / 2 +- 1;
for (int r = 0; r < nr; r++) {
char[] row = grid[r];
if (r == middle) {
setRow(row, ' ');
}
}
}
private void setRow(char[] row, char c) {
for (int i = 0; i < row.length; i++) {
row[i] = c;
}
}
private void buildverticalCar(int verticalCar) {
int nc = project.getGridSize();
int middle = nc / 2 +- 1;
for (int row = 0; row < grid.length; row++) {
for (int column = 0; column < grid.length; column++) {
if (column == middle) {
grid[row][column] = ' ';
}
}
}
}
}
package TrafficSim;
import java.util.Random;
/**
*
* @author Simes
*/
public class AssignmentModel {
private int horizontalLaneNum;
private int verticalLaneNum;
private int gridSize;
private int horizontalCarProbability;
private int verticalCarProbability;
/**
*
* @return
*/
public int getGridSize() {
return gridSize;
}
/**
*
* @param gridSize
*/
public void setGridSize(int gridSize) {
this.gridSize = gridSize;
}
/**
*
*/
public AssignmentModel() {
init();
}
private void init() {
horizontalLaneNum = 1;
verticalLaneNum = 1;
gridSize = 50;
horizontalCarProbability = 1;
verticalCarProbability = 1;
}
/**
*
* @return
*/
public int horizontalLaneNum() {
return horizontalLaneNum;
}
/**
*
* @param horizontalLaneNum
*/
public void sethorizontalLaneNum(int horizontalLaneNum) {
this.horizontalLaneNum = horizontalLaneNum;
}
/**
*
* @return
*/
public int getverticalLaneNum() {
return verticalLaneNum;
}
/**
*
* @param verticalLaneNum
*/
public void verticalLaneNum(int verticalLaneNum) {
this.verticalLaneNum = verticalLaneNum;
}
int gethorizontalLaneNum() {
return horizontalLaneNum;
}
/**
*
* @return
*/
public int getverticalCarProbability() {
return verticalCarProbability;
}
/**
*
* @return
*/
public int gethorizontalCarProbability() {
return horizontalCarProbability;
}
/**
*
* @param verticalCarProbability
*/
public void setverticalCarProbability(int verticalCarProbability) {
this.verticalCarProbability = verticalCarProbability;
}
/**
*
* @param horizontalCarProbability
*/
public void sethorizontalCarProbability(int horizontalCarProbability) {
this.horizontalCarProbability = horizontalCarProbability;
}
}
package TrafficSim;
/**
*
* @author Simes
*/
public class GridModel {
private int horizontalLaneNum;
private int verticalLaneNum;
private int gridSize;
private int horizontalCarProbability;
private int verticalCarProbability;
private int simulationCycle;
private int runCycle;
private boolean Exit;
/**
*
* @return
*/
public int getGridSize() {
return gridSize;
}
/**
*
* @param gridSize
*/
public void setGridSize(int gridSize) {
this.gridSize = gridSize;
}
/**
*
*/
public GridModel() {
init();
}
private void init() {
horizontalLaneNum = 1;
verticalLaneNum = 1;
gridSize = 50;
horizontalCarProbability = 1;
verticalCarProbability = 1;
simulationCycle = 10;
runCycle = 1;
}
/**
*
* @return
*/
public int horizontalLaneNum() {
return horizontalLaneNum;
}
/**
*
* @param horizontalLaneNum
*/
public void sethorizontalLaneNum(int horizontalLaneNum) {
this.horizontalLaneNum = horizontalLaneNum;
}
/**
*
* @return
*/
public int getverticalLaneNum() {
return verticalLaneNum;
}
/**
*
* @param verticalLaneNum
*/
public void verticalLaneNum(int verticalLaneNum) {
this.verticalLaneNum = verticalLaneNum;
}
/**
*
* @param verticalLaneNum
*/
public void setverticalLaneNum(int verticalLaneNum) {
this.verticalLaneNum = verticalLaneNum;
}
int gethorizontalLaneNum() {
return horizontalLaneNum;
}
/**
*
* @return
*/
public int getverticalCarProbability() {
return verticalCarProbability;
}
/**
*
* @return
*/
public int gethorizontalCarProbability() {
return horizontalCarProbability;
}
/**
*
* @param verticalCarProbability
*/
public void setverticalCarProbability(int verticalCarProbability) {
this.verticalCarProbability = verticalCarProbability;
}
/**
*
* @param horizontalCarProbability
*/
public void sethorizontalCarProbability(int horizontalCarProbability) {
this.horizontalCarProbability = horizontalCarProbability;
}
/**
*
* @return
*/
public int getsimulationCycle() {
return simulationCycle;
}
/**
*
* @param simulationCycle
*/
public void setsimulationCycle(int simulationCycle) {
this.simulationCycle = simulationCycle;
}
/**
*
* @return
*/
public int getrunCycle() {
return runCycle;
}
/**
*
* @param runCycle
*/
public void setrunCycle(int runCycle){
this.runCycle = runCycle;
}
/**
*
* @return
*/
public boolean isExit() {
return Exit;
}
/**
*
* @param Exit
*/
public void setExit(boolean Exit) {
this.Exit = Exit;
}
}
package TrafficSim;
/**
*
* @author Simes
*/
public class GridView {
private char[][] grid;
private GridModel project;
/**
*
* @param project
*/
public GridView(GridModel project) {
this.project = project;
// int horizontalnumber = project.gethorizontalLaneNum();
// int verticalnumber = project.getverticalLaneNum();
// grid = new char[horizontalnumber][verticalnumber];
int gs = project.getGridSize();
grid = new char[gs][gs];
setAll('?');
buildHorizontalStreet(project.gethorizontalLaneNum());
buildVerticalStreet(project.getverticalLaneNum());
// char character = '?';
}
/**
*
* @param character
*/
public void setAll(char character) {
for (int row = 0; row < grid.length; row++) {
char[] tempRow = grid[row];
for (int column = 0; column < tempRow.length; column++) {
//row[column] = character;
grid[row][column] = character;
//System.out.println("" + character);
}
}
}
/**
*
* @param row
* @param column
* @param value
*/
public void set(int row, int column, char value) {
grid[row][column] = value;
}
void show() {
for (int r = 0; r < grid.length; r++) {
char[] tempRow = grid[r];
for (int column = 0; column < tempRow.length; column++) {
char character = grid[r][column];
System.out.print(character);
}
System.out.println("");
}
}
private void buildHorizontalStreet(int horizontalLaneNum) {
int nr = grid.length;
int middle = nr / 2;
for (int r = 0; r < nr; r++) {
char[] row = grid[r];
if (r == middle) {
setRow(row, ' ');
}
}
}
private void setRow(char[] row, char c) {
for (int i = 0; i < row.length; i++) {
row[i] = c;
}
}
private void buildVerticalStreet(int verticalLaneNum) {
int nc = project.getGridSize();
int middle = nc / 2;
for (int row = 0; row < grid.length; row++) {
for (int column = 0; column < grid.length; column++) {
if (column == middle) {
grid[row][column] = ' ';
}
}
}
}
}