Java program to calculate employee salary

java

Problem statement:

Create a class Employee with the following private member variables.
• int employeeId

• String employeeName

• double salary

• double netSalary
Include appropriate getters and setters method in Employee class. Write the following method in the Employee class: public void calculateNetSalary(int pfpercentage) – This method should take PF percentage as an argument. Deduct the PF amount from the salary and set the netSalary.

Create a Main class which has the main method which invokes the method to get the input and prints the details as shown in the sample.
Also write a method :
public static Employee getEmployeeDetails() – which gets the employee details and returns the employee object.
public static int getPFPercentage() – which gets the PF percentage and returns the same

Note : In getEmployeeDetails method get the employeeId, employeeName and salary alone. Ensure not to get the PF percentage
In main method invoke the getEmployeeDetails, next invoke getPFPercentage and then invoke calculateNetSalary.


Sample Input 1:
Enter Id: 101

Enter Name: Vivek

Enter salary: 20000

Enter PF percentage: 7


Sample Output 1:
Id: 101
Name: Vivek
Salary: 20000.0
Net Salary: 18600.0

Solution:

Employee.java :
public class Employee {
	private int employeeId;
	private String employeeName;
	private double salary;
	private double netSalary;
	public void setEmployeeId(int employeeId) {
		this.employeeId=employeeId;
	}
	public void setEmployyeName(String employeeName) {
		this.employeeName=employeeName;
	}
	public void setSalary(double salary) {
		this.salary=salary;
		
	}
	public void setNetSalary(double netSalary) {
		this.netSalary=netSalary;
	}
	public int getEmployeeId() {
		return this.employeeId;
	
	}
	public String getEmployeeName() {
		return this.employeeName;
	
	}
	public double getSalary() {
		return this.salary;
	}
	public double getNetSalary() {
		return this.netSalary;
	}
	public void calculateNetSalary(int pfpercentage) {
		this.netSalary=this.salary-(this.salary*pfpercentage/100);
	}
}
Main.java :
import java.util.Scanner;
public class Main extends Employee {
	public static Employee getEmployeeDetails() {
		Scanner sc=new Scanner(System.in);
		Employee emp=new Employee();
		System.out.println("Enter Id:");
		emp.setEmployeeId(sc.nextInt());
		sc.nextLine();
		System.out.println("Enter Name:");
		emp.setEmployyeName(sc.nextLine());
		System.out.println("Enter Salary:");
		emp.setSalary(sc.nextDouble());
		
		
		return emp;
	}
	public static int getPFPercentage() {
		Scanner sc=new Scanner(System.in);
		System.out.println("Enter PF percentage");
		return sc.nextInt();
	}
	public static void main(String[] args) {
		Employee emp=new Employee();
		emp=getEmployeeDetails();
		emp.calculateNetSalary(getPFPercentage());
		System.out.println("Id: "+emp.getEmployeeId());
		System.out.println("Name: "+emp.getEmployeeName());
		System.out.println("Salary: "+emp.getSalary());
		System.out.println("Net Salary: "+emp.getNetSalary());

	}

}

1 thought on “Java program to calculate employee salary”

  1. In the above program there is no method call for setNetSalary(). I have mentioned the call in calculateNetSalary() and it works.
    // code

    public void calculateNetSalary(int pfpercentage){
    NetSalary=salary-(salary*pfpercentage/100);
    setNetSalary(NetSalary);
    }

Leave a Comment

Your email address will not be published. Required fields are marked *