The following class just has one method that returns the divisor of a certain percent, so if I pass it 5, it will return 0.05.
public class Adjustment
{
public decimal Amount {get;set;}
public bool IsCompounded {get;set;}
public bool Add{get;set;}
public decimal Calculate(decimal amount)
{
return (amount / 100.0M);
}
}
My main method is defined as follows:
void Main()
{
Adjustment a1 = new Adjustment {Amount = 10.0M, IsCompounded = false, Add = true};
Adjustment a2 = new Adjustment {Amount = 7.0M, IsCompounded = false, Add = true};
List<Adjustment> adjustments = new List<Adjustment>();
adjustments.Add(a1);
adjustments.Add(a2);
decimal value = 0.0M;
decimal total = 100.0M;
foreach(Adjustment a in adjustments)
{
if(a.IsCompounded)
{
value = (1 + a.Calculate(a.Amount));
if(a.Add)
total *= value;
else
total /= value;
}
else if(!a.IsCompounded)
{
value += a.Calculate(a.Amount);
if(a.Add)
total *= value;
else
total /= value;
}
}
Console.WriteLine(total);
}
In the above main method, I am starting off with 100 as the total and if all the taxes are compounded, it works fine, I get 117.7 and if I take 117.7 and remove the taxes, I get back to 100. For non compounded, I only need to add 1 to the very end and then divide the total by that, but I am not sure how to do it. Currently, when I am looping through, I am just adding the divisors, for example 0.1 + 0.07 = 0.17. Outside the loop, I need to add 1 to that to get 1.17 and then multiply or divide the total to either add or remove the tax respectively. Then there is the issue of the adjustments being compounded and non-compounded which gets more complicated. In this case, I need to do something like the following:
For example, Assume I have 3 taxes, 10, 7, and 3. 10 and 7 are compounded and 3 is non-compounded, so the formula is:
100 * (1+((1+0.10) * (1+0.07)−1)+((1+0.03)−1)) which simplifies to 100 * ((1+0.10) * ( (1+0.07)+0.03) = 120.70
I am not sure how to implement the above.