top of page

Basic Java Programs for Beginners - Payroll Management System

Updated: Dec 17, 2023


Basic Java Programs for Beginners

Write Java Payroll Management System program to create a comprehensive payroll management system that allows HR administrators to add employee details, track working hours, calculate salaries, and generate pay stubs and reports.


This program uses basic data structures and console input/output for simplicity. In a real-world application, you'd typically use a database to store employee data.


Core Java Programming

Java Back-End Development


package payroll_management_system;

class Employee {

// Instance variables (attributes)

String id; // Employee ID

String name; // Employee name

double hourlyRate; // Employee's hourly pay rate

double hoursWorked; // Number of hours worked by the employee

double deductions; // Deductions from the employee's salary

double bonuses; // Bonuses added to the employee's salary


// Constructor to initialize employee information

Employee(String id, String name, double hourlyRate) {

this.id = id; // Assign the provided ID to the instance variable

this.name = name; // Assign the provided name to the instance variable

this.hourlyRate = hourlyRate; // Assign the provided hourly rate to the instance variable

}


// Method to calculate the gross salary

double calculateGrossSalary() {

return hoursWorked * hourlyRate; // Multiply hours worked by hourly rate

}


// Method to calculate the net salary

double calculateNetSalary() {

// Calculate gross salary using the calculateGrossSalary() method,

// then subtract deductions and add bonuses

return calculateGrossSalary() - deductions + bonuses;

}

}

PayrollManagementSystem.java package payroll_management_system;

import java.util.ArrayList;

import java.util.Scanner;


public class PayrollManagementSystem {

public static void main(String[] args) {


ArrayList<Employee> employees = new ArrayList<>();

Scanner scanner = new Scanner(System.in);


while (true) {

System.out.println("\nPayroll Management System Menu:");

System.out.println("1. Add Employee");

System.out.println("2. Calculate Salary");

System.out.println("3. Generate Pay Stub");

System.out.println("4. Exit");

System.out.print("Enter your choice: ");


int choice = scanner.nextInt();


switch (choice) {

case 1:

addEmployee(employees, scanner); // Calls the addEmployee method

break;

case 2:

calculateSalary(employees, scanner); // Calls the calculateSalary method

break;

case 3:

generatePayStub(employees, scanner); // Calls the generatePayStub method

break;

case 4:

System.out.println("Exiting the Payroll Management System. Goodbye!");

scanner.close();

System.exit(0);

default:

System.out.println("Invalid choice. Please try again.");

}

}

}


private static void addEmployee(ArrayList<Employee> employees, Scanner scanner) {

// Takes user input to add a new employee

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

String id = scanner.next();

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

String name = scanner.next();

System.out.print("Enter Hourly Rate: $");

double hourlyRate=scanner.nextDouble();

// Calls the Employee constructor to create an Employee object

Employee employee = new Employee(id, name, hourlyRate);

// Adds the employee to the employees ArrayList

employees.add(employee);

System.out.println("Employee added successfully.");

}


private static void calculateSalary(ArrayList<Employee> employees, Scanner scanner) {


// Takes user input to calculate the salary of an existing employee

// Modifies the hours worked, deductions, and bonuses for the employee

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

String id = scanner.next();

Employee employee = findEmployee(employees, id);

if (employee != null) {

System.out.print("Enter Hours Worked: ");

employee.hoursWorked = scanner.nextDouble();

System.out.print("Enter Deductions: $");

employee.deductions = scanner.nextDouble();

System.out.print("Enter Bonuses: $");

employee.bonuses = scanner.nextDouble();

System.out.println("Salary Calculated.");

} else {

System.out.println("Employee not found.");

}

}


private static void generatePayStub(ArrayList<Employee> employees, Scanner scanner) {

// Takes user input to generate a pay stub for an existing employee

// Displays information about the employee's gross and net salary

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

String id = scanner.next();

Employee employee = findEmployee(employees, id);

if (employee != null) {

System.out.println("\nPay Stub for Employee: " + employee.name);

System.out.println("Gross Salary: $" + employee.calculateGrossSalary());

System.out.println("Deductions: $" + employee.deductions);

System.out.println("Bonuses: $" + employee.bonuses);

System.out.println("Net Salary: $" + employee.calculateNetSalary());

} else {

System.out.println("Employee not found.");

}

}


private static Employee findEmployee(ArrayList<Employee> employees, String id) {

// Searches for an employee in the employees ArrayList by ID

// Returns the employee object if found, or null if not found

for (Employee employee : employees) {

if (employee.id.equals(id)) {

return employee;

}

}

return null;

}

}


Core Java Programming

Java Back-End Development

In this simplified program, you can add employees, calculate their salaries, and generate pay stubs. Employee data is stored in an ArrayList for demonstration purposes. In a real-world application, you would store this data in a database.

Please note that this program lacks data persistence (employee data won't be saved after exiting the program) and security measures. A production-level system would also include validation, authentication, and a more user-friendly interface. Additionally, consider using a database for data storage and retrieval to make the system more scalable and robust.

14 views0 comments

Comments


bottom of page