Employee Loan Eligibility – Polymorphism Program in java

In this post, we will learn Employee Loan Eligibility – Polymorphism Program in java Programming language.

Question:

Global Engineering is one of the fastest growing company. It needs to automate the transactions performed in the organization.  As start up, they need to automate the Employee management system. 

Partial code is provided to do this. Don’t change the skeleton. Do the additions wherever necessary.

You are provided with a public class Employee with protected attributes :

                                  int employeeId

                                  String employeeName

                                   double salary

       Appropriate public getters and setters are already written.

Write a public 2 argument constructor with arguments – employeeId,and employeeName.

Write a public abstract method calculateSalary()  in Employee class as,     

            public abstract void calculateSalary()

You are provided with a public class PermanentEmployee with private attribute : 

                double basicPay

       Appropriate public getters and setters are already written.

 Make this class PermanentEmployee to inherit the Employee class.

Write a public 3 argument constructor with arguments – employeeId, employeeName and basicPay.  

Implement the calculateSalary method in Employee class as

    salary = basicPay – PF amount

Here PF Amount = basicPay * 0.12; Set this value to the salary attribute.

You are provided with a public class TemporaryEmployee with private attribute : 

                int  hoursWorked

                int hourlyWages

     Appropriate public getters and setters are already written.

This class TemporaryEmployee should inherit the Employee class.

Write a public 4 argument constructor with arguments – employeeId, employeeName, hoursWorked and hourlyWages.  

Implement the calculateSalary method in Employee class as

        salary = hoursWorked * hourlyWages

Set this value to the salary attribute.

You are provided with a public class Loan

A method calculateLoanAmount is provided as shown below :

public double calculateLoanAmount(Employee employeeObj)

This method should calculate the loan amount and return that amount. 

Provide the implementation for this method as mentioned below

Loan amount is calculated as follows :

If the Employee object is of type PermanentEmployee the loan amount should be 15%  of the salary.  

If the Employee object is of type TemporaryEmployee the loan amount should be 10%  of the salary.

You are provided with a public class Main which has the main method.  

Check the correctness of the methods written in these classes.

Note :  All class, methods needs to be declared as public

CODE:

Main.java

import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        String b=sc.nextLine();
        double c=sc.nextDouble();
        double d=sc.nextDouble();
        int e=sc.nextInt();
        int f=sc.nextInt();
        Employee e1=new PermanentEmployee(a,b,d);
        e1.calculateSalary();
        Employee e2=new TemporaryEmployee(a,b,e,f);
        e2.calculateSalary();
        Loan l=new Loan();
        l.calculateLoanAmount(e1);
        l.calculateLoanAmount(e2);
    }
}

Employee.java

abstract public class Employee
{
	protected int employeeId;
    protected String employeeName;
    protected double salary;
    //Getters and Setters
  	public int getEmployeeId() {
  		return employeeId;
  	}
  	public void setEmployeeId(int employeeId) {
  		this.employeeId = employeeId;
  	}
  	public String getEmployeeName() {
  		return employeeName;
  	}
  	public void setEmployeeName(String employeeName) {
  		this.employeeName = employeeName;
  	}
  	public double getSalary() {
  		return salary;
  	}
  	public void setSalary(double salary) {
  		this.salary = salary;
  	}
    //Write a public 2 argument constructor with arguments – employeeId,and employeeName
    public Employee(int employeeId,String employeeName){
    this.employeeId=employeeId;
    this.employeeName=employeeName;
    }
    //Write a method -  public void calculateSalary()
	//Make this method as abstract
	abstract public void calculateSalary();
}

Loan.java

public class Loan {
	//Implement the below method
	public double calculateLoanAmount(Employee employeeObj) {
		double loan=0;
		if(employeeObj instanceof PermanentEmployee)
		{
		    loan=0.15*(employeeObj.getSalary());
		    return loan;
		}
		else
		{
		    loan=0.1*(employeeObj.getSalary());
		    return loan;
		}
	}
}

PremanentEmployee.java

//Make this class inherit the Employee class
public class PermanentEmployee extends Employee
{
    private double basicPay;
    // Getters and Setters
    public double getBasicPay() {
		return basicPay;
	}
	public void setBasicPay(double basicPay) {
		this.basicPay = basicPay;
	}
    //1. Write a public 3 argument constructor with arguments – employeeId, employeeName and basicPay.
    public PermanentEmployee(int employeeId,String employeeName,double basicpay)
    {
        super(employeeId,employeeName);
        //this.employeeId=employeeId;
        //this.employeeName=employeeName;
        this.basicPay=basicpay;
    }
	//2. Implement the  - public void calculateSalary() - method
    public void calculateSalary()
    {
        double PFamount=0;
        PFamount=(basicPay*0.12);
        setSalary(basicPay-PFamount);
    }
}

TemporaryEmployee.java

//Make this class inherit the Employee class
public class TemporaryEmployee extends Employee{
	private int  hoursWorked;
	private int hourlyWages;
	// Getters and Setters
	public int getHoursWorked() {
		return hoursWorked;
	}
	public void setHoursWorked(int hoursWorked) {
		this.hoursWorked = hoursWorked;
	}
	public int getHourlyWages() {
		return hourlyWages;
	}
	public void setHourlyWages(int hourlyWages) {
		this.hourlyWages = hourlyWages;
	}
	//1. Write a public 4 argument constructor with arguments – employeeId, employeeName, hoursWorked and hourlyWages.
	public TemporaryEmployee(int a,String b,int c,int d)
	{
	    super(a,b);
	    this.hoursWorked=c;
	    this.hourlyWages=d;
	}
	//2. Implement the  - public void calculateSalary() - method
	public void calculateSalary()
	{
	    setSalary(hoursWorked*hourlyWages);
	}
}
  1. Birthday
  2. Marathon Registration
  3. Array Manipulation – Use try with multi catch
Sharing Is Caring