The following code in Java is supposed to represent a vending machine that termi
ID: 641662 • Letter: T
Question
The following code in Java is supposed to represent a vending machine that terminates the program when "X" is selected and gives an invalid entry when the Money variable is less than or equal to zero. There is a problem in the code for the second half and I cannot figure out what it is. Please help me figure out what this problem is!
import java.util.Scanner;
public class VendingMachine1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
double money = 0;
final double milk = 2.50;
final double soda = 2.25;
final double candybar = 1.25;
final double gummybears = 1.50;
final double chips = 1.00;
int quantitymilk =5;
int quantitysoda =4;
int quantitycandybar =5;
int quantitygummybear =6;
int quantitychips =6;
String selection="";
while (money >= 0 &&!selection.equalsIgnoreCase("X")) {
System.out.println("Welcome to Patrick's Vending Machine! We have all sorts of goodies for you:");
System.out.println(" Item Price Quantity"); //Start of the table
System.out.println("Milk $2.50 " +quantitymilk); //Price of Milk and Quantity
System.out.println("Soda $2.25 " +quantitysoda); //Price of Soda and Quantity
System.out.println("Candy Bar $1.25 " +quantitycandybar); //Price of Candy Bar and Quantity
System.out.println("Gummy Bears $1.50 " +quantitygummybear); //Price of Gummy Bears and Quantity
System.out.println("Chips $1.00 " +quantitychips); //Price of Chips and Quantity
System.out.println("What is the amount of money you wish to spend?");
money =scan.nextDouble();
if (money >= 0); {
System.out.println("The amount of money you wish to spend is " + money);
}
while (!selection.equalsIgnoreCase("X")) {
System.out.println("Please select an option:");
System.out.println("A-Milk B-Soda C-Candy Bar D-Gummy Bear E-Chips X-Escape");
selection =scan.next();
if (selection.equalsIgnoreCase("A")) {
quantitymilk--;
money = money -milk;
System.out.println("You have selected Milk. You have:" + money);
}
if (selection.equalsIgnoreCase("B")) {
quantitysoda--;
money = money -soda;
System.out.println("You have selected Soda. You have:" + money);
}
if (selection.equalsIgnoreCase("C")) {
quantitycandybar--;
money = money -candybar;
System.out.print("You have selected Candy Bar. You have:" + money);
}
if (selection.equalsIgnoreCase("D")) {
quantitygummybear--;
money = money -gummybears;
System.out.println("You have selected Gummy Bears. You have:" + money);
}
if (selection.equalsIgnoreCase("E")) {
quantitychips--;
money = money -chips;
System.out.println("You have selected Chips. You have:" +money);
}
if (selection.equalsIgnoreCase("X")) {
System.out.println("Thank you for choosing Patrick's Vending Machine!");
}
}
}
}
}
Explanation / Answer
import java.util.Scanner;
public class VendingMachine1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
double money = 0;
final double milk = 2.50;
final double soda = 2.25;
final double candybar = 1.25;
final double gummybears = 1.50;
final double chips = 1.00;
int quantitymilk =5;
int quantitysoda =4;
int quantitycandybar =5;
int quantitygummybear =6;
int quantitychips =6;
String selection="";
while (money >= 0 &&!selection.equalsIgnoreCase("X")) {
System.out.println("Welcome to Patrick's Vending Machine! We have all sorts of goodies for you:");
System.out.println(" Item Price Quantity"); //Start of the table
System.out.println("Milk $2.50 " +quantitymilk); //Price of Milk and Quantity
System.out.println("Soda $2.25 " +quantitysoda); //Price of Soda and Quantity
System.out.println("Candy Bar $1.25 " +quantitycandybar); //Price of Candy Bar and Quantity
System.out.println("Gummy Bears $1.50 " +quantitygummybear); //Price of Gummy Bears and Quantity
System.out.println("Chips $1.00 " +quantitychips); //Price of Chips and Quantity
System.out.println("What is the amount of money you wish to spend?");
money =scan.nextDouble();
if (money >= 0) {
System.out.println("The amount of money you wish to spend is " + money);
while (!selection.equalsIgnoreCase("X")) {
System.out.println("Please select an option:");
System.out.println("A-Milk B-Soda C-Candy Bar D-Gummy Bear E-Chips X-Escape");
selection =scan.next();
if (selection.equalsIgnoreCase("A")) {
quantitymilk--;
money = money -milk;
System.out.println("You have selected Milk. You have:" + money);
}
if (selection.equalsIgnoreCase("B")) {
quantitysoda--;
money = money -soda;
System.out.println("You have selected Soda. You have:" + money);
}
if (selection.equalsIgnoreCase("C")) {
quantitycandybar--;
money = money -candybar;
System.out.print("You have selected Candy Bar. You have:" + money);
}
if (selection.equalsIgnoreCase("D")) {
quantitygummybear--;
money = money -gummybears;
System.out.println("You have selected Gummy Bears. You have:" + money);
}
if (selection.equalsIgnoreCase("E")) {
quantitychips--;
money = money -chips;
System.out.println("You have selected Chips. You have:" +money);
}
if (selection.equalsIgnoreCase("X")) {
System.out.println("Thank you for choosing Patrick's Vending Machine!");
}
}
}
else{
System.out.println("Invalid Entry");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.