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

Assignment7_main.java: Your task in this assignment is to write a simple class d

ID: 3683100 • Letter: A

Question

Assignment7_main.java: Your task in this assignment is to write a simple class definition for a vending machine.

The class should have:

Attributes

Soda count

Money credit (how much money has been inserted)

Methods

Add money

Vend

Restock

Constructor

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 – just display the amount returned.

When the machine is "restocked," just set the soda count to the maximum that the machine will hold.

Once you have written the class, create a main/driver program that interacts with an instance of the class. Your program should present a menu of options to the user and process the user's choice; use creativity and object-oriented design in your solution.

Explanation / Answer

public class VendingMachine {

   private int sodaCount;

   public int getSodaCount() {
       return sodaCount;
   }

   public void setSodaCount(int sodaCount) {
       this.sodaCount = sodaCount;
   }

   public double getMoneyCredited() {
       return moneyCredited;
   }

   public void setMoneyCredited(double moneyCredited) {
       this.moneyCredited = moneyCredited;
   }

   private double moneyCredited;

   public VendingMachine() {
       this(50, 0.0);
   }

   public VendingMachine(int sodaCount, double moneyCredited) {
       this.sodaCount = sodaCount;
       this.moneyCredited = moneyCredited;
   }

   public double addMoney(double moneyAdded) {
       double change = 0.0;
       if (moneyAdded >= 0.65) {
           if (sodaCount <= 0) {
               restock();
           }
           vend();
           change = getChange(moneyAdded);
           this.moneyCredited += moneyAdded - change;
       }
       return change;
   }

   private void vend() {
       sodaCount--;

   }

   private void restock() {
       sodaCount = 50;
   }

   private double getChange(double moneyAdded) {
       return moneyAdded - 0.65;
   }

   public static void main(String[] args) {
       VendingMachine vendingMachine = new VendingMachine();

       System.out.println("Current Soda stock "
               + vendingMachine.getSodaCount());
       System.out.println("Adding money 0.70 to machine and vending soda.. ");
       System.out.format("Change : %.2f ", vendingMachine.addMoney(.70));
       System.out.println("Current Soda stock "
               + vendingMachine.getSodaCount());
       System.out.println("Current money Added to machine : "
               + vendingMachine.getMoneyCredited());
   }
}

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