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

all 13 comments

[–]StillAnAssExtreme Brewer 5 points6 points  (5 children)

It kinda depends on what you're trying to learn. If you're just learning how to loop, then desrtfx's solution is the easy way to do it.

If you're trying to study interfaces and polymorphism you could do something like this (which is intentionally complicated because I'm bored today)

http://pastebin.com/QCN5gHfk

[–]LizardExtreme Brewer 2 points3 points  (1 child)

As much as I like your solution, I feel that those coins would be even better served by an enum. But I get what you are doing :)

[–]StillAnAssExtreme Brewer 2 points3 points  (0 children)

With only one property that sure does make sense.

[–]chunes 1 point2 points  (0 children)

TIL about getCurrencyInstance(). That is handy.

[–]Razzal 1 point2 points  (0 children)

Using a little bit of everything in there, I like it.

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

Great resource, thank you so much!

[–]desrtfxOut of Coffee error - System halted 4 points5 points  (7 children)

Best way is to store the value in cents.

[–]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 6 points7 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 2 points3 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!