RITIK PRABHAKAR

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.5 kB
5
Indexable
Never
public abstract class Employee {
    protected int employeeId;
    protected String employeeName;
    protected double salary;

    public Employee(int employeeId, String employeeName) {
        this.employeeId = employeeId;
        this.employeeName = employeeName;
    }

    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 abstract void calculateSalary();
}

// PermanentEmployee.java
public class PermanentEmployee extends Employee {
    private double basicPay;

    public PermanentEmployee(int employeeId, String employeeName, double basicPay) {
        super(employeeId, employeeName);
        this.basicPay = basicPay;
    }

    public double getBasicPay() {
        return basicPay;
    }

    public void setBasicPay(double basicPay) {
        this.basicPay = basicPay;
    }

    @Override
    public void calculateSalary() {
        double pfAmount = basicPay * 0.12;
        salary = basicPay - pfAmount;
    }
}

// TemporaryEmployee.java
public class TemporaryEmployee extends Employee {
    private int hoursWorked;
    private int hourlyWages;

    public TemporaryEmployee(int employeeId, String employeeName, int hoursWorked, int hourlyWages) {
        super(employeeId, employeeName);
        this.hoursWorked = hoursWorked;
        this.hourlyWages = hourlyWages;
    }

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

    @Override
    public void calculateSalary() {
        salary = hoursWorked * hourlyWages;
    }
}

// Loan.java
public class Loan {
    public double calculateLoanAmount(Employee employeeObj) {
        if (employeeObj instanceof PermanentEmployee) {
            double loanAmount = employeeObj.getSalary() * 0.15;
            return loanAmount;
        } else if (employeeObj instanceof TemporaryEmployee) {
            double loanAmount = employeeObj.getSalary() * 0.10;
            return loanAmount;
        } else {
            System.out.println("Invalid employee type!");
            return 0.0;
        }
    }
}

// Main.java
public class Main {
    public static void main(String[] args) {
        PermanentEmployee permanentEmployee = new PermanentEmployee(1, "RITIK", 5000.0);
        permanentEmployee.calculateSalary();
        System.out.println("Permanent Employee Salary: " + permanentEmployee.getSalary());

        TemporaryEmployee temporaryEmployee = new TemporaryEmployee(2, "PRABHAKAR", 40, 12);
        temporaryEmployee.calculateSalary();
        System.out.println("Temporary Employee Salary: " + temporaryEmployee.getSalary());

        Loan loan = new Loan();
        double loanAmount1 = loan.calculateLoanAmount(permanentEmployee);
        System.out.println("Loan Amount for Permanent Employee: " + loanAmount1);

        double loanAmount2 = loan.calculateLoanAmount(temporaryEmployee);
        System.out.println("Loan Amount for Temporary Employee: " + loanAmount2);
    }
}