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

Pages: 1-

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 13:36

Good: pass
Bad: it's written in Java

Name: Anonymous 2011-01-23 13:38

public final boolean GAY_FOR_PROF = True // I want your ass and nothing will ever change that, I don't care who knows.

This will impress any java professor.

Name: Anonymous 2011-01-23 13:40

>>3
What if his professor is a woman?

Name: Anonymous 2011-01-23 13:43

>>4
There are no such things as static or scoped variables in HTML to the best of my knowlege.

Name: Anonymous 2011-01-23 13:45

>>5
I chuckled.

Name: Anonymous 2011-01-23 14:00

>>3
Sure you might get his attention, but do you really think he's going to stick around long term for someone who forgets his semicolon?

Name: Anonymous 2011-01-23 14:24

POP_TYPE
oh u

Name: Anonymous 2011-01-23 14:31

>>1
hnHungarianNotation
Just saying.
WHBT

Name: Anonymous 2011-01-23 14:36

No bacon sammich?

Name: Anonymous 2011-01-23 14:37

>>10
Read your poignant guide for chucky bacon.

Name: Anonymous 2011-01-23 14:42

Is this the new random indent I've been hearing much about lately? I also stopped reading as soon as I saw Hungarian notation.

Name: Anonymous 2011-01-23 14:54

What's wrong with hungarian notation guys.  Fill me in when it's good and when it's bad please?

Name: nAnonymous 2011-01-23 14:55

p>>13
sIt vIs wAlways aBad.

Name: Anonymous 2011-01-23 14:58

>>14
Why is it bad?  What should I be doing instead?

Name: Anonymous 2011-01-23 15:04

>>15
shitCase for methods, UPCASE for constants, CamelCase for classes, lowercase_with_underscores for variables and functions.

Name: Anonymous 2011-01-23 15:07

why a different case for functions and methods?

Name: Anonymous 2011-01-23 15:08

>>15
Do it lisp-style. It's the best-style-there-is.

Name: Anonymous 2011-01-23 15:08

>>16
Also:

i, j, k, l       for counter variables (for loops).
n, m, i, j, k, l for integer temporary variables.
c                for character temporary variables.
f, g, h          for func-wait, this is Java, disregard this line.
x, y, z, w       for generic temporary variables.
xs, ys, zs, ws   for generic array/list variables.
r                for temporary to-be-returned variables.

Name: Anonymous 2011-01-23 15:10

>>18
You can't do best-case-ever-p in JEWa.

>>17
because callToFunction() sucks, and obj.call_to_method() sucks, while call_to_function() and obj.callToMethod() are acceptable.

Name: Anonymous 2011-01-23 15:17

>>19
xs, ys, zs, ws   for generic array/list variables.
Fucking sandnigger.

Name: Anonymous 2011-01-23 15:18

>>21
Also l

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.

Name: Anonymous 2011-01-23 15:39

>>23

http://www.drinkredlight.com/
You misunderstand..
I'll read the java code convention

Name: Anonymous 2011-01-23 15:47

>>24
You misunderstand..
..


Please, just leave.

Name: Anonymous 2011-01-23 15:48

>>24

Ok, but why do you have two return statement? One for the red light.

Name: Anonymous 2011-01-23 15:56

>>26
It's not really for a red light soda, I was kidding, it's for a red light error so I handled it differently.

Name: Anonymous 2011-01-23 18:14

Using floating point for money considered a pretty bad idea. Just use integers and have all values in pennies.

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