I need to do some calculations like this (simplified):
amount1 amount2 amount3 amount4 total
5000 500 1000 350 6850
3.55% 177.5 17.75 35.5 12.43 243.18
1.00% 50 5 10 3.5 68.5
- 4772.5 477.25 954.5 334.07 6538.32
16.66% ....
...
The calculations are pretty easy; But I’m not sure how I best implement this.
I don’t only need to be able to fetch and show the total results from the calculations but also every sub calculation.
I can make a method for every sub calculation and group methods together in one for a total calculation.
f.e.:
calcTotal1() {
calcSub1() + calcSub2() + ... ;
}
But I was wondering if there is a better way to implement this.
It also should be a little library that can be used by different programs.
I think, your requirement could be nicely met with an array of percentages to be calculated:
Please note the use of
BigDecimals, while they do use a bit more memory, it will have a severe impact on the correctness of your figures.With
nulls in the percentages[], you can specify rows where subtotals have to be printed out.