Question

Please Walk me through this problem. Write a program that simulates the functionality of a vending...

Please Walk me through this problem. Write a program that simulates the functionality of a vending machine having the following characteristics:

  • The vending machine offers 5 products
  • The vending machine accepts coins, 1 dollar bills, and 5 dollar bills
  • The change is always given in coins, with maximum possible number of coins in each value: 25, 10, 5 or 1 cent.
  • The selections available for user are numbers from 1 to 5.
  • The user enters the money – simulate the action through a loop that ends when the user enters 0. Each coin, or paper bill will be read individually.
  • The user makes the selection, and the machine allows a maximum 4 other selections if the amount entered doesn’t cover the price of the item.
  • Once an item is delivered, the machine gives the change in coins.
  • There is no increment for the money during one selection.
  • The user can stop the selection at any time by entering 0 for the product selection.
  • If the user chooses to cancel the selection, the machine returns the initial amount in coins.
  • Display the outcome of the operation for each alternative you consider possible for the vending machine.
  • Make sure that the machine returns the correct change at all times.

Use appropriate selection and repetition loops to solve the problem.

Write a test program that would take at least 3 combinations of amounts entered and choices of products, and displays the results of all three trials. You can include the vending machine program as a method in the testing program.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

import java.util.Scanner;

public class VendingMachine {
  
   public static void printProducts(){
       System.out.println("1. Product1($7.5)");
       System.out.println("2. Product2($1.5)");
       System.out.println("3. Product3($2.75)");
       System.out.println("4. Product4($0.8)");
       System.out.println("5. Product5($6.25)");
   }
  
   public static void printMoney(){
       System.out.println("1. 5 dollar bill");
       System.out.println("2. 1 dollar bill");
       System.out.println("3. 50 cents coin");
       System.out.println("4. 25 cents coin");
       System.out.println("5. 10 cents coin");
       System.out.println("6. 5 cents coin");
       System.out.println("7. 1 cent coin");
   }
  
   public static int getProductPrice(int option){
       switch(option){
       case 1:
           return 750;
       case 2:
           return 150;
       case 3:
           return 275;
       case 4:
           return 80;
       case 5:
           return 625;
       default:
           return 0;
       }
   }
  
   public static int getMoneyByOption(int option){
       switch(option){
       case 1:
           return 500;
       case 2:
           return 100;
       case 3:
           return 50;
       case 4:
           return 25;
       case 5:
           return 10;
       case 6:
           return 5;
       case 7:
           return 1;
       default:
           return 0;
       }
   }
  
   public static int getInput(int max){
       Scanner in = new Scanner(System.in);
       while(true){
           System.out.print("Choose an option(0 to stop): ");
           int temp;
           temp = in.nextInt();
           if(temp >= 0 && temp <= max){
               return temp;
           }
           else{
               System.out.println("Invalid choice. try again!!!");
           }
       }
   }
  
   public static void displayChange(int money){
       int quarters = money / 25;
       money %= 25;
       int tens = money / 10;
       money %= 10;
       int fives = money / 5;
       money %= 5;
       int ones = money;
       System.out.println(quarters + " 25 cent coins");
       System.out.println(tens + " ten cent coins");
       System.out.println(fives + " five cent coins");
       System.out.println(ones + " one cent coics");
   }
  
   public static void main(String[] args){
       System.out.println("Enter your money details");
       printMoney();
       int money = 0, choice, price;
       while(true){
           choice = getInput(7);
           if(choice == 0){
               break;
           }
           else{
               money += getMoneyByOption(choice);
               System.out.println(money + " cents entered till now!!");
           }
       }
       for(int i = 0; i < 5; ++i){
           printProducts();
           choice = getInput(5);
           if(choice == 0){
               break;
           }
           price = getProductPrice(choice);
           if(price > money){
               System.out.println("your money does not cover item's price. try again");
           }
           else{
               System.out.println("Take your product");
               System.out.println("You paid: " + money + " cents");
               System.out.println("Item cost: " + price + " cents");
               money -= price;
               break;
           }
       }
       System.out.println("Change received");
       displayChange(money);
      
   }
}

