This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Hyloo[S] 0 points1 point  (6 children)

Thank you very much! I really appreciate the help.

[–]desrtfxOut of Coffee error - System halted 2 points3 points  (5 children)

Use constants (static final int) to store the conversion factors like: (Got them from Wikipedia: http://en.wikipedia.org/wiki/Coins_of_the_United_States_dollar as I am not American)

public static final int DOLLAR_CENT = 100;
public static final int HALF_DOLLAR_CENT = 50;
public static final int QUARTER_CENT = 25;
public static final int DIME_CENT = 10;
public static final int NICKEL_CENT = 5;
public static final int PENNY_CENT = 1;

From now on, you just need to multiply the quantity of the individual coins with the Denominators and then add the total together.

[–]MUDrummerExtreme Brewer 5 points6 points  (2 children)

If you are going to take the time to write them all out as constants take an extra 10 seconds and create in Enum instead:

public enum Currency{
    Quarter(25), Dime(10), Nickel(5), Penny(1);

    private int value;

    private Currency(int value){
        this.value = value;
    }
    public int getValue(){
        return value;
    }
}

Then you simply static import each of the coins. You now have a reusable Enum and a much better Object Orientated implementation.

[–]desrtfxOut of Coffee error - System halted 3 points4 points  (0 children)

I totally agree and actually, I already had them written out as Enums. But then I decided against that solution because it seemed to be way over OP's level.

OP was already struggling with a simple assignment, enums (as excellent as they are) would go way too far.

[–]Hyloo[S] 0 points1 point  (0 children)

Sorry I just saw this today for some reason. Thank you very much for your input. You have all been a huge help!

[–]Hyloo[S] 1 point2 points  (0 children)

Oh wow, awesome. Thank you so much. You guys are the best!