Question

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 display the total amount inserted then the buyer will 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 the equal amount of money to the food cost, 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 back to the buyer.

Every food is initialized with a fixed quantity. 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

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

Food.java

public class Food {
private String name;
private double cost;
private int quantity;
  
public Food()
{
this.name = "";
this.cost = 0.0;
this.quantity = 0;
}

public Food(String name, double cost, int quantity) {
this.name = name;
this.cost = cost;
this.quantity = quantity;
}

public String getName() {
return name;
}

public double getCost() {
return cost;
}

public int getQuantity() {
return quantity;
}

public void setName(String name) {
this.name = name;
}

public void setCost(double cost) {
this.cost = cost;
}

public void setQuantity(int quantity) {
this.quantity = quantity;
}
}

VendingMachineApp.java (Driver class)

import java.util.ArrayList;
import java.util.Scanner;

public class VendingMachineApp {
  
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
ArrayList<Food> foods = loadFood();
int choice;
  
do
{
displayMenu(foods);
choice = Integer.parseInt(sc.nextLine().trim());
  
if(choice == 0)
{
System.out.println("\nThanks for using the Vending Machine.\nGoodbye!\n");
System.exit(0);
}
  
if(choice < 0 || choice > foods.size())
System.out.println("\nInvalid choice!\n");
else
{
if (foods.get(choice - 1).getQuantity() == 0)
{
System.out.println("Sold out. Please make another choice!\n");
break;
}
else
{
System.out.print("Enter number of quarters (1 quarter = 25 cents): ");
int nQuarters = Integer.parseInt(sc.nextLine().trim());
System.out.print("Enter number of dimes (1 dime = 10 cents): ");
int nDimes = Integer.parseInt(sc.nextLine().trim());
System.out.print("Enter number of nickels (1 nickel = 5 cents): ");
int nNickels = Integer.parseInt(sc.nextLine().trim());
int totalCents = (nQuarters * 25) + (nDimes * 10) + (nNickels * 5);
double dollars = ((double)totalCents / 100);
System.out.println("\nYou entered: $" + String.format("%.2f", dollars));

if(dollars < foods.get(choice - 1).getCost())
System.out.println("Insufficient price entered.\n");
else
{
System.out.println("Please take your " + foods.get(choice - 1).getName());
foods.get(choice - 1).setQuantity(foods.get(choice - 1).getQuantity() - 1);
if(dollars > foods.get(choice - 1).getCost())
{
System.out.print("Here's your change: ");
double change = dollars - foods.get(choice - 1).getCost();
double cents = (change * 100);
int quarters = (int)(cents / 25);
double rem = (cents % 25);
int dimes = (int)(rem / 10);
rem = (rem % 10);
int nickels = (int)(rem / 5);

System.out.println(quarters + " quarters, " + dimes + " dimes and " + nickels + " nickels.");
}
System.out.println();
}   
}
}
}while(choice != 0);
}
  
private static ArrayList<Food> loadFood()
{
ArrayList<Food> foods = new ArrayList<>();
foods.add(new Food("Potato chips", 1.25, 10));
foods.add(new Food("Cookies", 0.85, 13));
foods.add(new Food("Candies", 0.95, 10));
  
return foods;
}
  
private static void displayMenu(ArrayList<Food> foods)
{
System.out.println("Choose from below:");
int counter = 0;
for(Food food : foods)
{
System.out.println(++counter + ". " + food.getName() + " $" + food.getCost());
}
System.out.print("0. Exit\n"
+ "Enter your choice: ");
}
}

******************************************************************* SCREENSHOT ********************************************************

Add a comment
Know the answer?
Add Answer to:
Write a program in Java that simulates a vending machine: The vending machine sells three types...
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
  • 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...

  • 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...

  • 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...

  • 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...

  • 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...

  • 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...

  • 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...

  • 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...

  • write a PHP program makes change for a given number of cents. The program should ask...

    write a PHP program makes change for a given number of cents. The program should ask the user for the amount of cents and then output the change for specific denominations of ten dollar bills, five dollar bills, one dollar bills, quarters, dimes, nickels, and pennies. Here is a sample program output for an input of 265 cents: Change for 2 dollars and 65 cents : 0 ten dollar bills 0 five dollar bills 2 one dollar bills 2 quarters...

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