@MadProgrammer, the switch-case function stops after one choice. I can't seem to get it to go back to OnlineStore( ). I tried do-while statement. It didn’t work. It will go to default then back to the menu regardless of the input, 1 or 2.
import javax.swing.JOptionPane;
public class OnlineStore
{
String[] ColorType = {"blue", "green", "black"};
final int COLOURS = 3; // t choices
int[] Color = new int[COLOURS];
int sum, mselect;
public static int display_menu() // Not the main program but the main menu.
{
String input;
int mselect;
input = JOptionPane.showInputDialog("Welcome!" + "\n\n1. Add tshirt order\n2. Edit tshirt order\n3. View current order\n4. Checkout" + "\n\nPlease enter your choice: ");
// return Integer.parseInt(input);
mselect = Integer.parseInt(input);
return 0;
}
public OnlineStore() // Switch-case program
{
do
{
display_menu();
switch (mselect)
{
case 1:
add_t();
break;
case 2:
edit_t();
break;
default: // If an error is encountered.
JOptionPane.showMessageDialog(null, "Oh dear! Error!");
break;
}
} while (mselect < 3);
}
public final int add_t()
{
for (int index = 0; index < ColorType.length; index++)
{
<output>
}
return Color.length;
}
public final int edit_t()
{
for (int index = 0; index < ColorType.length; index++)
{
<output>
}
public static void main(String[] args) // Main program
{
new OnlineStore(); // Call out the program.
}
}
First...
public static int display_menu() // Not the main program but the main menu.
{
String input;
int mselect;
input = JOptionPane.showInputDialog("Welcome!" + "\n\n1. Add tshirt order\n2. Edit tshirt order\n3. View current order\n4. Checkout" + "\n\nPlease enter your choice: ");
// return Integer.parseInt(input);
mselect = Integer.parseInt(input);
return 0;
}
You aren't returning mselect
, but are returning 0
. Instead you should probably do something more like...
public static int display_menu() // Not the main program but the main menu.
{
String input;
input = JOptionPane.showInputDialog("Welcome!" + "\n\n1. Add tshirt order\n2. Edit tshirt order\n3. View current order\n4. Checkout" + "\n\nPlease enter your choice: ");
// return Integer.parseInt(input);
return Integer.parseInt(input);
}
Next, you are using the mselect
, which has no value to check in your switch
public OnlineStore() // Switch-case program
{
do {
display_menu();
switch (mselect) {
case 1:
add_t();
break;
case 2:
edit_t();
break;
default: // If an error is encountered.
JOptionPane.showMessageDialog(null, "Oh dear! Error!");
break;
}
} while (mselect < 3);
}
but instead, you should be using the returned value from display_menu()
public OnlineStore() // Switch-case program
{
do {
switch (display_menu()) {
case 1:
add_t();
break;
case 2:
edit_t();
break;
default: // If an error is encountered.
JOptionPane.showMessageDialog(null, "Oh dear! Error!");
break;
}
} while (mselect < 3);
}
Be VERY wary of static
, it's not your friend and can easily turn a nice running program into garbage.
Instead, you could do something more like...
public class OnlineStore {
String[] ColorType = {"blue", "green", "black"};
final int COLOURS = 3; // t choices
int[] Color = new int[COLOURS];
int sum;
public int display_menu() // Not the main program but the main menu.
{
String input;
input = JOptionPane.showInputDialog("Welcome!" + "\n\n1. Add tshirt order\n2. Edit tshirt order\n3. View current order\n4. Checkout" + "\n\nPlease enter your choice: ");
// return Integer.parseInt(input);
return Integer.parseInt(input);
}
public OnlineStore() // Switch-case program
{
boolean exit = false;
do {
switch (display_menu()) {
case 1:
add_t();
break;
case 2:
edit_t();
break;
case 4:
exit = true;
break;
default: // If an error is encountered.
JOptionPane.showMessageDialog(null, "Oh dear! Error!");
break;
}
} while (!exit);
}
public final int add_t() {
return 0;
}
public final int edit_t() {
return 0;
}
public static void main(String[] args) // Main program
{
new OnlineStore(); // Call out the program.
}
}