Ask Question
16 January, 15:50

Create a class Cola Vending Machine. This class is simulating a cola vending machine. It keeps track of how many cola bottles are in the class and how much one bottle costs. There should be a method sell Bottle which sells one bottle to a customer, decreases the amount of bottles left. There also is a method restock which sets the number of bottles to the number it is restocked to. Write a main method to test the functionality of the Cola Vending Machine machine.

+3
Answers (1)
  1. 16 January, 17:14
    0
    public class CocaColaVending {

    private int numBottles;

    private double costPerBottle;

    public CocaColaVending (int numBottles, double costPerBottle) {

    this. numBottles = numBottles;

    this. costPerBottle = costPerBottle;

    }

    public int getNumBottles () {

    return numBottles;

    }

    public void setNumBottles (int numBottles) {

    this. numBottles = numBottles;

    }

    public double getCostPerBottle () {

    return costPerBottle;

    }

    public void setCostPerBottle (double costPerBottle) {

    this. costPerBottle = costPerBottle;

    }

    public void sellBottles (int numSold) {

    int remainingStock = this. numBottles-numSold;

    setNumBottles (remainingStock);

    }

    public void restockBottles (int numRestock) {

    int newStock = this. numBottles+numRestock;

    setNumBottles (newStock);

    }

    }

    THE TEST CLASS IS showing the functionality of the class is given in the explanation section

    Explanation:

    public class CocaColaVendingTest {

    public static void main (String[] args) {

    CocaColaVending vending = new CocaColaVending (1000,2.3);

    System. out. println ("Intial Stock " + vending. getNumBottles ());

    vending. sellBottles (240);

    System. out. println ("After Selling 240 bottles " + vending. getNumBottles ());

    vending. restockBottles (1000);

    System. out. println ("After restocking 1000 bottles " + vending. getNumBottles ());

    }

    }
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Create a class Cola Vending Machine. This class is simulating a cola vending machine. It keeps track of how many cola bottles are in the ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers