mercredi 21 janvier 2015

JAVA vending machine example

I am trying to simulate a vending machine in java. This was based off an exercise in my software design class and I was interested in the problem.


a few of the things i want it to do is...


*drinks can be purchased by inserting the correct number of coins


*the user can select a product from a list, add coins, and gets the product or the money is returned due to not enough coins or there isnt any of that drink left


*the operator can remove money in the machine and restock with drinks


right now i have two classes.


The First class is VendingMachine and it looks like this so far..



package vendingMachine;

public class VendingMachine{

private int coinsLeftInTheMachine = 0;
private int currentCoinAmountInMachine = 0;

public enum Soda{
Coke,
Pepsi,
MountainDew,
RootBeer;
}


private Sodas coke = new Sodas(Sodas.Soda.Coke);
private Sodas pep = new Sodas(Sodas.Soda.Pepsi);
private Sodas mtndw = new Sodas(Sodas.Soda.MountainDew);
private Sodas rtbeer = new Sodas(Sodas.Soda.RootBeer);


public void insertCoins(int amountOfCoins){
this.currentCoinAmountInMachine = this.currentCoinAmountInMachine + amountOfCoins;
this.coinsLeftInTheMachine = this.coinsLeftInTheMachine + amountOfCoins;
}

public void purchaseSoda(Sodas.Soda selection){
if(coke.buySoda()){

}
}

public void restockSodas(){



}

public void selectSoda(){

}

public void returnCoinsToUser(){
this.currentCoinAmountInMachine = 0;
}

public void removeMoneyFromMachine(){
this.coinsLeftInTheMachine = 0;
}

}


The next class i have is Sodas and here that one is..



package vendingMachine;

public class Sodas{
public enum Soda{
Coke,
Pepsi,
MountainDew,
RootBeer;
}

private int count = 0;
private Soda type;

public Sodas(Soda brand){
this.type = brand;
}

public boolean buySoda(){
if (count > 0){
count--;
return true;
}else {
return false;
}
}
}


I am not really sure where to go with this, but I would love some actual code to help me finish it. If anyone has some time to spend on a small project with me that would be awesome!


Aucun commentaire:

Enregistrer un commentaire