The content of the if statements is not relevant to my question and I've removed as much as I can that's irrelevant.
I'm really struggling to understand what I need to do to have the method getNetSalary
return the value of the netSalary
variable.
I think I need to instantiate an object to have a value to be "returnable" but no matter how many youtube videos and other forum posts I read I can't get my head around it.
package mortgage;
import java.util.Scanner;
public class MortgageDecision {
public static void main(String[] args) {
double grossSalary;
Scanner input = new Scanner(System.in);
System.out.print("Gross Annual Salary: £");
grossSalary = input.nextDouble();
}
public static double getNetSalary(double grossSalary) {
double netSalary;
double taxFreeAllowance = 12500;
double taxFreeDeduction = 0;
double tax = 0;
if (grossSalary > 100000) {
taxFreeDeduction = (grossSalary - 100000) / 2;
}
if (taxFreeDeduction > 12500) {
taxFreeDeduction = 12500;
}
taxFreeAllowance = taxFreeAllowance - taxFreeDeduction;
if (grossSalary <= 50000) {
tax = (grossSalary - taxFreeAllowance) * 0.2;
} else if (grossSalary <= 100000) {
tax = ((grossSalary - 50000) * 0.3)
((grossSalary - taxFreeAllowance
- (grossSalary - 50000)) * 0.2);
} else if (grossSalary > 100000) {
tax = ((grossSalary - 100000) * 0.4) 15000
((50000 - taxFreeAllowance) * 0.2);
}
if (tax < 0) {
tax = 0;
}
netSalary = grossSalary - tax;
return netSalary;
}
CodePudding user response:
You want to call you getNetSalary method in your main method like so:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Gross Annual Salary: £");
double grossSalary = input.nextDouble();
System.out.println(String.valueOf(grossSalary);
double netSalary = getNetSalary(grossSalary);
System.out.print("Net salary: £");
System.out.println(String.valueOf(netSalary);
}
And some hints on programm structure in Java:
- You seem to use a C/C stlye of programming, usually in Java you don't need to declaration all used variables at the top.
- That is why I would inline
netSalary
and move down the declaration oftax
- You declared
taxFreeAllowance
inside the method and you don't overwrite it inside. You might want to extract it as a constant static field.