Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

critique this code

Name: Anonymous 2011-01-23 13:34

Tell me what's good or bad about this java class.  My professor isn't going to be providing much input as this is already way better than what the assignment was expecting.  I want to avoid the development of bad habits.


public class VendingMachine
{
  //0=pepsi 1=drpepper 2=sprite 3=dew 4=error
  private final String[] POP_TYPE = {"pepsi", "dr. pepper", "sprite", "mt det", "red blinking light"};
  private final int MAX_INVENTORY = 9;
  private final double COST_OF_POP = 1.50;

  private int[] ariInventory = new int[4];
  private double dCashDeposited;
  private double dCashBox;


  public VendingMachine()
  {
    //Fill up new pop machine
    fillInventory();
    //Set initial deposit value at zero
    returnCash();
    emptyCashBox();
  }

  public void insertCash(double fAdditionalDeposit)
  {
    //todo: Takes money and accumulates total
    dCashDeposited = dCashDeposited + fAdditionalDeposit;
  }
    public String selectPop(int iPopSelection)
  {
      if (ariInventory[iPopSelection] > 0 && dCashDeposited >= COST_OF_POP){
        ariInventory[iPopSelection] = ariInventory[iPopSelection] - 1;
        dCashDeposited = dCashDeposited - COST_OF_POP;
        dCashBox = dCashBox + COST_OF_POP;
        returnCash();

        return POP_TYPE[iPopSelection];}
      else{
        return POP_TYPE[4];} /// A red blinking light
  }
    public double returnCash()
    {
      double toBeReturned = dCashDeposited;
      dCashDeposited = 0.00;
      return toBeReturned;
    }
    public double emptyCashBox()
    {
        double dCashOut = dCashBox;
        dCashBox = 0.0;
        return dCashOut;
    }

        public int getInventory(int iSoda)
  {
            return ariInventory[iSoda];

  }
          public void fillInventory()
  {
    for (int i = 1; i <= 3; i++){
        ariInventory[i] = MAX_INVENTORY;
    }
  }

}

Name: Anonymous 2011-01-23 15:29

>>1

That's not very good, kid.

You're not respecting Java's code convention. Go read them. At least be consistent. Bad indented code is painful to read.

But that's not the saddest. Can you drink a red blinking light? You have an Array with 5 cells (4 drinks and a blinking light). And another Array with 4 cells, containing the inventory for the 4 first cells of the POP_TYPE Array.

They're teaching you Java, that means they're asking you to design objects. Use your common sense.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List