954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help with do while loop

hey, I'm a beginner in Java programming.
I'm trying to do the vending machine program. And I'm having difficulty doing the do while loop.

The scenario is if the user entered invalid input 3 times, it will loop back to the first menu using do while loop and switch case. Because I still dont understand the creating a new method way.

Any helps will be appreciated.

l0stb0y2008
Newbie Poster
2 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

can somebody help me to debug this code

import java.text.NumberFormat;

public class kaskus
{
    public static void main (String[] args)
    {
        //Variable Declaration
        NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();
        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 price = 0.00;
        char optionOfDrink = ' ';
        char optionOfSweetness = ' ';
        char optionOfMoney = ' ';
        char money = ' ';
        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;
        boolean limit = false;
        
        //Universal part
        header = "\t\t-----------------------------------------\n      \t\t\tCampus Coffee Machine\n\t\t-----------------------------------------\n";
        footer = "\t\t-----------------------------------------\n\t\tPlease make your selection : ";
        
        //The structure of available selections of drink
        typeOfDrink = "\t\tA. Espresso\n\t\tB. Caffe Latte - Milk, Coffee\n\t\tC. Cappuccino - Milk, Coffee\n\t\tD. Mochaccino - Milk, Chocolate, Coffee\n\t\tE. Cap-Ciok - Milk, Chocolate, Coffee\n"; 
        menuDrink = header + typeOfDrink + footer;
        
        //The structure of available selections of sweetness
        typeOfSweetness ="\t\tA. No Sugar\n\t\tB. Sweet\n\t\tC. Very Sweet\n\t\tZ. Cancel\n";
        menuSweetness = header + typeOfSweetness + footer;
        
        //The structure of available selection of money
        typeOfMoney = "\t\tA. $2.00\n\t\tB. $1.00\n\t\tC. $0.50\n\t\tD. 0.20\n\t\tE. 0.10\n\t\tZ. Cancel\n";
        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 ("\t\t\tInvalid choice.");                    //If any invalid inputs given
                                System.out.println ("\t\t\tPlease press enter to continue");    //user will be prompted to re-enter
                                enter = Console.readString();                                    //his/her choice.
                                mainMenu = false;
                            
                }
            }
            while (mainMenu != true);
             
             
            do
            {
                while (!limit)
                {
                    
                    System.out.print (menuSweetness);
                    optionOfSweetness = Console.readChar();
                
                    switch (optionOfSweetness) 
                    {
                        case 'a' :    case 'A' : 
                                    sweetness = "No Sugar";
                                    mainMenu = true;
                                    secondMenu = true;
                                    thirdMenu = false;
                                    count = 0;
                                    break;
                        case 'b' :     case 'B' :
                                    sweetness = "Sweet";
                                    mainMenu = true;
                                    secondMenu = true;
                                    thirdMenu = false;
                                    count = 0;
                                    break;
                        case 'c' :     case 'C' :
                                    sweetness = "Very Sweet";
                                    mainMenu = true;
                                    secondMenu = true;
                                    thirdMenu = false;
                                    count = 0;
                                    break;
                        case 'z' :     case 'Z' :
                                    mainMenu = false;
                                    secondMenu = true;
                                    thirdMenu = false;
                                    count = 0;
                                    break;
                        default :     System.out.println ("\t\t\tInvalid choice.");
                                    System.out.println ("\t\t\tPlease press enter to continue");
                                    enter = Console.readString();
                                    secondMenu = false; 
                    }
                                    if (optionOfSweetness != 'a' || optionOfSweetness != 'b' || optionOfSweetness != 'c')
                                    count ++;
                                    if (count > 2)
                                    {
                                        limit = true;
                                        System.out.println (menuDrink);
                                    }
                }
                
             
             }
             while (secondMenu != true);
             
             while (thirdMenu != true)
             {
                     System.out.println (menuMoney);
                    optionOfMoney = Console.readChar(); // Prompting for coins
                    
                    switch (optionOfMoney)
                    {
                        case'A': case 'a':
                            amountInserted = amountInserted + 2.00;
                            count = 0;
                            money = '1';
                            if (amountInserted > price)
                            {    
                                change = amountInserted - price;
                                System.out.println ("Press enter to collect your " 
                                        + drink + " - " + sweetness + " and your " 
                                        + moneyFormat.format(change) + " change");
                                enter = Console.readString();
                                amountInserted = 0;
                                mainMenu = false;
                                thirdMenu = true;
                            }
                            else if (amountInserted == price)
                            {
                                System.out.println ("Press enter to collect your " 
                                        + drink + " - " + sweetness );
                                enter = Console.readString();
                                amountInserted = 0;
                                mainMenu = false;
                                thirdMenu = true;
                            }
                            break;
                        case'B': case'b':
                            amountInserted = amountInserted + 1.00;
                            count = 0;
                            money = '1';
                            if (amountInserted > price)
                            {    
                                change = amountInserted - price;
                                System.out.println ("Press enter to collect your " 
                                        + drink + " - " + sweetness + " and your " 
                                        + moneyFormat.format(change) + " change");
                                enter = Console.readString();
                                amountInserted = 0;
                                mainMenu = false;
                                thirdMenu = true;
                            }
                            else if (amountInserted == price)
                            {
                                System.out.println ("Press enter to collect your " 
                                        + drink + " - " + sweetness );
                                enter = Console.readString();
                                amountInserted = 0;
                                mainMenu = false;
                                thirdMenu = true;
                            }
                            break;
                        case'C': case'c':
                            amountInserted = amountInserted + 0.50;
                            count = 0;
                            money = '1';
                            if (amountInserted > price)
                            {    
                                change = amountInserted - price;
                                System.out.println ("Press enter to collect your " 
                                        + drink + " - " + sweetness + " and your " 
                                        + moneyFormat.format(change) + " change");
                                enter = Console.readString();
                                amountInserted = 0;
                                mainMenu = false;
                                thirdMenu = true;
                            }
                            else if (amountInserted == price)
                            {
                                System.out.println ("Press enter to collect your " 
                                        + drink + " - " + sweetness );
                                enter = Console.readString();
                                amountInserted = 0;
                                mainMenu = false;
                                thirdMenu = true;
                            }
                            break;
                        case'D': case'd':
                            amountInserted = amountInserted + 0.20;
                            count = 0;
                            money = '1';
                            if (amountInserted > price)
                            {    
                                change = amountInserted - price;
                                System.out.println ("Press enter to collect your " 
                                        + drink + " - " + sweetness + " and your " 
                                        + moneyFormat.format(change) + " change");
                                enter = Console.readString();
                                amountInserted = 0;
                                mainMenu = false;
                                thirdMenu = true;
                            }
                            else if (amountInserted == price)
                            {
                                System.out.println ("Press enter to collect your " 
                                        + drink + " - " + sweetness );
                                enter = Console.readString();
                                amountInserted = 0;
                                mainMenu = false;
                                thirdMenu = true;
                            }
                            break;
                        case'E': case'e':
                            amountInserted = amountInserted + 0.10;
                            count = 0;
                            money = '1';
                            if (amountInserted > price)
                            {    
                                change = amountInserted - price;
                                System.out.println ("Press enter to collect your " 
                                        + drink + " - " + sweetness + " and your " 
                                        + moneyFormat.format(change) + " change");
                                enter = Console.readString();
                                amountInserted = 0;
                                mainMenu = false;
                                thirdMenu = true;
                            }
                            else if (amountInserted == price)
                            {
                                System.out.println ("Press enter to collect your " 
                                        + drink + " - " + sweetness );
                                enter = Console.readString();
                                amountInserted = 0;
                                mainMenu = false;
                                thirdMenu = true;
                            }
                            break;
                        case'Z': case'z':
                            switch (money)
                            {
                                case '1':
                                System.out.println ("Press enter to collect your " 
                                                    + moneyFormat.format(amountInserted) 
                                                    + " refund.");
                                enter = Console.readString();
                                amountInserted = 0;
                            }
                            mainMenu = false;
                            thirdMenu = true;
                            count = 0;
                            money = '0';
                            break;
                        default:
                        //if there are 3 invalid input, then go back to drink menu
                            count ++;
                            if (count == 3)
                             {
                                 System.out.println ("Invalid choice!");
                                 System.out.println ("Press enter to collect your "
                                                     + moneyFormat.format(amountInserted) 
                                                     + " refund.");
                                 enter = Console.readString();
                                 amountInserted = 0;
                                 thirdMenu = true;
                                 mainMenu = false;    
                             }
                             else
                             {
                            System.out.println ("Invalid choice!");
                             System.out.println ("Press enter to continue.");
                             enter = Console.readString();
                             thirdMenu    = false;
                             }
                    }
            
             }
             while (thirdMenu != true);
             
         }while (mainMenu != true);
     }
 }
