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);
}
}