Attributes Methods For simplicity sake, you may assume that the machine can hold
ID: 3566778 • Letter: A
Question
Attributes
Methods
For simplicity sake, you may assume that the machine can hold up to 50 of one type of soda; you may also assume that all sodas cost $0.65. When adding money, simply take in a floating-point number representing how much money to add (i.e. you do not have to manage different coins).
Be sure to make sure that enough money was added to the machine before a soda is vended. Also, when a soda is vended, return the appropriate change (i.e. if more than $0.65 was added to the machine, then return the extra money). Again, don't worry about managing different kinds of coins
Explanation / Answer
//The java program simulates the vending machine of soda machine
//VendingMachine.java
public class VendingMachine
{
private int sodaCount; //to count number of sodas
private final float SODA_COST=0.65f; //cost of soda
private float amount; //to store the soda cost
private static float totalAmount; //to add sum of the total amount of sodas
//constructor to set the constant values of soda
public VendingMachine()
{
//Total sodas in the machine
sodaCount=50;
//To add the cost of the soda to totalAmount
totalAmount=0;
}
//Method to add the cost to the total amount
public void addMoney(float cost)
{
amount=cost;
totalAmount=totalAmount+amount;
}
//Method returs the amount inserted by the customer into the machine
public float getMoney()
{
return amount;
}
//Returns the change of the soda if more money is added
//to the vending machine otherwise returns 0.0;
public float vend()
{
//Subtract 1 from the soda count
if(sodaCount<=50)
sodaCount=sodaCount-1;
else
reStock();
//change of the extra soda cost is inserted in the vending machine
float change=0;
if(amount>0.65)
//subtract soda cost from the
change=amount-SODA_COST;
return change;
}
//Reset the soda cost to the actual soda count
public void reStock()
{
//reset soda count
sodaCount=50;
}
}
------------------------------------------------------------------------------------------------------------------------
//VendingMachineTester.java
import java.util.Scanner;
public class VendingMachineTester
{
static Scanner input=new Scanner(System.in);
static VendingMachine sodaMachine;
public static void main(String[] args)
{
int choice=menu();
sodaMachine=new VendingMachine();
switch(choice)
{
case 1:AddMoney(); break;
case 2:Vend();break;
case 3:
sodaMachine.reStock();break;
case 4:
System.exit(0);
}
}
//Menu choice to select user choice from the menu options
private static int menu()
{
int choice=0;
Scanner scanner=new Scanner(System.in);
do
{
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");
System.out.println("Enter your choice :");
choice=scanner.nextInt();
if(choice>5)
System.out.println("Invalid choice");
}while(choice>5);
return choice;
}
//AddMoney metod to ask user to enter the cost of the
//soda and display the change of the soda
public static void AddMoney()
{
System.out.println("Enter soda cost : $");
float cost=input.nextFloat();
sodaMachine.addMoney(cost);
System.out.println("Change : "+sodaMachine.vend());
}
//Vend method that checks if the sufficient amount is added
//to the machine before vendind
public static void Vend()
{
if(sodaMachine.getMoney()!=0)
System.out.println("Your change : "+sodaMachine.vend());
else
AddMoney();
}
}
---------------------------------------------------------------------------------------------------------------------------------------
Sample Ouput:
Run1:
--MENU---
1.Add money
2.Vend
3.Restock
4.Exit
Enter your choice :
1
Enter soda cost : $
3
Your change : 2.35
Run2:
--MENU---
1.Add money
2.Vend
3.Restock
4.Exit
Enter your choice :
1
Enter soda cost : $
3
Change : 2.35
Run 3:
--MENU---
1.Add money
2.Vend
3.Restock
4.Exit
Enter your choice :
2
Enter soda cost : $
3
Change : 2.35
Hope this would be helpful
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.