'Employee Salary Calculation

Task Description

I have this 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 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 - id, name and salary, and returns the employee object.

public static int getPFPercentage() - which gets the PF percentage and returns the same

In the main method invoke the above two methods, and then call the calculateNetSalary method in Employee class and print the output as shown below.

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


What I have done

I wrote the getter & setters methods and calculateNetSalary() method in Employee.java. I am stuck in as what and how should I write in Main.java

Employee.java

public class Employee{

    private int employeeId;
    private String employeeName;
    private double salary;
    private double netSalary;

    //setters
    public void setEmployeeId(int employeeId){
        this.employeeId=employeeId;
    }
    public void setEmployeeName(String employeeName){
        this.employeeName=employeeName;
    }
    public void setSalary(double salary){
        this.salary=salary;
    }
    public void netSalary(double netSalary){
        this.netSalary=netSalary;
    }

    //getters
    public int getEmployeeId(){
        return employeeId;
    }
    public String getEmployeeName(){
        return employeeName;
    }
    public double getSalary(){
        return salary;
    }
    public double getNetSalary(){
        return netSalary;
    }

    public void calculateNetSalary(int pfpercentage){

        pfamount=salary*pfpercentage;
        netSalary=salary-pfamount;
    }
}

Main.java

import java.util.Scanner;
public class Main{
    public staic void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Employee emp = new Employee();

        System.out.println("Enter Id:"+setEmployeeId(sc.nextInt()))
        System.out.println("Enter Name:"+setEmployeeName(sc.next()));
        System.out.println("Enter salary:"+setSalary(sc.nextDouble()));

        System.out.println("Enter PF percentage:");
        double pfpercentage = sc.nextDouble();

        public static Employee getEmployeeDetails(){


        }

        public static int getPFPercentage(){

        }

    }
}

I am unable to complete Main.java as I am not sure what and how to write.



Solution 1:[1]

This should be your code:

 public void main(String[] args){
        Scanner sc = new Scanner(System.in);
        Employee emp = new Employee();

        emp.setEmployeeId(sc.nextInt());
        emp.setEmployeeName(sc.next()) ;
        emp.setSalary(sc.nextDouble());

        System.out.println("Enter PF percentage:");
        double pfpercentage = sc.nextDouble();
        emp.calculateNetSalary(pfpercentage);
        System.out.println("Salay is " + emp.getNetSalary());
    }

Also please notice that you haven't defined the type of pfamount:

 public void calculateNetSalary(double pfpercentage){

        double pfamount=salary*pfpercentage;
        netSalary=salary-pfamount;
    }

You can't define another methods inside the main() method. You can call other methods inside it (as much as you like).

Solution 2:[2]

You have a couple of issues in your code. Firstly looking at Employee.java, there are a couple of issues:

  1. Your method to set netSalary is declared as public void netSalary(double netSalary), where it should rather be public void SetNetSalary(double netSalary)
  2. The calculation in public void calculateNetSalary(int pfpercentage) looks incorrect. If you going to pass in a double (i.e. 2 to represent 2%) then you need to divide this by 100 to convert the number to percentage.
  3. You need to declare variables if you going to use them (namelty pfamount needs to be declared as a double before you can assign something to it).
  4. You'll probably need a public String toString() method, for printing out your employee object.

So you'll end up with something like this:

public class Employee{

  private int employeeId;
  private String employeeName;
  private double salary;
  private double netSalary;

//setters
  public void setEmployeeId(int employeeId){
      this.employeeId=employeeId;
  }

  public void setEmployeeName(String employeeName){
      this.employeeName=employeeName;
  }

  public void setSalary(double salary){
      this.salary=salary;
  }

  public void setNetSalary(double netSalary){
      this.netSalary=netSalary;
  }

  //getters
  public int getEmployeeId(){
      return employeeId;
  }

  public String getEmployeeName(){
      return employeeName;
  }

  public double getSalary(){
      return salary;
  }

  public double getNetSalary(){
      return netSalary;
  }

  public void calculateNetSalary(double pfpercentage) {
      double pfamount = salary * (pfpercentage / 100);
      netSalary = salary - pfamount;
  }

  @Override
  public String toString() {
      String output = new StringBuffer()
              .append("Id: ").append(employeeId)
              .append(System.lineSeparator()).append("Name: ").append(employeeName)
              .append(System.lineSeparator()).append("Salary: ").append(salary)
              .append(System.lineSeparator()).append("Net Salary: ").append(netSalary).toString();
      return output;
  }
}

