-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculatorMain.cs
93 lines (87 loc) · 2.52 KB
/
CalculatorMain.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace IncomeTaxCalculator
{
public partial class CalculatorMainForm : Form
{
private const string LABEL_PLACEHOLDER = "£0";
private const string SALARY_RANGE_ZERO = "Salary must be greater than zero.";
private const string SALARY_RANGE_MAX_VALUE = "Salary is greater than the max calculable value.";
public List<TaxBand> Bands { get; set; }
public CalculatorMainForm()
{
Bands = new List<TaxBand>()
{
new TaxBand(0, 5000, 0),
new TaxBand(5000, 20000, 0.2),
new TaxBand(20000, 0.4)
};
InitializeComponent();
GrossAnnualNumericUpDown.Maximum = decimal.MaxValue;
GrossAnnualNumericUpDown.Select(0, 1);
}
private void CalculateButton_Click(object sender, EventArgs e)
{
double salary = (double)GrossAnnualNumericUpDown.Value;
if (salary <= 0)
{
MessageBox.Show(SALARY_RANGE_ZERO);
ClearLabels();
return;
}
else if (salary > double.MaxValue)
{
MessageBox.Show(SALARY_RANGE_MAX_VALUE);
ClearLabels();
return;
}
double tax = 0;
foreach (var band in Bands)
{
if (salary > band.LowerLimit && band.Percentage > 0)
tax +=
Math.Min(band.UpperLimit - band.LowerLimit, salary - band.LowerLimit)
* band.Percentage;
}
SetLabels(salary, tax);
}
private void SetLabels(double salary, double tax)
{
double net = salary - tax;
GrossAnnualLabel.Text = salary.ToString("C");
GrossMonthlyLabel.Text = (salary / 12).ToString("C");
NetAnnualLabel.Text = net.ToString("C");
NetMonthlyLabel.Text = (net / 12).ToString("C");
AnnualTaxLabel.Text = tax.ToString("C");
MonthlyTaxLabel.Text = (tax / 12).ToString("C");
}
private void ClearLabels()
{
GrossAnnualLabel.Text = LABEL_PLACEHOLDER;
GrossMonthlyLabel.Text = LABEL_PLACEHOLDER;
NetAnnualLabel.Text = LABEL_PLACEHOLDER;
NetMonthlyLabel.Text = LABEL_PLACEHOLDER;
AnnualTaxLabel.Text = LABEL_PLACEHOLDER;
MonthlyTaxLabel.Text = LABEL_PLACEHOLDER;
}
}
public class TaxBand
{
public double LowerLimit { get; set; }
public double UpperLimit { get; set; }
public double Percentage { get; set; }
public TaxBand(double lower, double percentage)
{
LowerLimit = lower;
UpperLimit = double.MaxValue;
Percentage = percentage;
}
public TaxBand(double lower, double upper, double percentage)
{
LowerLimit = lower;
UpperLimit = upper;
Percentage = percentage;
}
}
}