I’m writing a simple vending machine program and I’m unsure how to do a few things. So far my program prints a menu with 5 items and repeats until the customer hits ‘x’ or exits. I would like for the loop to also terminate when the customer is out of money, but I’m unsure how to do that. I also tried to make a print statement that says an item is out of stock when the quantity is 0, but I couldn’t get that to work. Any help is appreciated!
import java.util.Scanner; public class Testing { static void MilkA() { System.out.println("\nYou have purchased Milk for $ 2.00"); return; } static void SodaB() { System.out.println("\nYou have purchased Soda for $ 2.25"); return; } static void CandyBarC() { System.out.println("\nYou have purchased a Candy bar for $ 1.25"); return; } static void GummyBearsD() { System.out.println("\nYou have purchased Gummy Bears for $ 1.50"); return; } static void ChipsE() { System.out.println("\nYou have purchased chips for $ 1.00"); return; } static void ExitX() { System.exit(0); } public static void main(String[] args) { Scanner scnr = new Scanner(System.in); System.out.println("Please enter how much money you have to spend (enter -1 to shut down"); double startingAmount = scnr.nextDouble(); double milkPrice = 2.00; double sodaPrice = 2.25; double candyBarPrice = 1.25; double gummyBearsPrice = 1.50; double chipsPrice = 1.00; int milkQuantity = 5; int sodaQuantity = 4; int candyBarQuantity = 5; int gummyBearsQuantity = 6; int chipsQuantity = 6; char quit = 'x'; String choice; Scanner scan = new Scanner(System.in); while (quit != 'y') { System.out.println("Welcome to the Vending Machine!" + "\nI sense that you are hungry... This is what I have to offer:" + "\nPlease make a selection" + "\nItem Price Quantity" + "\nMilk - A $ 2.00 " + milkQuantity + "\nSoda - B $ 2.25 " + sodaQuantity + "\nCandy Bar - C $ 1.2 " + candyBarQuantity + "\nGummy Bears - D $ 1.50 " + gummyBearsQuantity + "\nChips - E $ 1.00 " + chipsQuantity + "\nExit = X"); choice = scan.nextLine().toUpperCase(); switch (choice) { case "A": MilkA(); double amountAfterMilk = startingAmount - milkPrice; double changeMilk = amountAfterMilk -= milkPrice; System.out.println("You now have $ " + changeMilk); milkQuantity -= 1; break; case "B": SodaB(); double amountAfterSoda = startingAmount - sodaPrice; double changeSoda = amountAfterSoda -= sodaPrice; System.out.println("You now have $ " + changeSoda); sodaQuantity -= 1; break; case "C": CandyBarC(); double amountAfterCandyBar = startingAmount - candyBarPrice; double changeCandyBar = amountAfterCandyBar -= candyBarPrice; System.out.println("You now have $ " + changeCandyBar); candyBarQuantity -= 1; break; case "D": GummyBearsD(); double amountAfterGummyBears = startingAmount - gummyBearsPrice; double changeGummyBears = amountAfterGummyBears -= gummyBearsPrice; System.out.println("You now have $ " + changeGummyBears); gummyBearsQuantity -= 1; break; case "E": ChipsE(); double amountAfterChips = startingAmount - chipsPrice; double changeChips = amountAfterChips -= chipsPrice; System.out.println("You now have $ " + changeChips); chipsQuantity -= 1; break; case "X": ExitX(); default: System.out.println("That's not an option."); if (milkQuantity == 0) { System.out.println("Out of Stock"); } } } }
}