Reputation: 12789
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.
Upvotes: 1
Views: 322
Reputation: 2147
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));
}
}
Upvotes: 1