You are on page 1of 17

Computer Application Project

2023-2024

Name: Aakriti Bansal


Class: 10 D
Question
Employee Payroll Management System
The System maintains the employee ID, name, designation, age, and
basic pay for N employees in One-dimensional arrays.
Write a menu-driven application for performing the following
operations.
a) Accept Employees’ details
b) Sort the records in the ascending order of Basic Pay and display.
c) Print the following reports
(i)Employees aged between 50 and 60 (inclusive of both).
(ii)Employees who earn above a given salary.
d) Display Employee details
Accepts an Employee id and displays the employee’s details
e)Print Salary slips in the format shown below for a given month and
year.
SALARY SLIP FOR THE MONTH OF <month name> Year <year>
Employee ID :
Employee Name :
Designation :
Earnings Deductions
----------- -------------
Basic pay : PF:
HRA : TAX:
DA : Total Deductions:
Gross pay :
Net pay :
HRA is calculated as 20% of basic
DA is 10% of (Basic + HRA)
Gross pay is the sum of Basic,, HRA, and DA
PF is 12.25% of Basic Pay
TAX is 25% of Basic Pay
Total Deductions: PF+TAX
Net pay is Gross Pay – Deductions
Note: Store three usernames and their passwords in two arrays.
When the program executes, accept a username and a password
from the user, If they are valid then allow the user to work with the
system, otherwise, display an error message and exit the program
CODE
import java.util.Scanner;
public class EmployeePayrollSystem
{
public static int max_employees=100;// maximum amount of
employees the program can manage
public static int empId[]=new int[max_employees];
public static String empName[]=new String[max_employees];
public static String empDesignation[]=new String[max_employees];
public static int empAge[]=new int[max_employees];
public static double empBasicPay[]= new double[max_employees];
public static int numEmployees = 0;
public static void main()
{
Scanner sc= new Scanner(System.in);
String usernames[]= {"user1", "user2", "user3"};
String passwords[] = {"pass1", "pass2", "pass3"};
System.out.println("Enter Username: ");
String username= sc.nextLine();
System.out.println("Enter Password: ");
String password= sc.nextLine();
boolean flag= false;
for (int i = 0; i < usernames.length; i++)// for loop to check if user
entered values match any of the assigned usernames and passwords
{
if (username.equals(usernames[i]) &&
password.equals(passwords[i]))
{
flag= true;
break;
}
}
if (!flag)// if no match found
{
System.out.println("Authentication failed. Exiting...");
System.exit(0);
}
System.out.println("Enter the number of employees");
int n=sc.nextInt();
if(n>max_employees)
{
System.out.println("System cannot manage these many
employees");//by keeping check with a predefined maximum value,
we can make sure the program does not process more data than it
can handle
System.exit(0);
}
int choice;
do {
System.out.println("\nEmployee Payroll Management System
Menu:");
System.out.println("1. Accept Employees' details");
System.out.println("2. Sort and Display by Basic Pay");
System.out.println("3. Print Employees aged between 50 and
60");
System.out.println("4. Print Employees earning above a given
salary");
System.out.println("5. Display Employee details");
System.out.println("6. Print Salary Slip");
System.out.println("7. Exit");
System.out.print("Enter your choice: ");
choice =sc.nextInt();
switch (choice) {
case 1:
acceptEmployeeDetails(n);
break;
case 2:
sortAndDisplayByBasicPay();
break;
case 3:
printEmployeesBetweenAge50And60();
break;
case 4:
printEmployeesAboveSalary();
break;
case 5:
displayEmployeeDetails();
break;
case 6:
printSalarySlip();
break;
case 7:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice!=7);
}
public static void acceptEmployeeDetails(int a)
{
Scanner scanner=new Scanner(System.in);
if (numEmployees < a) {
System.out.print("Enter Employee ID: ");
empId[numEmployees] = scanner.nextInt();
scanner.nextLine(); //to read the next line efficiently
System.out.print("Enter Employee Name: ");
empName[numEmployees] = scanner.nextLine();
System.out.print("Enter Employee Designation: ");
empDesignation[numEmployees] = scanner.nextLine();
System.out.print("Enter Employee Age: ");
empAge[numEmployees] = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter Employee Basic Pay: ");
empBasicPay[numEmployees] = scanner.nextDouble();
scanner.nextLine();
numEmployees++;
System.out.println("Employee details added successfully.");
} else {
System.out.println("Employee database is full. Cannot add more
employees.");
}
}
public static void sortAndDisplayByBasicPay() {
// Sort the employees by Basic Pay (Bubble Sort)
for (int i = 0; i < numEmployees - 1; i++) {
for (int j = 0; j < numEmployees - i - 1; j++) {
if (empBasicPay[j] > empBasicPay[j + 1]) {
// Swap the employees
int tempId = empId[j];
empId[j] = empId[j + 1];
empId[j + 1] = tempId;
String tempName = empName[j];
empName[j] = empName[j + 1];
empName[j + 1] = tempName;

String tempDesignation = empDesignation[j];


empDesignation[j] = empDesignation[j + 1];
empDesignation[j + 1] = tempDesignation;

int tempAge = empAge[j];


empAge[j] = empAge[j + 1];
empAge[j + 1] = tempAge;

double tempBasicPay = empBasicPay[j];


empBasicPay[j] = empBasicPay[j + 1];
empBasicPay[j + 1] = tempBasicPay;
}
}
}

// Display the sorted employees


System.out.println("Employee details sorted by Basic Pay in
ascending order:");
for (int i = 0; i < numEmployees; i++) {
System.out.println("Employee ID: " + empId[i]);
System.out.println("Employee Name: " + empName[i]);
System.out.println("Employee Designation: " +
empDesignation[i]);
System.out.println("Employee Age: " + empAge[i]);
System.out.println("Employee Basic Pay: " + empBasicPay[i]);
System.out.println();
}
}
public static void printEmployeesBetweenAge50And60()
{
System.out.println("Employees aged between 50 and 60
(inclusive):");
boolean found = false; // A flag to check if any employees were
found in the age range

for (int i = 0; i < numEmployees; i++) {


if (empAge[i] >= 50 && empAge[i] <= 60) {
System.out.println("Employee ID: " + empId[i]);
System.out.println("Employee Name: " + empName[i]);
System.out.println("Employee Designation: " +
empDesignation[i]);
System.out.println("Employee Age: " + empAge[i]);
System.out.println("Employee Basic Pay: " + empBasicPay[i]);
System.out.println();
found = true; // Set the flag to true when an employee is
found in the age range
}
}

if (!found) {
System.out.println("No employees aged between 50 and 60
found.");
}
}
public static void printEmployeesAboveSalary() {
Scanner obj=new Scanner(System.in);
System.out.println("Enter the minimum salary: ");
double minSalary=obj.nextDouble();
System.out.println("Employees earning above " + minSalary + ":");
boolean found=false; // A flag to check if any employees were
found above the specified salary
for (int i = 0; i < numEmployees; i++) {
if (empBasicPay[i] > minSalary) {
System.out.println("Employee ID: " + empId[i]);
System.out.println("Employee Name: " + empName[i]);
System.out.println("Employee Designation: " +
empDesignation[i]);
System.out.println("Employee Age: " + empAge[i]);
System.out.println("Employee Basic Pay: " + empBasicPay[i]);
System.out.println();
found = true; // Set the flag to true when an employee is
found above the specified salary
}
}

if (!found) {
System.out.println("No employees earning above " + minSalary +
" found.");
}
}

public static void displayEmployeeDetails()


{
Scanner obj=new Scanner(System.in);
System.out.println("Enter Employee ID to display details: ");
int searchId = obj.nextInt();
boolean found = false;
for (int i = 0; i < numEmployees; i++) {
if (empId[i] == searchId) {
System.out.println("Employee ID: " + empId[i]);
System.out.println("Employee Name: " + empName[i]);
System.out.println("Employee Designation: " +
empDesignation[i]);
System.out.println("Employee Age: " + empAge[i]);
System.out.println("Employee Basic Pay: " +
empBasicPay[i]);
found = true;
break;
}
}
if (!found) {
System.out.println("Employee with ID " + searchId + " not
found.");
}
}
public static void printSalarySlip()
{
Scanner scanner=new Scanner(System.in);
System.out.println("Enter Employee ID to print salary slip: ");
int searchId = scanner.nextInt();
boolean found = false;
for (int i = 0; i < numEmployees; i++) {
if (empId[i] == searchId) {
System.out.println("Enter the month (e.g., January,
February, etc.): ");
scanner.nextLine();
String month = scanner.nextLine();
System.out.println("Enter the year: ");
int year = scanner.nextInt();
// Calculate salary components
double hra = 0.2 * empBasicPay[i];
double da = 0.1 * (empBasicPay[i] + hra);
double grossPay = empBasicPay[i] + hra + da;
double pf = 0.1225 * empBasicPay[i];
double tax = 0.25 * empBasicPay[i];
double totalDeductions = pf + tax;
double netPay = grossPay - totalDeductions;

// Print Salary Slip


System.out.println("SALARY SLIP FOR THE MONTH OF " +
month + " Year " + year);
drawLine(70);
System.out.println("Employee ID: " + empId[i]);
System.out.println("Employee Name: " + empName[i]);
System.out.println("Employee Designation: " +
empDesignation[i]);
System.out.println("Earnings\t\tDeductions");
System.out.println("-----------\t\t-------------");
System.out.println("Basic Pay: " + empBasicPay[i] + "\t\tPF: "
+ pf);
System.out.println("HRA: " + hra + "\t\tTAX: " + tax);
System.out.println("DA: " + da + "\t\tTotal Deductions: " +
totalDeductions);
System.out.println("Gross Pay: " + grossPay);
System.out.println("Net Pay: " + netPay);
drawLine(70);
found = true;
break;
}
}
if (!found) {
System.out.println("Employee with ID " + searchId + " not
found.");
}
}
public static void drawLine(int length)
{
for (int i = 0; i <length; i++) {
System.out.print("-");
}
System.out.println(); // Move to the next line after drawing the
line
}
}
Variable description table
Variable name Datatype Purpose
max_employees int Maximum number of
employees the program
can manage.
empId int[] Stores employee IDs.
empName String[] Stores employee names.
empDesignation String[] Stores employee
designations.
empAge int[] Stores employee age.
empBasicPay double[] Stores employee basic
pay amounts.
numemployees Int[] Current count of stored
employees.
usernames String[] Predefined usernames
for authentication.
passwords String[] Predefined passwords
for authentication.
flag boolean Used to check whether
the specified data is
found
n int The number of
employees entered by
the user
choice int The user's choice from
the menu of actions in
the program.
minSalary double The minimum salary
entered by the user for
filtering employees.
searchId int The employee ID
entered by the user to
search for a specific
employee.
month String The month for which
the salary slip is being
generated.
year int The year for which the
salary slip is being
generated.
hra double Calculating HRA

da double Calculating DA
grossPay double Calculating gross pay
pf double Calculating PF
tax double Calculating Tax
deduction
totalDeductions Double Calculating total
deductions (PF+ Tax)
netPay double Calculating net pay
(gross pay- total
deductions)
a int Used in the formal
parameter for
acceptEmployeeDetails
method

i int Used in the outer for


loop
j int Used in the inner for
loop
found boolean Check if the specified
data is found or not

You might also like