The you also have a couple of issues with your Main.java:

  1. You can't declare methods within methods.
  2. This is not the correct way to implement java.util.Scanner ... System.out.println("Enter Id:"+setEmployeeId(sc.nextInt())). Essentially the Scanner portion needs to be separated from the System.out (remember the only thing System.out does is print out text, it does not wait for input from Scanner).
  3. If I understand your question correctly, you need to move some of your logic out of your main method and into getEmployeeDetails() and getPFPercentage()

So you'll end up with something like this:

import java.util.Scanner;

public class EmployeeSalaryCalculation {

private Scanner scanner;

  public EmployeeSalaryCalculation() {
    scanner = new Scanner(System.in);
  }

  public Employee getEmployeeDetails() {
      Employee employee = new Employee();
      System.out.println("Enter Id:");
      employee.setEmployeeId(scanner.nextInt());
      System.out.println("Enter Name:");
      employee.setEmployeeName(scanner.next());
      System.out.println("Enter salary:");
      employee.setSalary(scanner.nextDouble());
      return employee;
  }

  public double getPFPercentage(){
      System.out.println("Enter PF percentage:");
      return scanner.nextDouble();
  }

  public static void main(String[] args) {
      EmployeeSalaryCalculation employeeSalaryCalculation = new EmployeeSalaryCalculation();
      Employee employee = employeeSalaryCalculation.getEmployeeDetails();
      employee.calculateNetSalary(employeeSalaryCalculation.getPFPercentage());
      System.out.println(employee.toString());
  }
}

Solution 3:[3]

You've made a good start. However, there are a couple of syntax errors such as forgetting to end some statements with a semi-colon. Also, as far as I know, Java doesn't support nested methods, so you shouldn't have the methods getEmployeeDetails() and getPFPercentage() inside your main method. I have rearranged the code to correct that.

Other changes I made was to your Employee class, particularly the calculateNetSalary method. pfPercentage is divided by 100 before multiplication by salary. Also, the instance variable netSalary is set to be the local variable netSalary after it has been calculated using the appropriate setter method. The method netSalary has also been renamed to setNetSalary since that is more descriptive of what the method does.

I haven't made any other changes to your code other than completing the Main class according to specifications. If there's any other part of the code that needs clarification, you can leave a comment about that.

Main.java

import java.util.Scanner;

public class Main {

    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args){
        Employee newEmployee = getEmployeeDetails();
        double pfPercentage = getPFPercentage();

        System.out.println();
        System.out.println("Confirm employee details: ");
        System.out.println("Id : " + newEmployee.getEmployeeId());
        System.out.println("Name : " + newEmployee.getEmployeeName());
        System.out.println("Salary : " + newEmployee.getSalary());

        newEmployee.calculateNetSalary(pfPercentage);
        System.out.println("Net Salary : " + newEmployee.getNetSalary());
    }

    /**
     * Gets the details of a new employee from user input
     * @return the new {@link Employee}
     */
    public static Employee getEmployeeDetails() {
        Employee employee = new Employee();
        System.out.println("Enter Id: ");
        employee.setEmployeeId(scanner.nextInt());
        System.out.println("Enter Name: ");
        employee.setEmployeeName(scanner.next());
        System.out.println("Enter salary: ");
        employee.setSalary(scanner.nextDouble());
        return employee;
    }

    /**
     * Gets the PF percentage from user input
     * @return the PF percentage
     */
    public static double getPFPercentage(){
        System.out.println("Enter PF percentage:");
        double pfPercentage = scanner.nextDouble();
        return pfPercentage;
    }

}

Employee.java

public class Employee{

    private int employeeId;
    private String employeeName;
    private double salary;
    private double netSalary;

    // Setters
    public void setEmployeeId(int employeeId){
        this.employeeId = employeeId;
    }

    public void setEmployeeName(String employeeName){
        this.employeeName = employeeName;
    }

    public void setSalary(double salary){
        this.salary = salary;
    }

    private void setNetSalary(double netSalary){
        this.netSalary = netSalary;
    }

    // Getters
    public int getEmployeeId(){
        return employeeId;
    }

    public String getEmployeeName(){
        return employeeName;
    }

    public double getSalary(){
        return salary;
    }

    public double getNetSalary(){
        return netSalary;
    }

    public void calculateNetSalary (double pfPercentage){
        double pfAmount = salary * (pfPercentage / 100);
        double netSalary = salary - pfAmount;
        this.setNetSalary(netSalary);
    }
}

Solution 4:[4]

Write a class, Manager, that extends MonthlyEmployee, and has another instance variables, bonus and an instance method, setBonus. Override the pay method and update TestEmployee accordinly. 3.Write a class, TestShapes, that creates an instance of each of our shape classes and prints it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tal Angel
Solution 2 Ambro-r
Solution 3
Solution 4 Hassan Yakubu Abubakar