1

We are teaching a class where we need to teach about currency. We would like our students to add and demonstrate using different currencies across the world.

(e.g. Country=US, How much does 3 nickels + 2 pennies + 3 dimes)
(e.g. Country=UK, ...)
(e.g. Country=JAPAN, ...)
(e.g. Country=CHINA, ....)
(e.g. Country=AUSTRALIA, ...)

Is there any sample code, which demonstrates conversion of coins in different currencies?

Note: If it also contains the conversion between smaller units (e.g. b/w nickels, pennies, dimes, dollar) it would be useful.

Jason
  • 12,229
  • 20
  • 51
  • 66
  • 1
    Do you mean you want to add, for example: 3 pfennigs + 2 cents + 2 groats ? Or do you mean, what are the rules for adding coins in the variety of currencies you specify, just adding groats, florins, and ha'p'nies in the case of the UK ? – High Performance Mark Oct 18 '10 at 16:16
  • 1
    @High Performance Mark, I mean she wants something of this effect: `(e.g. Country=ZA, How much does 3 rands + 2 cents)` – Buhake Sindi Oct 18 '10 at 16:32
  • 1
    I meant coins from the same currency – Jason Oct 18 '10 at 16:35
  • 1
    The pfennig is extinct, now we have cents, too. http://en.wikipedia.org/wiki/Pfennig – starblue Oct 18 '10 at 18:05
  • 1
    If it's from the same country, there is nothing to convert; just look up what currency each country uses, and what each coin is worth in that currency. This isn't programming; it's just a small amount of research. – BlueRaja - Danny Pflughoeft Oct 18 '10 at 22:07

1 Answers1

1

Java enumerations are a nice tool. You can create one for each currency:

public enum US
{
  PENNY(1),
  NICKLE(5),
  DIME(5),
  QUARTER(1);

  int value;
  US(int i)
  {
    value = i;
  }

  public int getValue()
  {
    return value;
  }
}

This code shows how to use it:

public class Driver
{
  public static void main(String[] args)
  {
    System.out.println("Total value is " + (US.NICKLE.getValue() * 3 + US.QUARTER.getValue() * 2 + US.PENNY.getValue() * 4));
  }
}
Paul Jackson
  • 2,077
  • 2
  • 19
  • 29