Object-Oriented Payroll System: Calculating Employee Salaries with Inheritance in Java

 You are required to write a Java program that computes salaries for a variety of employees, using object-oriented programming and inheritance principles.

Object-Oriented Payroll System: Calculating Employee Salaries with Inheritance in Java


The program should include four classes:

  1. Employee (Base class)

    • Holds employee’s name and monthly salary.

    • Methods:

      • Constructor to initialize data

      • annualSalary() method to return yearly salary

      • toString() method to describe the employee

  2. Salesman (Subclass of Employee)

    • Adds annual sales.

    • Methods:

      • Constructor

      • Overridden annualSalary() (includes commission: 2% of sales, up to $20,000)

      • Overridden toString()

  3. Executive (Subclass of Employee)

    • Adds stock price.

    • Methods:

      • Constructor

      • Overridden annualSalary() (includes $30,000 bonus if stock > $50)

      • Overridden toString()

  4. Payroll (Main class)

    • Reads employee info from a file (for years 2014 and 2015, max 10 employees each).

    • Stores employees in arrays by year.

    • Prints a report for each year, showing employee info and annual salary, and computes the average salary for the year.

Detailed Explanation

This project demonstrates object-oriented design by using inheritance to represent different employee types.

  • The Employee class encapsulates common attributes and behaviors.

  • Salesman and Executive are special types of employees, each with unique compensation rules, achieved through overriding methods.

  • The Payroll class handles file reading, object creation, data storage, and reporting.

Each employee type can be extended further or modified without changing the rest of the system, which is a core benefit of object-oriented programming.

Data is read from a text file formatted as:
Year Type Name MonthlySalary [AnnualSales|StockPrice]

Example:


2014 Employee Smith,John 2000 2015 Salesman Jones,Bill 3000 100000 2014 Executive Bush,George 5000 55

Employees are grouped by year, reports display all details and annual salaries, and average salaries are calculated for each year.


Full Java Code

java
import java.util.*; import java.io.*; class Employee { private String name; private int monthlySalary; public Employee(String name, int monthlySalary) { this.name = name; this.monthlySalary = monthlySalary; } public int annualSalary() { return monthlySalary * 12; } public String toString() { return "Name: " + name + ", Monthly Salary: $" + monthlySalary; } } class Salesman extends Employee { private int annualSales; private static final int MAX_COMMISSION = 20000; public Salesman(String name, int monthlySalary, int annualSales) { super(name, monthlySalary); this.annualSales = annualSales; } @Override public int annualSalary() { int commission = (int)(annualSales * 0.02); if (commission > MAX_COMMISSION) { commission = MAX_COMMISSION; } return super.annualSalary() + commission; } @Override public String toString() { return super.toString() + ", Annual Sales: $" + annualSales; } } class Executive extends Employee { private int stockPrice; public Executive(String name, int monthlySalary, int stockPrice) { super(name, monthlySalary); this.stockPrice = stockPrice; } @Override public int annualSalary() { int bonus = (stockPrice > 50) ? 30000 : 0; return super.annualSalary() + bonus; } @Override public String toString() { return super.toString() + ", Stock Price: $" + stockPrice; } } public class Payroll { public static void main(String[] args) { Employee[] employees2014 = new Employee[10]; Employee[] employees2015 = new Employee[10]; int count2014 = 0; int count2015 = 0; try { Scanner sc = new Scanner(new File("employees.txt")); while (sc.hasNextLine()) { String line = sc.nextLine(); String[] parts = line.split(" "); int year = Integer.parseInt(parts[0]); String type = parts[1]; String name = parts[2]; int monthlySalary = Integer.parseInt(parts[3]); Employee emp = null; if (type.equals("Employee")) { emp = new Employee(name, monthlySalary); } else if (type.equals("Salesman")) { int annualSales = Integer.parseInt(parts[4]); emp = new Salesman(name, monthlySalary, annualSales); } else if (type.equals("Executive")) { int stockPrice = Integer.parseInt(parts[4]); emp = new Executive(name, monthlySalary, stockPrice); } if (year == 2014 && count2014 < 10) { employees2014[count2014++] = emp; } else if (year == 2015 && count2015 < 10) { employees2015[count2015++] = emp; } } sc.close(); } catch (IOException e) { System.out.println("File error: " + e.getMessage()); return; } // Print report for each year printReport("2014", employees2014, count2014); printReport("2015", employees2015, count2015); } private static void printReport(String year, Employee[] employees, int count) { System.out.println("---- Salary Report for " + year + " ----"); int total = 0; for (int i = 0; i < count; i++) { Employee emp = employees[i]; int salary = emp.annualSalary(); System.out.println(emp.toString() + ", Annual Salary: $" + salary); total += salary; } if (count > 0) { double avg = total / (double)count; System.out.printf("Average Annual Salary for %s: $%.2f%n%n", year, avg); } else { System.out.println("No employees for year " + year + "\n"); } } }

📩 Need a similar solution? Email me: adel455@hotmail.com















Previous Post Next Post

Comments

Nepali Graphics - Learn design, Animation, and Progrmming