Enter your money details
1. 5 dollar bill
2. 1 dollar bill
3. 50 cents coin
4. 25 cents coin
5. 10 cents coin
6. 5 cents coin
7. 1 cent coin
Choose an option(0 to stop): 2
100 cents entered till now!!
Choose an option(0 to stop): 4
125 cents entered till now!!
Choose an option(0 to stop): 5
135 cents entered till now!!
Choose an option(0 to stop): 6
140 cents entered till now!!
Choose an option(0 to stop): 6
145 cents entered till now!!
Choose an option(0 to stop): 7
146 cents entered till now!!
Choose an option(0 to stop): 7
147 cents entered till now!!
Choose an option(0 to stop): 7
148 cents entered till now!!
Choose an option(0 to stop): 0
1. Product1($7.5)
2. Product2($1.5)
3. Product3($2.75)
4. Product4($0.8)
5. Product5($6.25)
Choose an option(0 to stop): 2
your money does not cover item's price. try again
1. Product1($7.5)
2. Product2($1.5)
3. Product3($2.75)
4. Product4($0.8)
5. Product5($6.25)
Choose an option(0 to stop): 5
your money does not cover item's price. try again
1. Product1($7.5)
2. Product2($1.5)
3. Product3($2.75)
4. Product4($0.8)
5. Product5($6.25)
Choose an option(0 to stop): 4
Take your product
You paid: 148
Item cost: 80
Change received
2 25 cent coins
1 ten cent coins
1 five cent coins
3 one cent coics

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Please Walk me through this problem. Write a program that simulates the functionality of a vending...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ) Object-Oriented Design 7 Write a program that simulates a vending machine. Products can be purc...

    Write in java as simple as possible coding.. ) Object-Oriented Design 7 Write a program that simulates a vending machine. Products can be purchased by inserting coins with a value at least equal to the cost of the product. A user selects product from a list of available products, adds coins, and either gets the productor gets the coins returned. The coins are returned if insufficient money was supplied or if the product is sold out. The machine does not...

  • Write a program in Java that simulates a vending machine: The vending machine sells three types...

    Write a program in Java 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 and prompt the buyer to enter the amount of quarters, dimes and nickels. The machine would then proceed to...

  • 1. Create a new multi-class Java program which implements a vending machine simulator which contains the...

    1. Create a new multi-class Java program which implements a vending machine simulator which contains the following functionality: A) At program startup, the vending machine is loaded with a variety of products in a variety of packaging for example soda/tonic/Coke in bottles, peanuts in bags, juice in cartons, etc. Also included is the cost of each item. The program should be designed to easily load a different set of products easily (for example, from a file). Also at program startup,...

  • Write a C+OOP program that simulates a vending machine. The program should define at least two...

    Write a C+OOP program that simulates a vending machine. The program should define at least two classes. Less than two classes will result in 20 points deduction. (30 points) One must be general vending machine (50 points) The other can be either vending machine of soda, or vending machine of snack. It must be a subclass of above general machine These vending machines should have following components and functions e Coin slot o Allow adding coins o Display total coin...

  • Vending Machine (Python 3) Thinking Outside the box Write a program that asks the user to...

    Vending Machine (Python 3) Thinking Outside the box Write a program that asks the user to enter a bill value (1 = $1 bill, 5 = $5 bill, etc.) and the price of an item they want to buy in pennies and calculate their change amount in dollars, quarters, dimes, nickels, and pennies. Your code should start like this: ## # This program simulates a vending machine that gives change # Define constants PENNIES PER DOLLAR = 100 PENNIES PER...

  • Write the algorithm that determines the change to be dispensed from a vending machine. An item...

    Write the algorithm that determines the change to be dispensed from a vending machine. An item in the machine can cost between 5 cents and 1 dollar, in 5-cent increments (5,10,15,......90, 95, or 100), and the machine accepts only a single dollar bill to pay for the item. The user will provide the price and the program will output the price and calculate and output the total change and how many quarters, dimes and nickels need to be dispensed. DO...

  • write a program in java that determines the change to be dispensed from a vending machine....

    write a program in java that determines the change to be dispensed from a vending machine. An item in the machine can cost between 25 cents and a dollar, in a 5-cent increments(25,30,35.....,90,95,or 100), and the machine accepts only a single dollar bill to pay for the item. for example a possible dialogue with the user might be "Enter price of item (from 25 cents to a dollar, in 5-cent increments):45 you boughtan item for 45 cents and gave me...

  • write a program in C that determines the change to be dispensed from a vending machine....

    write a program in C that determines the change to be dispensed from a vending machine. An item in the machine can cost between 25 cents and a dollar, in a 5-cent increments(25,30,35.....,90,95,or 100), and the machine accepts only a single dollar bill to pay for the item. for example a possible dialogue with the user might be "Enter price of item (from 25 cents to a dollar, in 5-cent increments):45 you boughtan item for 45 cents and gave me...

  • C++ HW Question Your program will simulate a simple change maker for a vending machine. It...

    C++ HW Question Your program will simulate a simple change maker for a vending machine. It will start with a stock of coins and dollars. It will then repeatedly request the price for an item to be purchased or to quit. If given a price, it will accept nickels, dimes, quarters, one-dollar and five-dollar bills—deposited one at a time—in payment. When the user has deposited enough to cover the cost of the item, the program will calculate the coins to...

  • Pyhton Program : Write a coffee vending machine class. Include fields giving the number of cups...

    Pyhton Program : Write a coffee vending machine class. Include fields giving the number of cups of coffee available, the cost of one cup of coffee, and the total amount of money inserted by the user. This machine requires exact change. Include one constructor which stocks the machine with a quantity and price of coffee specified as parameters. Include the following methods; menu() // display the quantity and price of coffee insert(int quarters, int dimes, int nickels) // insert the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT