We assume we have unlimited number of coins and bills. My code works fine but the method optimalDMCA()
has a Cyclomatic Complexity of 8.
import java.util.*;
import java.math.*;
class GoodEnough {
long how2 = 0;
long about5 = 0;
long this10 = 0;
}
class Solved {
static GoodEnough optimalDMCA(long s) {
long change = s;
GoodEnough c = new GoodEnough();
if (change >=10) {
if (change % 2 != 0 && change % 5 !=0) {
return dealWithChangeLike31Euros(change);
}
else {
c.this10 = (long) change / 10;
change = change % c.this10;
}
}
if (change <10 && change >=5) {
if (change % 2 == 0) {
c.about5 = 0;
c.how2 = change/2;
}
else {
change = change - 5;
c.about5 = 1;
}
}
else if (change %2 ==0) {
c.about5 = 0;
c.how2 = change/2;
} else {
return null;
}
return c;
}
static GoodEnough dealWithChangeLike31Euros(long s) {
GoodEnough c = new GoodEnough();
c.this10 = ((long) s / 10) - 1;
long change = (long) s % (c.this10 * 10);
if (change > 5) {
change = change - 5;
c.about5 = 1;
}
if (change % 2 == 0) {
c.how2 = change / 2;
}
else {
return null;
}
return c;
}
}
Can the implementation of the dealWithChangeLike31Euros()
method be merged into the other method?