public decimal Calculate(decimal amount, ITaxSettings settings) { //we expect a table of values in the format: from:to //the from starts from 0 and extends to the max value (100000000) decimal currentMin = 0; decimal currentTax = 0; foreach (var(name, value) in settings.GetList()) { if (amount < currentMin) { break; } try { var upperBound = Convert.ToDecimal(name); var taxPerc = Convert.ToDecimal(value); var bracketAmount = Math.Min(amount, upperBound) - currentMin; var taxAmount = bracketAmount * taxPerc; currentTax += taxAmount; currentMin = upperBound; } catch { // ignored } } return(currentTax); }
public void TestIterator() { var results = new Dictionary <string, string>(); foreach (var item in _settings.GetList()) { results[item.name] = item.value; } Assert.AreEqual(results.Count, _settings.GetCount()); foreach (var key in results.Keys.Where(key => results[key] != _settings.GetValue <string>(key))) { Assert.Fail("Value doesn't match"); } }