So I am trying to implement a menu that loops in a CLI simulator, I have the menu implemented but I am stuck figuring out how to make it loop or progress to the next option that requires input from the user. I have used scanner in for the menu, see attached files for what I have done. There are 5 attached java files and probably could make it so there is only three of them which would include the cars class when I make it. I have access to netbeans and IntelliJ to work on it.
Simes 0 Light Poster
package cp1300assignment1;
/**
*
* @author Simes
*/
public class AssignmentModel {
private int horizontalLaneNum;
private int verticalLaneNum;
private int gridSize;
public int getGridSize() {
return gridSize;
}
public void setGridSize(int gridSize) {
this.gridSize = gridSize;
}
public AssignmentModel() {
init();
}
private void init() {
horizontalLaneNum = 1;
verticalLaneNum = 1;
gridSize = 23;
}
public int horizontalLaneNum() {
return horizontalLaneNum;
}
public void sethorizontalLaneNum(int horizontalLaneNum) {
this.horizontalLaneNum = horizontalLaneNum;
}
public int getverticalLaneNum() {
return verticalLaneNum;
}
public void verticalLaneNum(int verticalLaneNum) {
this.verticalLaneNum = verticalLaneNum;
}
int gethorizontalLaneNum() {
return horizontalLaneNum;
}
}
package cp1300assignment1;
/**
*
* @author Simes
*/
public class GridModel {
private int horizontalLaneNum;
private int verticalLaneNum;
private int gridSize;
public int getGridSize() {
return gridSize;
}
public void setGridSize(int gridSize) {
this.gridSize = gridSize;
}
public GridModel() {
init();
}
private void init() {
horizontalLaneNum = 1;
verticalLaneNum = 1;
gridSize = 23;
}
public int horizontalLaneNum() {
return horizontalLaneNum;
}
public void sethorizontalLaneNum(int horizontalLaneNum) {
this.horizontalLaneNum = horizontalLaneNum;
}
public int getverticalLaneNum() {
return verticalLaneNum;
}
public void verticalLaneNum(int verticalLaneNum) {
this.verticalLaneNum = verticalLaneNum;
}
public void setverticalLaneNum(int verticalLaneNum) {
this.verticalLaneNum = verticalLaneNum;
}
int gethorizontalLaneNum() {
return horizontalLaneNum;
}
}
package cp1300assignment1;
/**
*
* @author Simes
*/
public class GridView {
private char[][] grid;
private GridModel 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 = '?';
}
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);
}
}
}
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] = ' ';
}
}
}
}
}
package cp1300assignment1;
/**
*
* @author Simes
*/
public class Main {
/**
* This initializes my program
* @param args command line parameters
*/
public static void main(String[] args) {
new MainUI().run();
}
}
package cp1300assignment1;
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"
+ "Enter your choice> ");
}
void run() {
GridView gv = new GridView(project);
gv.show();
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]");
int choice = in.nextInt();
if (choice > 0 && choice < 4) {
project.sethorizontalLaneNum(in.nextInt());
break; }
}
}
if (menuChoice == 2) {
while (true) {
System.out.println("Please set the number of vertical lanes [min 1, max 4]");
int choice = in.nextInt();
if (choice > 0 && choice < 5) {
project.setverticalLaneNum(in.nextInt());
break; }
}
}
if (menuChoice == 3) {
while (true) {
System.out.println("Please set the probability of a car entering H-street [min 0, max 1]");
int choice = in.nextInt();
if (choice > 0 && choice < 2) {
project.setverticalLaneNum(in.nextInt());
break; }
}
}
if (menuChoice == 4) {
while (true) {
System.out.println("Please set the probability of a car entering V-street [min 0, max 1]");
int choice = in.nextInt();
if (choice > 0 && choice < 2) {
project.setverticalLaneNum(in.nextInt());
break; }
}
}
if (menuChoice == 5) {
while (true) {
System.out.println("Run one simulation cycle");
int choice = in.nextInt();
if (choice > 0 && choice < 5) {
project.setverticalLaneNum(in.nextInt());
break; }
}
}
if (menuChoice == 6) {
while (true) {
System.out.println("Set and run number of simulation cycles [min 1, max 10]");
int choice = in.nextInt();
if (choice > 0 && choice < 11) {
project.setverticalLaneNum(in.nextInt());
break; }
}
}
}
}
NormR1 563 Posting Sage Team Colleague
You have too many files for anyone to work with easily.
Can you make a small simple program that compiles and executes and demonstrates the problem and post that here?
Simes 0 Light Poster
ok attached is the compile jar file of those files.
This attachment is potentially unsafe to open. It may be an executable that is capable of making changes to your file system, or it may require specific software to open. Use caution and only open this attachment if you are comfortable working with zip files.
NormR1 563 Posting Sage Team Colleague
Sorry, I dont' like executing code I don't create from source.
Can you make a small simple program that compiles and executes and demonstrates the problem and post that here?
Simes 0 Light Poster
There isn't a problem, I just need it to loop after each menu choice to display the menu again or go to the next option in the menu. Which if you grab the original java files and compile them you will see.
Edited by Simes because: n/a
NormR1 563 Posting Sage Team Colleague
Can you make a small simple program that compiles and executes and demonstrates the problem and post that here?
A small simple program is easier to work with than the whole project which will have lots of code not related to the problem.
Simes 0 Light Poster
It is all related as the menu calls on variables in other files to do its options
NormR1 563 Posting Sage Team Colleague
Have you tried moving some of the code to methods?
Have a loop to call the methods in the order that you need.
Have boolean flags to keep track of the order of processing.
Make a small simple program that compiles and executes and demonstrates the problem
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.