l0stb0y2008
Newbie Poster
2 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

import java.text.NumberFormat;

public class coffeeVendingMachine
{
public static void main (String[] args)
{
//Variable Declaration
NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();
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 price = 0.00;
char optionOfDrink = ' ';
char optionOfSweetness = ' ';
char optionOfMoney = ' ';
char money = ' ';
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;
boolean limit = false;

//Universal part
header = "\t\t-----------------------------------------\n \t\t\tCampus Coffee Machine\n\t\t-----------------------------------------\n";
footer = "\t\t-----------------------------------------\n\t\tPlease make your selection : ";

//The structure of available selections of drink
typeOfDrink = "\t\tA. Espresso\n\t\tB. Caffe Latte - Milk, Coffee\n\t\tC. Cappuccino - Milk, Coffee\n\t\tD. Mochaccino - Milk, Chocolate, Coffee\n\t\tE. Cap-Ciok - Milk, Chocolate, Coffee\n";
menuDrink = header + typeOfDrink + footer;

//The structure of available selections of sweetness
typeOfSweetness ="\t\tA. No Sugar\n\t\tB. Sweet\n\t\tC. Very Sweet\n\t\tZ. Cancel\n";
menuSweetness = header + typeOfSweetness + footer;

//The structure of available selection of money
typeOfMoney = "\t\tA. $2.00\n\t\tB. $1.00\n\t\tC. $0.50\n\t\tD. 0.20\n\t\tE. 0.10\n\t\tZ. Cancel\n";
menuMoney = header + typeOfMoney;
do
{


//Displaying the Drink's Menu
do
{
secondMenu = false;
thirdMenu = false;
limit = false;
count = 0;
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 ("\t\t\tInvalid choice."); //If any invalid inputs given
System.out.println ("\t\t\tPlease press enter to continue"); //user will be prompted to re-enter
enter = Console.readString(); //his/her choice.
mainMenu = false;

}
}
while (mainMenu != true);


do
{
while (!limit)
{

System.out.print (menuSweetness);
optionOfSweetness = Console.readChar();

switch (optionOfSweetness)
{
case 'a' : case 'A' :
sweetness = "No Sugar";
mainMenu = true;
secondMenu = true;
thirdMenu = false;
limit = true;
break;
case 'b' : case 'B' :
sweetness = "Sweet";
mainMenu = true;
secondMenu = true;
thirdMenu = false;
limit = true;
break;
case 'c' : case 'C' :
sweetness = "Very Sweet";
mainMenu = true;
secondMenu = true;
thirdMenu = false;
limit = true;
break;
case 'z' : case 'Z' :
mainMenu = false;
secondMenu = true;
thirdMenu = false;
limit = true;
break;
default : System.out.println ("\t\t\tInvalid choice.");
System.out.println ("\t\t\tPlease press enter to continue");
enter = Console.readString();
secondMenu = false;
}
if (optionOfSweetness != 'a' || optionOfSweetness != 'b' || optionOfSweetness != 'c')
count ++;
if (count > 2)
{
limit = true;
secondMenu = true;
mainMenu = false;
count = 0;
}
}

}
while (!secondMenu);

if (mainMenu)
do
{count = 0;
while (!thirdMenu)
{
System.out.println(menuMoney);
System.out.println("\t\t-----------------------------------------\n\t\tDrink: " + drink + " " + "-" + " " + sweetness);
System.out.println();
System.out.println("\t\tPrice: " + "" + moneyFormat.format(price) + " " + "Amount Inserted: "+moneyFormat.format(amountInserted));
System.out.println();
System.out.print("\t\tPlease insert coin : " + " ");
optionOfMoney = Console.readChar(); // Prompting for coins



switch (optionOfMoney)
{
case'A': case 'a':
amountInserted = amountInserted + 2.00;
count = 0;
money = '1';
if (amountInserted > price)
{
change = amountInserted - price;
System.out.println ("\t\tPress enter to collect your "
+ drink + " - " + sweetness + " and your "
+ moneyFormat.format(change) + " change");
enter = Console.readString();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
else if (amountInserted == price)
{
System.out.println ("Press enter to collect your "
+ drink + " - " + sweetness );
enter = Console.readString();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
break;
case'B': case'b':
amountInserted = amountInserted + 1.00;
count = 0;
money = '1';
if (amountInserted > price)
{
change = amountInserted - price;
System.out.println ("Press enter to collect your "
+ drink + " - " + sweetness + " and your "
+ moneyFormat.format(change) + " change");
enter = Console.readString();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
else if (amountInserted == price)
{
System.out.println ("Press enter to collect your "
+ drink + " - " + sweetness );
enter = Console.readString();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
break;
case'C': case'c':
amountInserted = amountInserted + 0.50;
count = 0;
money = '1';
if (amountInserted > price)
{
change = amountInserted - price;
System.out.println ("Press enter to collect your "
+ drink + " - " + sweetness + " and your "
+ moneyFormat.format(change) + " change");
enter = Console.readString();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
else if (amountInserted == price)
{
System.out.println ("Press enter to collect your "
+ drink + " - " + sweetness );
enter = Console.readString();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
break;
case'D': case'd':
amountInserted = amountInserted + 0.20;
count = 0;
money = '1';
if (amountInserted > price)
{
change = amountInserted - price;
System.out.println ("Press enter to collect your "
+ drink + " - " + sweetness + " and your "
+ moneyFormat.format(change) + " change");
enter = Console.readString();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
else if (amountInserted == price)
{
System.out.println ("Press enter to collect your "
+ drink + " - " + sweetness );
enter = Console.readString();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
break;
case'E': case'e':
amountInserted = amountInserted + 0.10;
count = 0;
money = '1';
if (amountInserted > price)
{
change = amountInserted - price;
System.out.println ("Press enter to collect your "
+ drink + " - " + sweetness + " and your "
+ moneyFormat.format(change) + " change");
enter = Console.readString();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
else if (amountInserted == price)
{
System.out.println ("Press enter to collect your "
+ drink + " - " + sweetness );
enter = Console.readString();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
break;
case'Z': case'z':
switch (money)
{
case '1':
System.out.println ("Press enter to collect your "
+ moneyFormat.format(amountInserted)
+ " refund.");
enter = Console.readString();
amountInserted = 0;
}
mainMenu = false;
thirdMenu = true;
count = 0;
money = '0';
break;
default:
//if there are 3 invalid input, then go back to drink menu
count ++;
if (count == 3)
{
System.out.println ("Invalid choice!");
System.out.println ("Press enter to collect your "
+ moneyFormat.format(amountInserted)
+ " refund.");
enter = Console.readString();
amountInserted = 0;
thirdMenu = true;
mainMenu = false;
}
else
{
System.out.println ("Invalid choice!");
System.out.println ("Press enter to continue.");
enter = Console.readString();
thirdMenu = false;
}
}

}
}
while (thirdMenu != true);

}while (mainMenu != true);
}
}import java.text.NumberFormat;

public class coffeeVendingMachine
{
public static void main (String[] args)
{
//Variable Declaration
NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();
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 price = 0.00;
char optionOfDrink = ' ';
char optionOfSweetness = ' ';
char optionOfMoney = ' ';
char money = ' ';
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;
boolean limit = false;

//Universal part
header = "\t\t-----------------------------------------\n \t\t\tCampus Coffee Machine\n\t\t-----------------------------------------\n";
footer = "\t\t-----------------------------------------\n\t\tPlease make your selection : ";

//The structure of available selections of drink
typeOfDrink = "\t\tA. Espresso\n\t\tB. Caffe Latte - Milk, Coffee\n\t\tC. Cappuccino - Milk, Coffee\n\t\tD. Mochaccino - Milk, Chocolate, Coffee\n\t\tE. Cap-Ciok - Milk, Chocolate, Coffee\n";
menuDrink = header + typeOfDrink + footer;

//The structure of available selections of sweetness
typeOfSweetness ="\t\tA. No Sugar\n\t\tB. Sweet\n\t\tC. Very Sweet\n\t\tZ. Cancel\n";
menuSweetness = header + typeOfSweetness + footer;

//The structure of available selection of money
typeOfMoney = "\t\tA. $2.00\n\t\tB. $1.00\n\t\tC. $0.50\n\t\tD. 0.20\n\t\tE. 0.10\n\t\tZ. Cancel\n";
menuMoney = header + typeOfMoney;
do
{


//Displaying the Drink's Menu
do
{
secondMenu = false;
thirdMenu = false;
limit = false;
count = 0;
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 ("\t\t\tInvalid choice."); //If any invalid inputs given
System.out.println ("\t\t\tPlease press enter to continue"); //user will be prompted to re-enter
enter = Console.readString(); //his/her choice.
mainMenu = false;

}
}
while (mainMenu != true);


do
{
while (!limit)
{

System.out.print (menuSweetness);
optionOfSweetness = Console.readChar();

switch (optionOfSweetness)
{
case 'a' : case 'A' :
sweetness = "No Sugar";
mainMenu = true;
secondMenu = true;
thirdMenu = false;
limit = true;
break;
case 'b' : case 'B' :
sweetness = "Sweet";
mainMenu = true;
secondMenu = true;
thirdMenu = false;
limit = true;
break;
case 'c' : case 'C' :
sweetness = "Very Sweet";
mainMenu = true;
secondMenu = true;
thirdMenu = false;
limit = true;
break;
case 'z' : case 'Z' :
mainMenu = false;
secondMenu = true;
thirdMenu = false;
limit = true;
break;
default : System.out.println ("\t\t\tInvalid choice.");
System.out.println ("\t\t\tPlease press enter to continue");
enter = Console.readString();
secondMenu = false;
}
if (optionOfSweetness != 'a' || optionOfSweetness != 'b' || optionOfSweetness != 'c')
count ++;
if (count > 2)
{
limit = true;
secondMenu = true;
mainMenu = false;
count = 0;
}
}

}
while (!secondMenu);

if (mainMenu)
do
{count = 0;
while (!thirdMenu)
{
System.out.println(menuMoney);
System.out.println("\t\t-----------------------------------------\n\t\tDrink: " + drink + " " + "-" + " " + sweetness);
System.out.println();
System.out.println("\t\tPrice: " + "" + moneyFormat.format(price) + " " + "Amount Inserted: "+moneyFormat.format(amountInserted));
System.out.println();
System.out.print("\t\tPlease insert coin : " + " ");
optionOfMoney = Console.readChar(); // Prompting for coins



switch (optionOfMoney)
{
case'A': case 'a':
amountInserted = amountInserted + 2.00;
count = 0;
money = '1';
if (amountInserted > price)
{
change = amountInserted - price;
System.out.println ("\t\tPress enter to collect your "
+ drink + " - " + sweetness + " and your "
+ moneyFormat.format(change) + " change");
enter = Console.readString();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
else if (amountInserted == price)
{
System.out.println ("Press enter to collect your "
+ drink + " - " + sweetness );
enter = Console.readString();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
break;
case'B': case'b':
amountInserted = amountInserted + 1.00;
count = 0;
money = '1';
if (amountInserted > price)
{
change = amountInserted - price;
System.out.println ("Press enter to collect your "
+ drink + " - " + sweetness + " and your "
+ moneyFormat.format(change) + " change");
enter = Console.readString();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
else if (amountInserted == price)
{
System.out.println ("Press enter to collect your "
+ drink + " - " + sweetness );
enter = Console.readString();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
break;
case'C': case'c':
amountInserted = amountInserted + 0.50;
count = 0;
money = '1';
if (amountInserted > price)
{
change = amountInserted - price;
System.out.println ("Press enter to collect your "
+ drink + " - " + sweetness + " and your "
+ moneyFormat.format(change) + " change");
enter = Console.readString();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
else if (amountInserted == price)
{
System.out.println ("Press enter to collect your "
+ drink + " - " + sweetness );
enter = Console.readString();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
break;
case'D': case'd':
amountInserted = amountInserted + 0.20;
count = 0;
money = '1';
if (amountInserted > price)
{
change = amountInserted - price;
System.out.println ("Press enter to collect your "
+ drink + " - " + sweetness + " and your "
+ moneyFormat.format(change) + " change");
enter = Console.readString();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
else if (amountInserted == price)
{
System.out.println ("Press enter to collect your "
+ drink + " - " + sweetness );
enter = Console.readString();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
break;
case'E': case'e':
amountInserted = amountInserted + 0.10;
count = 0;
money = '1';
if (amountInserted > price)
{
change = amountInserted - price;
System.out.println ("Press enter to collect your "
+ drink + " - " + sweetness + " and your "
+ moneyFormat.format(change) + " change");
enter = Console.readString();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
else if (amountInserted == price)
{
System.out.println ("Press enter to collect your "
+ drink + " - " + sweetness );
enter = Console.readString();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
break;
case'Z': case'z':
switch (money)
{
case '1':
System.out.println ("Press enter to collect your "
+ moneyFormat.format(amountInserted)
+ " refund.");
enter = Console.readString();
amountInserted = 0;
}
mainMenu = false;
thirdMenu = true;
count = 0;
money = '0';
break;
default:
//if there are 3 invalid input, then go back to drink menu
count ++;
if (count == 3)
{
System.out.println ("Invalid choice!");
System.out.println ("Press enter to collect your "
+ moneyFormat.format(amountInserted)
+ " refund.");
enter = Console.readString();
amountInserted = 0;
thirdMenu = true;
mainMenu = false;
}
else
{
System.out.println ("Invalid choice!");
System.out.println ("Press enter to continue.");
enter = Console.readString();
thirdMenu = false;
}
}

}
}
while (thirdMenu != true);

}while (mainMenu != true);
}
}

thuyyeu
Newbie Poster
3 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

hi:
you can see this code,but I can't understand that Console meaning.Can you explain that Console Class?
public static void main(String[] args) throws IOException {
// Variable Declaration
NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();
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 price = 0.00;
char optionOfDrink = ' ';
char optionOfSweetness = ' ';
char optionOfMoney = ' ';
char money = ' ';
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;
boolean limit = false;
System.out.println(enter);
// Universal part
header = "\t\t-----------------------------------------\n \t\t\tCampus Coffee Machine\n\t\t-----------------------------------------\n";
footer = "\t\t-----------------------------------------\n\t\tPlease make your selection : ";
// The structure of available selections of drink
typeOfDrink = "\t\tA. Espresso\n\t\tB. Caffe Latte - Milk, Coffee\n\t\tC. Cappuccino - Milk, Coffee\n\t\tD. Mochaccino - Milk, Chocolate, Coffee\n\t\tE. Cap-Ciok - Milk, Chocolate, Coffee\n";
menuDrink = header + typeOfDrink + footer;
// The structure of available selections of sweetness
typeOfSweetness = "\t\tA. No Sugar\n\t\tB. Sweet\n\t\tC. Very Sweet\n\t\tZ. Cancel\n";
menuSweetness = header + typeOfSweetness + footer;
// The structure of available selection of money
typeOfMoney = "\t\tA. $2.00\n\t\tB. $1.00\n\t\tC. $0.50\n\t\tD. 0.20\n\t\tE. 0.10\n\t\tZ. Cancel\n";
menuMoney = header + typeOfMoney + footer;
do {
// Displaying the Drink's Menu
do {
System.out.print(menuDrink);
try {
optionOfDrink = Console.readChar(); // Making selections for
} catch (IOException e) {
throw e;
}
// 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("\t\t\tInvalid choice."); // If any
// invalid
// inputs
// given
System.out.println("\t\t\tPlease press enter to continue"); // user
// will
// be
// prompted
// to
// re-enter
enter = bf.readLine(); // his/her choice.
mainMenu = false;
}
} while (mainMenu != true);
do {
while (!limit) {
System.out.print(menuSweetness);
optionOfSweetness = Console.readChar();
switch (optionOfSweetness) {
case 'a':
case 'A':
sweetness = "No Sugar";
mainMenu = true;
secondMenu = true;
thirdMenu = false;
count = 0;
break;
case 'b':
case 'B':
sweetness = "Sweet";
mainMenu = true;
secondMenu = true;
thirdMenu = false;
count = 0;
break;
case 'c':
case 'C':
sweetness = "Very Sweet";
mainMenu = true;
secondMenu = true;
thirdMenu = false;
count = 0;
break;
case 'z':
case 'Z':
mainMenu = false;
secondMenu = true;
thirdMenu = false;
count = 0;
break;
default:
System.out.println("\t\t\tInvalid choice.");
System.out
.println("\t\t\tPlease press enter to continue");
enter = bf.readLine();
secondMenu = false;
}
if (optionOfSweetness != 'a' || optionOfSweetness != 'b'
|| optionOfSweetness != 'c')
count++;
if (count > 2) {
limit = true;
System.out.println(menuDrink);
}
}
} while (secondMenu != true);
while (thirdMenu != true) {
System.out.println(menuMoney);
optionOfMoney = Console.readChar(); // Prompting for coins
switch (optionOfMoney) {
case 'A':
case 'a':
amountInserted = amountInserted + 2.00;
count = 0;
money = '1';
if (amountInserted > price) {
change = amountInserted - price;
System.out.println("Press enter to collect your "
+ drink + " - " + sweetness + " and your "
+ moneyFormat.format(change) + " change");
enter = bf.readLine();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
} else if (amountInserted == price) {
System.out.println("Press enter to collect your "
+ drink + " - " + sweetness);
enter = bf.readLine();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
break;
case 'B':
case 'b':
amountInserted = amountInserted + 1.00;
count = 0;
money = '1';
if (amountInserted > price) {
change = amountInserted - price;
System.out.println("Press enter to collect your "
+ drink + " - " + sweetness + " and your "
+ moneyFormat.format(change) + " change");
enter = bf.readLine();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
} else if (amountInserted == price) {
System.out.println("Press enter to collect your "
+ drink + " - " + sweetness);
enter = bf.readLine();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
break;
case 'C':
case 'c':
amountInserted = amountInserted + 0.50;
count = 0;
money = '1';
if (amountInserted > price) {
change = amountInserted - price;
System.out.println("Press enter to collect your "
+ drink + " - " + sweetness + " and your "
+ moneyFormat.format(change) + " change");
enter = bf.readLine();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
} else if (amountInserted == price) {
System.out.println("Press enter to collect your "
+ drink + " - " + sweetness);
enter = bf.readLine();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
break;
case 'D':
case 'd':
amountInserted = amountInserted + 0.20;
count = 0;
money = '1';
if (amountInserted > price) {
change = amountInserted - price;
System.out.println("Press enter to collect your "
+ drink + " - " + sweetness + " and your "
+ moneyFormat.format(change) + " change");
enter = bf.readLine();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
} else if (amountInserted == price) {
System.out.println("Press enter to collect your "
+ drink + " - " + sweetness);
enter = bf.readLine();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
break;
case 'E':
case 'e':
amountInserted = amountInserted + 0.10;
count = 0;
money = '1';
if (amountInserted > price) {
change = amountInserted - price;
System.out.println("Press enter to collect your "
+ drink + " - " + sweetness + " and your "
+ moneyFormat.format(change) + " change");
enter = bf.readLine();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
} else if (amountInserted == price) {
System.out.println("Press enter to collect your "
+ drink + " - " + sweetness);
enter = bf.readLine();
amountInserted = 0;
mainMenu = false;
thirdMenu = true;
}
break;
case 'Z':
case 'z':
switch (money) {
case '1':
System.out.println("Press enter to collect your "
+ moneyFormat.format(amountInserted)
+ " refund.");
enter = bf.readLine();
amountInserted = 0;
}
mainMenu = false;
thirdMenu = true;
count = 0;
money = '0';
break;
default:
// if there are 3 invalid input, then go back to drink menu
count++;
if (count == 3) {
System.out.println("Invalid choice!");
System.out.println("Press enter to collect your "
+ moneyFormat.format(amountInserted)
+ " refund.");
enter = bf.readLine();
amountInserted = 0;
thirdMenu = true;
mainMenu = false;
} else {
System.out.println("Invalid choice!");
System.out.println("Press enter to continue.");
enter = bf.readLine();
thirdMenu = false;
}
}
}
while (thirdMenu != true)
;
} while (mainMenu != true);
}

deng_cen
Newbie Poster
14 posts since Apr 2007
Reputation Points: 30
Solved Threads: 4
 

import java.util.Scanner;


/**
* This class reads String and numeric values entered by the user
* at the keyboard and returns String, char and numeric values to the client class.
*
* @author Michael Smith, Dave Nye
* ( Adapted from original idea by Michael Kolling )
* @version 1.6 06/02/2007
*/
public class Console

{


private static final String ERROR_PREFIX = "*Console Message* [ ";

private static final String ERROR_SUFFIX = "] "; //errors bracketed to not surprise students

private static final String IO_ERROR_MESSAGE = "There was an error during reading: ";

private static final String INVALID_INT_MESSAGE = "Input must be an integer number. Re-enter...";

private static final String INVALID_DOUBLE_MESSAGE = "Input must be a real number. Re-enter...";

private static final String INVALID_FLOAT_MESSAGE = "Input must be a real number. Re-enter...";

private static final String INVALID_CHAR_MESSAGE = "Input must be a single character. Re-enter...";

private static Scanner reader = new Scanner(System.in);

private static String inputLine = "";

/**
* This method reads and returns a String entered at the keyboard.
*
* @return a string entered by the user
*/
public static String readString()
{
try
{
inputLine = reader.nextLine();
}
catch (Exception exc)
{
printError(IO_ERROR_MESSAGE + exc.getMessage());
}
return inputLine;
}

/**
* This method reads and returns an int value entered at the keyboard.
* It will loop until input is valid.
*
* @return an int value entered by the user
*/
public static int readInt()
{
int inputInteger = 0;
boolean ok = false;
while(!ok)
{
try
{

inputLine = reader.nextLine();
Scanner input = new Scanner(inputLine);
if(input.hasNextInt())
{
inputInteger = input.nextInt();
ok = true;
}
else
printError(INVALID_INT_MESSAGE);

}
catch (Exception exc)
{
printError(IO_ERROR_MESSAGE + exc.getMessage());
}
}
return inputInteger;
}

/**
* This method reads and returns a double value entered at the console.
* It will loop until input is valid.
*
* @return a double value entered by the user
*/
public static double readDouble()
{
double inputDouble = 0.0;
boolean ok = false;
while(!ok)
try
{
inputLine = reader.nextLine();
Scanner input = new Scanner(inputLine);
if(input.hasNextDouble())
{
inputDouble = input.nextDouble();
ok = true;
}
else
printError(INVALID_DOUBLE_MESSAGE);

}
catch (Exception exc)
{
printError(IO_ERROR_MESSAGE + exc.getMessage());
}

return inputDouble;
}

/**
* This method reads and returns a float value entered at the console.
* It will loop until input is valid.
*
* @return a float value entered by the user
*/
public static float readFloat()
{
float inputFloat = 0.0f;
boolean ok = false;
while(!ok)
try
{
inputLine = reader.nextLine();
Scanner input = new Scanner(inputLine);
if(input.hasNextFloat())
{
inputFloat = input.nextFloat();
ok = true;
}
else
printError(INVALID_FLOAT_MESSAGE);

}
catch (Exception exc)
{
printError(IO_ERROR_MESSAGE + exc.getMessage());
}

return inputFloat;
}

/**
* This method calls readString() to read a String entered at the keyboard.
* It checks the String is one character in length and, if so, returns a char.
* It will loop until input is valid.
*
* @return a char entered by the user
*/
public static char readChar()
{
boolean ok = false;
String input = "";
while(!ok)
{
input = readString();
if(input.length() == 1)
{
ok = true;
}
else
printError(INVALID_CHAR_MESSAGE);
}
return input.charAt(0);
}

/**
* Convenience method for printing error messages.
* Outputs message string between the Console errorPrefix and errorSuffix strings.
*/
private static void printError(String message)
{
System.out.print(ERROR_PREFIX + message + ERROR_SUFFIX);
}
}//end class Console

thuyyeu
Newbie Poster
3 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

you save this console class in the same folder with coffeek.java file, and you'll can run this coffee.java. That's maybe help

thuyyeu
Newbie Poster
3 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You