Create a class named Employee. Employees are identified by their employee number,
last name, first name and salary.
Create the necessary instance variables and methods for the class Employee.
In your main project, create at least five (5) employee records and display each record.
Employee’s data must be based on data entry from the user.
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());
}
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;
}
public static double getPFPercentage(){
System.out.println("Enter PF percentage:");
double pfPercentage = scanner.nextDouble();
return pfPercentage;
}
}
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);
}
}
Comments
Leave a comment