Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This project involves using a Soda Vending Machine where you start by entering t

ID: 3624975 • Letter: T

Question

This project involves using a Soda Vending Machine where you start by entering the number of cans the machine contains and then ask the user to deposit coins or a dollar bill into the machine. Whenever the user chooses to purchase a can, the program should check to make sure there is enough money deposited in the machine and the machine is not out of cans.

The program should consist of two classes. The first class SodaMachine represents data about the machine. It should contain the following data:
• Class constants for the price of can, which is 60 cents, and a dollar bill, which is 100 cents.
• An instance double precision variable containing the amount deposited into the machine.
• An instance integer variable containing the number of cans in the machine.
This class should contain the following methods:
• A constructor that is supplied an integer that contains the number of cans in the Soda Machine.
• An instance method called insertCoins that adds the amount entered to the amount deposited and displays the current deposited balance.
• An instance method called insertBill that adds the bill to the amount deposited and displays the current deposited balance.
• A class method called purchaseCan that checks there is enough cans in the machine, dispenses a can, and shows the remaining balance after the purchase.
• A class method called getAmountDeposited that returns the remaining deposited amount.

The second class should contain the main method. It should prompt the user for the number of cans in the Soda Machine. It should also repeatedly prompt the user to choose from the following options:
• Enter 1 to insert coins.
• Enter 2 to insert bill.
• Enter 3 to purchase can.
• Enter 4 to quit program.

When the user chooses to quit, the program should display the remaining deposited money in the machine by calling the getAmountDeposited method and dispense the amount to the user.

Explanation / Answer

import java.util.*;

public class SodaMachine
{
// Class constants for the price of can, which is 60 cents, and a dollar bill, which is 100 cents.
public static final double DOLLAR = 1.0;
public static final double PRICE = 0.6;

// An instance double precision variable containing the amount deposited into the machine.
private double deposited;

// An instance integer variable containing the number of cans in the machine.
private int numCans;

// A constructor that is supplied an integer that contains the number of cans in the Soda Machine.
public SodaMachine(int numCans)
{
this.numCans = numCans;
deposited = 0;
}

// An instance method called insertCoins that adds the amount entered to the amount deposited and displays the current deposited balance.
public void insertCoins(double amount)
{
deposited += amount;

System.out.printf("Balance: $%.2f ", deposited);
}

// An instance method called insertBill that adds the bill to the amount deposited and displays the current deposited balance.
public void insertBill()
{
insertCoins(DOLLAR);
}

// A class instance method called purchaseCan that checks there is enough cans in the machine, dispenses a can, and shows the remaining balance after the purchase.
public void purchaseCan()
{
if(deposited < PRICE)
{
System.out.println("Balance not high enough.");
}
else if(numCans <= 0)
{
System.out.println("No soda left.");
}
else
{
System.out.println("Can purchased.");
numCans--;
deposited -= PRICE;
}

System.out.printf("Balance: $%.2f ", deposited);
}

// A class instance method called getAmountDeposited that returns the remaining deposited amount.
public double getAmountDeposited()
{
return deposited;
}
}

public class Runner
{
public static void main(String[] args)
{
SodaMachine test = new SodaMachine(10);

Scanner kb = new Scanner(System.in);

/*
The second class should contain the main method. It should prompt the user for the number of cans in the Soda Machine. It should also repeatedly prompt the user to choose from the following options:
• Enter 1 to insert coins.
• Enter 2 to insert bill.
• Enter 3 to purchase can.
• Enter 4 to quit program.
*/


int option;
do
{
System.out.print("Choose an option: Enter 1 to insert coins. Enter 2 to insert bill. Enter 3 to purchase can. Enter 4 to quit program. > ");
option = kb.nextInt();

switch(option)
{
case 1:
System.out.print("How many cents? ");
test.insertCoins(kb.nextInt()/100.0);
break;
case 2:
test.insertBill();
break;
case 3:
test.purchaseCan();
break;
case 4:
// When the user chooses to quit, the program should display the remaining deposited money in the machine by calling the getAmountDeposited method and dispense the amount to the user.
System.out.printf("Change: $%.2f ", test.getAmountDeposited());
test.insertCoins(-test.getAmountDeposited());
break;
}
}
while(option != 4);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote