/// <summary> /// calculates the tax owed based on age and salary /// </summary> public void CalculateTax(TaxViewModel viewModel) { var taxDiscount = _taxReliefBrackets.SingleOrDefault(x => viewModel.Age >= x.Min && viewModel.Age <= x.Max); viewModel.MonthlyTaxRelief = taxDiscount == null ? 0m : taxDiscount.Discount; // TODO: do we apply the tax relief before or after computing the tax owed? var monthlySalary = viewModel.MonthlySalary - viewModel.MonthlyTaxRelief; viewModel.MonthlyTaxAmount = 0m; // we could LINQ this but we'd lose some readability... foreach (var bracket in _taxBrackets) { if (monthlySalary <= bracket.Low) { break; } var taxableAtThisRate = Math.Min(bracket.High - bracket.Low, monthlySalary - bracket.Low); var taxThisBand = taxableAtThisRate * bracket.Rate; viewModel.MonthlyTaxAmount += taxThisBand; } }
public MainWindow() { InitializeComponent(); DataContext = new TaxViewModel(); }