Question
Write a program that simulates a vending machine. The vending machine sells three types of food: 1) Potato chips $1.25; 2) Cookies $0.85; 3) Candies
Write a program that simulates a vending machine.
The vending machine sells three types of food: 1) Potato chips $1.25; 2) Cookies $0.85; 3) Candies $0.95. The program will prompt for the buyer to enter the amount in quarters (25 cents), dimes (10 cents), and nickels (5 cents). The program will then present a selection menu for the foods. The buyer makes a selection. If the buyer selects a food that is sold out, the program will display a message and re-display the selection menu. If the buyer has put in enough money, the selected food will be dispensed, with a message "Please take a your (food)". If the amount is more than the cost, an appropriate amount of change (in quarter, dime or nickel) will be dispensed.
Every food is initialized with a quantity, the user can only buy one item of each(use for loop). The quantity is reduced by one every time the food is dispensed. When the quantity of a food is 0, no more should be sold, with an error stating "Sold out. Please make another choice".
Note:
1 dollar = 4 quarters = 10 dimes = 20 nickels 1 dollar = 100 cents 1 quarter = 25 cents 1 dime = 10 cents 1 nickel = 5 cents
Requirements
Please do all of the following:
Create a Java source code file named VendingMachine.java. Make sure the file compile correctly.
3. Test your program with the following test cases: 3.1. Buyer selects a food that is available, with exact amount as the price; 3.2 Buyer selects a food that is in stock, with less money than the price; 3.3 Buyer selects a food that is in stock, with more money than the price; 3.4 Buyer selects a food that is sold out; 3.5 Buyer selects a food that does not exist. Repeat the test cases for all the foods. Capture the interactions and save in a file.
_____________________________________________________________________________________________________________________________
This is the code I wrote but I wanted to add Every food is initialized with a quantity, the user can only buy one item of each(use for loop). The quantity is reduced by one every time the food is dispensed. When the quantity of a food is 0, no more should be sold, with an error stating "Sold out. Please make another choice".
_____________________________________________________________________________________________________________________________
package myproject;
/* * Wed Feb 5 * Somya Shah * Lab 2 Vending Machine * This program reads the users input of money and chosen item * and returns whether the user has enough money to take the item * */
import java.util.Scanner;
public class VendingMachine { //class name VendingMachine
public static void main(String[] args) { int choice; double qty[] = { 20, 20, 20 }; int quartersE, dimesE, nickelE; double total = 0; double dispenceAmt = 0, cost = 0; final int QUARTER = 25, DIMES = 10, NICKELS = 5; /* * Creating an Scanner class object which is used to get the inputs * entered by the user */ Scanner sc = new Scanner(System.in); // Create new Scanner object
// Getting the input entered by the user System.out.println("1) Potato chips $1.25 "); System.out.println("2) Cookies $0.85 "); System.out.println("3) Candies $0.95"); System.out.println("Enter Quarters :"); quartersE = sc.nextInt(); System.out.println("Enter Dimes :"); dimesE = sc.nextInt(); System.out.println("Enter Nickels :"); nickelE = sc.nextInt(); total = quartersE * 0.25 + dimesE * 0.10 + nickelE * 0.05; while (true) { //while loop whether the user can take the item System.out.print("Choose an item :"); choice = sc.nextInt(); switch (choice) { case 1: { cost =total; if (cost >= 1.25) { if (qty[0] > 0) { System.out.println("Please take a your Chips."); qty[0] -= 1; if (cost > 1.25) { dispenceAmt = cost - 1.25; } } else { System.out.println("Chips not available.Please select another Item"); }
} else { System.out.print("Not have enough money "); } break; } case 2: { cost =total; if (cost >= 0.85) { if (qty[1] > 0) { System.out.println("Please take a your Cookies."); qty[1] -= 1; if (cost > 0.85) { dispenceAmt = cost - 0.85; } } else { System.out .println("Cookies not available.Please select another Item"); } } else { System.out.print("Not have enough money "); } break; } case 3: { cost = total; if (cost >= 0.95) { if (qty[1] > 0) { System.out.println("Please take a your Candies."); qty[2] -= 1; if (cost > 0.95) { dispenceAmt = cost - 0.95; } } else { System.out.println("Candies not available.Please select another Item"); } } else { System.out.print("Not have enough money "); } break; } default: { System.out.println("** Invalid Choice(does not exist) **"); continue; }
} break; }
if(dispenceAmt>0) { int cents=(int)Math.round(dispenceAmt*100); int no_Of_Quarters = cents / QUARTER; int result1 = cents % QUARTER; // calculating the number of Dimes denominations int no_Of_Dimes = result1 / DIMES; int result2 = result1 % DIMES; // Calculating the number of Nickels denominations int no_Of_Nickels = result2 / NICKELS; System.out.println(""); System.out.println(" Your change :"); if(no_Of_Quarters>0) System.out.println("Quarters :"+no_Of_Quarters); if(no_Of_Dimes>0) System.out.println("Dimes :"+no_Of_Dimes); if(no_Of_Nickels>0) System.out.println("Nickels :"+no_Of_Nickels);
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started