Java Program of Employee Loan Eligibility – Polymorphism

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);
	}
	
	
}

Recommended:

  1. Interest Calculator – Interface Concept
  2. Bank – Abstract concept
  3. Inheritance – Account
  4. Contact Details of Hosteller
  5. PF and Salary Calculation
  6. Check for Existence of Customer – equals method
  7. Account Manipulation – Abstract class
  8. Employee Loan Eligibility – Polymorphism

1 thought on “Java Program of Employee Loan Eligibility – Polymorphism”

  1. vehicle.java
    import java.util.*;
    public class Vehicle implements Loan,Insurance {

    private String vehicleNumber;
    private String modelName;
    private String vehicleType;
    private double price;

    public String getVehicleNumber() {
    return vehicleNumber;
    }
    public void setVehicleNumber(String vehicleNumber) {
    this.vehicleNumber = vehicleNumber;
    }
    public String getModelName() {
    return modelName;
    }
    public void setModelName(String modelName) {
    this.modelName = modelName;
    }

    public String getVehicleType() {
    return vehicleType;
    }
    public void setVehicleType(String vehicleType) {
    this.vehicleType = vehicleType;
    }
    public double getPrice() {
    return price;
    }
    public void setPrice(double price) {
    this.price = price;
    }

    public Vehicle(String vehicleNumber, String modelName, String vehicleType,double price) {

    this.vehicleNumber = vehicleNumber;
    this.modelName = modelName;
    this.vehicleType=vehicleType;
    this.price = price;
    }
    public double issueLoan()

    {
    // @Override

    if(vehicleType==”4 wheeler”)
    {
    return price*0.80;
    }
    else if(vehicleType==”3 wheeler”)
    {
    return price*0.75;
    }
    else if(vehicleType==”2 wheeler”)
    {
    return price*0.50;
    }
    return price;
    }
    public double takeInsurance()
    //@Override
    {
    if(price150000 && price300000)
    {
    price=5000;
    return price;
    }
    return price;

    }

    }
    loan.java
    public interface Loan
    {
    abstract double issueLoan();
    }
    insurance.java
    public interface Insurance{
    abstract double takeInsurance();
    }
    main.java
    import java.util.Scanner;
    public class Main
    {
    public static void main(String[] args)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println(“Enter the vehicle number”);
    String vnam=sc.nextLine();
    System.out.println(“enter the model name”);
    String mnam=sc.nextLine();
    System.out.println(“enter vehicle type”);
    String vtyp=sc.nextLine();
    System.out.println(“enter the price”);
    double pr=sc.nextDouble();
    Vehicle v=new Vehicle(vnam,mnam,vtyp,pr);
    double a= v.issueLoan();
    double b= v.takeInsurance();
    System.out.println(a);
    System.out.println(b);

    }
    }

Leave a Comment

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

x