Let’s say I have the following code in a class:
enum Currency
{
PENNY(1), NICKEL(5), DIME(10), QUARTER(25);
private int value;
private Bonus(int value)
{
this.value = value;
}
public int getValue()
{
return value;
}
};
public class Coin
{
Currency c;
public Coin(Currency c)
{
this.c = c;
}
public void setCurrency(Currency c)
{
this.c = c;
}
}
If I have a separate class that creates Coin objects with a certain Currency enum, how could I write a method in that separate class to set the enum from, say, PENNY to DIME?
You just call the method setCurrency in coin class from the separate class you created and pass the currency enum you want to set it.
example:
Lets say you create a Coin object with a currency enum as PENNY:
Now you can create a separate method in the separate class you want to call this and set the new currency enum as: