Programming Pandit

c/c++/c#/Javav/Python


Latest Update

Monday, February 24, 2025

JAVA Program for Salary slip


Objective: Develop a java application with Employee class with Emp_name, Emp_id, Address, Mail_id, Mobile_no as members. Inherit the classes, Programmer, Assistant Professor, Associate Professor and Professor from employee class. Add Basic Pay (BP) as the member of all the inherited classes with 97% of BP as DA, 10 % of BP as HRA, 12% of BP as PF, 0.1% of BP for staff club fund. Generate pay slips for the employees with their gross and net salary.


Code: 

import java.util.Scanner;


// Base class Employee

class Employee {

    String empName, empId, address, mailId;

    long mobileNo;


    public Employee(String empName, String empId, String address, String mailId, long mobileNo) {

        this.empName = empName;

        this.empId = empId;

        this.address = address;

        this.mailId = mailId;

        this.mobileNo = mobileNo;

    }


    // Display common details

    public void displayEmployeeDetails() {

        System.out.println("Employee Name: " + empName);

        System.out.println("Employee ID: " + empId);

        System.out.println("Address: " + address);

        System.out.println("Mail ID: " + mailId);

        System.out.println("Mobile No: " + mobileNo);

    }

}


// Derived class for different designations

class Programmer extends Employee {

    double basicPay;


    public Programmer(String empName, String empId, String address, String mailId, long mobileNo, double basicPay) {

        super(empName, empId, address, mailId, mobileNo);

        this.basicPay = basicPay;

    }


    public void generatePaySlip() {

        System.out.println("\n====== PAY SLIP: PROGRAMMER ======");

        displayEmployeeDetails();

        calculateSalary();

    }


    public void calculateSalary() {

        double da = 0.97 * basicPay;

        double hra = 0.10 * basicPay;

        double pf = 0.12 * basicPay;

        double staffClubFund = 0.001 * basicPay;

        double grossSalary = basicPay + da + hra;

        double netSalary = grossSalary - (pf + staffClubFund);


        System.out.println("Basic Pay: " + basicPay);

        System.out.println("DA (97% of BP): " + da);

        System.out.println("HRA (10% of BP): " + hra);

        System.out.println("PF (12% of BP): " + pf);

        System.out.println("Staff Club Fund (0.1% of BP): " + staffClubFund);

        System.out.println("Gross Salary: " + grossSalary);

        System.out.println("Net Salary: " + netSalary);

    }

}


// Assistant Professor class

class AssistantProfessor extends Programmer {

    public AssistantProfessor(String empName, String empId, String address, String mailId, long mobileNo, double basicPay) {

        super(empName, empId, address, mailId, mobileNo, basicPay);

    }


    public void generatePaySlip() {

        System.out.println("\n====== PAY SLIP: ASSISTANT PROFESSOR ======");

        displayEmployeeDetails();

        calculateSalary();

    }

}


// Associate Professor class

class AssociateProfessor extends Programmer {

    public AssociateProfessor(String empName, String empId, String address, String mailId, long mobileNo, double basicPay) {

        super(empName, empId, address, mailId, mobileNo, basicPay);

    }


    public void generatePaySlip() {

        System.out.println("\n====== PAY SLIP: ASSOCIATE PROFESSOR ======");

        displayEmployeeDetails();

        calculateSalary();

    }

}


// Professor class

class Professor extends Programmer {

    public Professor(String empName, String empId, String address, String mailId, long mobileNo, double basicPay) {

        super(empName, empId, address, mailId, mobileNo, basicPay);

    }


    public void generatePaySlip() {

        System.out.println("\n====== PAY SLIP: PROFESSOR ======");

        displayEmployeeDetails();

        calculateSalary();

    }

}


// Main Class to run the program

public class EmployeePayroll {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);


        System.out.println("Enter Employee Name: ");

        String name = sc.nextLine();

        

        System.out.println("Enter Employee ID: ");

        String empId = sc.nextLine();

        

        System.out.println("Enter Employee Address: ");

        String address = sc.nextLine();

        

        System.out.println("Enter Employee Mail ID: ");

        String mailId = sc.nextLine();

        

        System.out.println("Enter Mobile No: ");

        long mobileNo = sc.nextLong();

        

        System.out.println("Enter Basic Pay: ");

        double basicPay = sc.nextDouble();

        

        System.out.println("Choose Employee Role:");

        System.out.println("1. Programmer\n2. Assistant Professor\n3. Associate Professor\n4. Professor");

        int choice = sc.nextInt();

     

        switch (choice) {

            case 1:

                Programmer prog = new Programmer(name, empId, address, mailId, mobileNo, basicPay);

                prog.generatePaySlip();

                break;

            case 2:

                AssistantProfessor asstProf = new AssistantProfessor(name, empId, address, mailId, mobileNo, basicPay);

                asstProf.generatePaySlip();

                break;

            case 3:

                AssociateProfessor assocProf = new AssociateProfessor(name, empId, address, mailId, mobileNo, basicPay);

                assocProf.generatePaySlip();

                break;

            case 4:

                Professor prof = new Professor(name, empId, address, mailId, mobileNo, basicPay);

                prof.generatePaySlip();

                break;

            default:

                System.out.println("Invalid choice!");

        }

        sc.close();

    }

}


Output:





No comments:

Post a Comment