I took the existing code, and hard-wired the inputs (rather than using Console.ReadLine()), I get:
You are 28 years old Salary= 30000
Tax= 3000 Family bonus= 750 Net= 25500
The main problem seems to be not initializing values - i.e. treating fields as variables:
public double getTax()
{
if (Salary < 10000)
tax = 0;
if (Salary <= 10000 && Salary >= 20000)
tax += Salary * 0.05;
else tax += Salary * 0.1;
return tax;
}
OK - and what does tax
start at if Salary >= 10000
, etc. Likewise familyBouns
in getFamilyBonus
. By the way, how can Salary be both <= 10000
and >= 20000
?
To illustrate, I've changed the output to:
Console.WriteLine("Tax= {0}", getTax());
Console.WriteLine("Tax= {0}", getTax());
Console.WriteLine("Tax= {0}", getTax());
Which shows:
Tax= 3000 Tax= 6000 Tax= 9000
My advice would be: don't store calculated values unless you know the math is so complex that it is worth it. Just calculate them as needed (no field at all).