Need help with do while loop
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Eric_Budiyono
Posted On:   Tuesday, April 17, 2007 01:52 PM

Hi I'm a beginner in Java thingy. I'm trying to do the program for the vending machine. Just a simple program. I'm stuck in doing the do while loop and switch case, if the user entered 3 invalid inputs, and the program should go back to the first menu and start all over again. I mean I've been using counter for the invalid inputs, i just not quite understand how to do it. I made some coding, it looks like this: import java.text.NumberFormat; public class Assignment2 { public static void main (String[] args) { //Variable Declaration String header = ""; String footer = ""; String typeOfDrink = ""; //The avai   More>>

Hi I'm a beginner in Java thingy.
I'm trying to do the program for the vending machine. Just a simple program.

I'm stuck in doing the do while loop and switch case, if the user entered 3 invalid inputs, and the program should go back to the first menu and start all over again. I mean I've been using counter for the invalid inputs, i just not quite understand how to do it.

I made some coding, it looks like this:

			
import java.text.NumberFormat;

public class Assignment2
{
public static void main (String[] args)
{
//Variable Declaration
String header = "";
String footer = "";
String typeOfDrink = ""; //The available selections on the Drink's menu
String typeOfSweetness = ""; //The available selections on the Sweetness's menu
String typeOfMoney = ""; //The available selections on the Money's menu
String drink = ""; //The selection on the Drink's menu
String sweetness = ""; //The selection on the Sweetness's menu
String menuDrink = ""; //The menu of Drinks
String menuSweetness = ""; //The menu of Sweetness
String menuMoney = ""; //The menu of Money
String enter = " ";
double amountInserted = 0.00; //The Money input from the User
double change = 0.00; //The change from the Money
double money = 0.00; //The selection on the Money's menu
double price = 0.00;
char optionOfDrink = ' ';
char optionOfSweetness = ' ';
char optionOfMoney = ' ';
int count = 0; //Counter for the invalid input
boolean mainMenu = false; //Conditions of continuing to second menu
boolean secondMenu = false; //Conditions of continuing to last menu
boolean thirdMenu = false;
//Universal part
header = " -----------------------------------------
Campus Coffee Machine
-----------------------------------------
";
footer = " -----------------------------------------
Please make your selection : ";

//The structure of available selections of drink
typeOfDrink = " A. Espresso
B. Caffe Latte - Milk, Coffee
C. Cappuccino - Milk, Coffee
D. Mochaccino - Milk, Chocolate, Coffee
E. Cap-Ciok - Milk, Chocolate, Coffee
";
menuDrink = header + typeOfDrink + footer;

//The structure of available selections of sweetness
typeOfSweetness =" A. No Sugar
B. Sweet
C. Very Sweet
Z. Cancel
";
menuSweetness = header + typeOfSweetness + footer;

//The structure of available selection of money
typeOfMoney = " A. $2.00
B. $1.00
C. $0.50
D. 0.20
E. 0.10
Z. Cancel
";
menuMoney = header + typeOfMoney + footer;
do
{


//Displaying the Drink's Menu
do
{

System.out.print (menuDrink);
optionOfDrink = Console.readChar(); //Making selections for drink

switch (optionOfDrink)
{
case 'a' : case 'A' :
drink = "Espresso";
price = 1.80;
mainMenu = true;
break;
case 'b' : case 'B' :
drink = "Caffe Latte";
price = 2.00;
mainMenu = true;
break;
case 'c' : case 'C' :
drink = "Cappuccino";
price = 2.00;
mainMenu = true;
break;
case 'd' : case 'D' :
drink = "Mochaccino";
price = 2.20;
mainMenu = true;
break;
case 'e' : case 'E' :
drink = "Cap-Ciok";
price = 2.20;
mainMenu = true;
break;
default : System.out.println (" Invalid choice."); //If any invalid inputs given
System.out.println (" Please press enter to continue"); //user will be prompted to re-enter
enter = Console.readString(); //his/her choice.
mainMenu = false;

}
}
while (mainMenu != true);

optionOfSweetness = Console.readChar();


do
{
if (count < 3)
{
switch (optionOfSweetness)
{
case 'a' : case 'A' :
sweetness = "No Sugar";
mainMenu = true;
secondMenu = true;
count = 0;
break;
case 'b' : case 'B' :
sweetness = "Sweet";
mainMenu = true;
secondMenu = true;
count = 0;
break;
case 'c' : case 'C' :
sweetness = "Very Sweet";
mainMenu = true;
secondMenu = true;
count = 0;
break;
case 'z' : case 'Z' :
mainMenu = false;
secondMenu = true;
count = 0;
break;
default : System.out.println ("Invalid choice.");
System.out.println ("Please press enter to continue");
enter = Console.readString();
mainMenu = true;
secondMenu = false;
count = count + 1;
}
}
else
{
mainMenu = false;
secondMenu = true;

}
}
while (secondMenu != true);
}
while (mainMenu != true);
}
}
   <<Less

Re: Need help with do while loop

Posted By:   Robert_Lybarger  
Posted On:   Tuesday, April 17, 2007 11:06 PM

I would personally make each level of menu prompts be a separate method call ... it'll make writing it, understanding it, and testing/debugging it simpler. The only "trick" here is your requirement that the outer (main) menu will continue to prompt forever, but the inner menu will prompt only up to three times, at which point it triggers the outer menu to re-prompt and start over. (You don't indicate if there is an exit mode for the main menu, such as if the vendee wants their money back...) If you split the menus to separate methods, the inner method needs to communicate back what action has taken place: user cancelled explicitly, system erroring for too many invalid options, valid option selected. Consider:

public static final int INVALID=0;
public static final int CANCEL=1;
public static final int VALID=2;

public static void main(String[] args) {
...
int userChoice = showTopMenu();
...
}

private static int showTopMenu() {
int responseCode;
do {
//prompt user.
//read user response.

switch (user response) {
// process user response and set the
// responseCode to 0, 1, or 2. set
// whatever else you need based on
// the selection (cost, etc.)
}

switch (responseCode) {
case CANCEL:
return CANCEL;
case VALID:
int subResponseCode = showSecondMenu();
if (subResponseCode==INVALID) {
responseCode=INVALID;
}
break;
case INVALID:
}
} while (responseCode==INVALID);
return responseCode;
}

private static int showSecondMenu() {
int responseCode;
int errorCount = 0;
do {
//prompt user for second-level.
//read user response.

switch (user response) {
// process user response and set the
// responseCode to 0, 1, or 2. set
// whatever else you need based on
// the selection.
}

switch (responseCode) {
case INVALID:
errorCount++;
//error message for user?
}
} while (responseCode==INVALID && errorCount<3);
return responseCode;
}


Of course, while you are defining constants at the top of the file, you should probably define another one called "MAX_ERROR_COUNT" and use that in place of a hard-coded "3" in the do-while conditional.


Another advantage to separate-method-for-separate-menu is it gives more flexibility to change the logical flow between menus : what happens if a menu level is inserted *between* the two you already have, for example.


For even better credit later on, the static constants should probably be an 'enum' type. As written above w/ constants, it'll all go down the toilet just as soon as some misbehaving method returns, say, "9" or "-1". switch-defaults can catch those, but enum types eliminate them in the first place.

About | Sitemap | Contact