I have an amount class which provides the minus
method among others. The result of this operation is of course another amount.
The question is how can I handle the three cases according to the signum of this amount polymorphically?
Currently, I am just switching on the signum of the contained value in the amount but I would like to refines this implementation. Short example:
Amount remaining = amount.minus(other.amount);
switch (remaining.getRoundedValue().signum()) {
case -1:
return ...;
case 0:
return ...;
case +1:
return ...;
}
All three cases return the same type of object.
When facing this kind of problems, one usually moves the implementation to the class which knows about this state, like in this case, the amount class. However, the code that should be executed needs additional variables and including or passing them into a function of the amount class would break its cohesion.
1