I am still learning so go easy on me.
I have following currency system:
3 different coins, lets call them Rhodonite (Red colour), Daranen (gold colour) and Delis (gray colour). 100 Delis equal 1 Daranen, 100 Daranen equal 1 Rhodonite. No limit with Rhodonites.
So I am trying to multiply that currency by a double factor. Heres my function:
public static Price multiply(Price price, double factor) {
int de = (int)Math.round(price.getDelis() * factor);
int da = (int)Math.round(price.getDaranen() * factor);
int r = (int)Math.round(price.getRhodoniten() * factor);
if ((de / 100) >= 1) {
de = de % 100;
da += de / 100;
}
if ((da / 100) >= 1) {
da = da % 100;
r += da / 100;
}
return new Price(r, da, de);
}
So when I am multiplying, if the multiplied delis are greater than 100, it should get added to the daranen, same counts for the daranen and the rhodonites.
Here's an example, multiplied with the factor 2: Top one is before multiplication, bottom one is multiplied.
As you can see, the multiplication itself works, but not the addition of the remainder to the next higher coin type. I'm happy for any help, still really new to java.
I suggest keeping only the Delis and converting them to the other currencies as the other answers suggested. However, to answer your question, as to what you did wrong:
You simply have to flip the lines where you do the modulo operation and where you do the division.
e.g.
de = de % 100;
da += de / 100;
should be
da += de / 100;
de = de % 100;
Here is the correct code:
public static Price multiply(Price price, double factor) {
int de = (int) Math.round(price.getDelis() * factor);
int da = (int) Math.round(price.getDaranen() * factor);
int r = (int) Math.round(price.getRhodoniten() * factor);
if ((de / 100) >= 1) {
da += de / 100;
de = de % 100;
}
if ((da / 100) >= 1) {
r += da / 100;
da = da % 100;
}
return new Price(r, da, de);
}