Java: A place to buy candy is from a candy machine. A new candy machine is bought for the gym, but it is not working properly. The candy machine has four dispensers to hold and release items sold by the candy machine and a cash register. The machine sells four products — candies, chips, gum, and cookies—each of which is stored in a separate dispenser. You have been asked to write a program for this candy machine so that it can be put into operation. The program should do the following: Show the customer the different products sold by the candy machine Let the customer make the selection Show the customer the cost of the item selected Accept money from the customer Return change Release the item, that is, make the sale
Java: A place to buy candy is from a candy machine. A new candy machine is bought for the gym, but it is not working properly. The candy machine has four dispensers to hold and release items sold by the candy machine and a cash register. The machine sells four products — candies, chips, gum, and cookies—each of which is stored in a separate dispenser. You have been asked to write a program for this candy machine so that it can be put into operation.
The program should do the following:
- Show the customer the different products sold by the candy machine
- Let the customer make the selection
- Show the customer the cost of the item selected
- Accept money from the customer
- Return change
- Release the item, that is, make the sale
Here is my provided code:
import java.util.*;
public class CandyMachine
{
// Each candy machine is made of 1 CashRegister and 4 Dispensers
CashRegister cashRegister;
Dispenser candy;
Dispenser chips;
Dispenser gum;
Dispenser cookies;
//Default constructor to set the cashRegister and Dispensers with default values ()
public CandyMachine()
{
CashRegister cashRegister = new CashRegister();
Dispenser candy = new Dispenser();
// -- Your Code for the rest of the components
}
//Constructor to set each components with user input values
public CandyMachine()
{
Scanner console = new Scanner(System.in);
// Example to set cashRegister
cashRegister = new CashRegister(console.nextInt());
// -- Add your Code for the rest of the components
// Optional: Add Screen hints for input such as System.out.println("Set Cost of the item")
}
// Switch on the CandyMachine for operations
public void On()
{
Scanner input = new Scanner(System.in)
int choice; //variable to hold the selection
showSelection(); //Display the hint
choice = input.nextInt(); //Make choice
// Create a Sentinel loop to control the input
// Based on the input, sell different products
// Hint 1: use switch/case
// Hint 2: call the sellProduct method
// Hint 3: call method from cashRegister to add money.
// -- Add your Code Here
}
// On Screen Hint for Selection Hint
private void showSelection()
{
System.out.println("*** Welcome ***");
// Optional: Add Code to show the current balance
System.out.println("To select an item, enter ");
System.out.println("0 for Adding more cash");
System.out.println("1 for Candy");
System.out.println("2 for Chips");
System.out.println("3 for Gum");
System.out.println("4 for Cookies");
System.out.println("9 to exit");
}//end showSelection
// Method to sell the product
// Post Condition:
// If not enough item, print out "Sold out" and exit
// If not enough money inserted, ask for more money, or option to exit.
// Else, print out "Collect your item" and deduct deposit and inventory accordingly.
private void sellProduct(Dispenser product, CashRegister cRegister)
{
// Add your code here
}//end sellProduct
}
public class CashRegister
{
private int cashOnHand; //variable to store the cash in the register
//Default constructor to set the cash in the register to 0
//Postcondition: cashOnHand = 0
public CashRegister()
{
// -- Your Code Here
}
//Constructor with parameters to set the cash in the register to a specific amount
//Postcondition: cashOnHand = cashIn
public CashRegister(int cashIn)
{
// -- Your Code Here
}
//Method to show the current amount in the cash register
//Postcondition: The value of the instance variable cashOnHand is returned.
public int currentBalance()
{
// -- Your Code Here
}
//Method to receives the amount deposited by the customer and updates the amount in the register
//Postcondition: cashOnHand = cashOnHand + amountIn
public void acceptAmount(int amountIn)
{
// -- Your Code Here
}
//Method to deduct the amount deposited by the customer and updates the amount in the register
//Postcondition: cashOnHand = cashOnHand - amountCost
public void deductAmount(int amountCost)
{
// -- Your Code Here
}
}
public class Dispenser
{
private int numberOfItems; //variable to store the number of items in the dispenser
private int cost; //variable to store the cost of an item
//Default constructor to set the cost and number of items to the default values
//Postcondition: numberOfItems = 50; cost = 50;
public Dispenser()
{
// -- Your Code Here
}
//Constructor with parameters to set the cost and number
//of items in the dispenser specified by the user
//Postcondition: numberOfItems = setNoOfItems;
// cost = setCost;
public Dispenser(int setNoOfItems, int setCost)
{
// -- Your Code Here
}
//Method to show the number of items in the dispenser
//Postcondition: The value of the instance variable numberOfItems is returned.
public int getCount()
{
// -- Your Code Here
}
//Method to show the cost of the item
//Postcondition: The value of the instance variable cost is returned.
public int getProductCost()
{
// -- Your Code Here
}
//Method to reduce the number of items by 1
//Postcondition: numberOfItems = numberOfItems - 1
public void makeSale()
{
numberOfItems--;
}
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 7 images