Question
I need modification in my java code. currently this code has only 1 item which is soda just add 6 to 7 items like Snacks,
I need modification in my java code. currently this code has only 1 item which is soda
just add 6 to 7 items like Snacks, chocolate, candy, chips, nuts, coffee etc
i need changes in this code.
File: VendingMachine.java
// VendingMachine class implementation public class VendingMachine { // constant declarations private final double SODA_COST = 0.65; private final int MAX_SODAS = 50; // declare attributes private int sodaCount; private double moneyCredit;
// constructor implementation public VendingMachine() { sodaCount = MAX_SODAS; moneyCredit = 0; }
// addMoney method implementation public void addMoney(double amount) { moneyCredit = moneyCredit + amount; }
// vend method implementation public double vend() { if(sodaCount == 0) { System.out.println("No enough stock is in the machine to get soda."); return moneyCredit; } if(moneyCredit < SODA_COST) { System.out.println("No enough money is in the machine to get soda."); return moneyCredit; } sodaCount = sodaCount - 1; moneyCredit = moneyCredit - SODA_COST; return moneyCredit; }
// restock method implementation public void restock() { sodaCount = MAX_SODAS; System.out.println("Restocked"); } } // end of VendingMachine class
File: VendingMachineDemo.java
// VendingMachineDemo class implementation import java.util.Scanner; public class VendingMachineDemo { // start main method public static void main(String[] args) { // create an object for VendingMachine class VendingMachine machine = new VendingMachine(); // create an object for Scanner class Scanner input = new Scanner(System.in); // variables declaration int choice; double amount; // repeat the loop until the user wants to exit do { // display the menu options System.out.println("----MENU----"); System.out.println("1. Add money"); System.out.println("2. Vend"); System.out.println("3. Restock"); System.out.println("4. Exit"); // prompt the user to enter a menu choice System.out.print("Enter your choice: "); choice = input.nextInt(); // switch statement switch(choice) { case 1: // Add money System.out.print("Enter money to be added: $"); amount = input.nextDouble(); machine.addMoney(amount); break; case 2: // Vend amount = machine.vend(); System.out.printf("Returned amount: $%.2f ", amount); break; case 3: // Restock machine.restock(); break; case 4: // Exit System.out.println("Thank you"); break; default: // invalid option System.out.println("Invalid choice"); } System.out.println(); }while(choice != 4); } // end of main method } // end of VendingMachineDemo class
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