-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPayrollSystem.java
More file actions
155 lines (126 loc) · 5.69 KB
/
PayrollSystem.java
File metadata and controls
155 lines (126 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
// Base Employee class
abstract class Employee {
protected String name;
protected String employeeID;
protected double baseSalary;
protected static final double TAX_RATE = 0.1; // 10% tax
protected static final double PERFORMANCE_BONUS = 500; // Performance bonus for eligible employees
public Employee(String name, String employeeID, double baseSalary) {
this.name = name;
this.employeeID = employeeID;
this.baseSalary = baseSalary;
}
// Abstract method to calculate salary
public abstract double calculateSalary();
// Method to generate payslip
public void generatePayslip() {
System.out.println("Employee ID: " + employeeID);
System.out.println("Name: " + name);
System.out.println("Base Salary: $" + baseSalary);
System.out.println("Total Salary: $" + calculateSalary());
System.out.println("---------------------------");
}
}
// Full-Time Employee subclass
class FullTimeEmployee extends Employee {
private double overtimeHours;
private double overtimeRate;
public FullTimeEmployee(String name, String employeeID, double baseSalary, double overtimeHours, double overtimeRate) {
super(name, employeeID, baseSalary);
this.overtimeHours = overtimeHours;
this.overtimeRate = overtimeRate;
}
@Override
public double calculateSalary() {
double overtimePay = overtimeHours * overtimeRate;
double grossSalary = baseSalary + overtimePay + PERFORMANCE_BONUS;
return grossSalary - (grossSalary * TAX_RATE);
}
}
// Part-Time Employee subclass
class PartTimeEmployee extends Employee {
private double hoursWorked;
private double hourlyRate;
public PartTimeEmployee(String name, String employeeID, double hourlyRate, double hoursWorked) {
super(name, employeeID, 0);
this.hourlyRate = hourlyRate;
this.hoursWorked = hoursWorked;
}
@Override
public double calculateSalary() {
double grossSalary = hoursWorked * hourlyRate;
return grossSalary - (grossSalary * TAX_RATE);
}
}
// Contract Employee subclass
class ContractEmployee extends Employee {
private double contractBonus;
public ContractEmployee(String name, String employeeID, double baseSalary, double contractBonus) {
super(name, employeeID, baseSalary);
this.contractBonus = contractBonus;
}
@Override
public double calculateSalary() {
double grossSalary = baseSalary + contractBonus;
return grossSalary - (grossSalary * TAX_RATE);
}
}
// Payroll System
public class PayrollSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Employee> employees = new ArrayList<>();
try {
System.out.print("Enter the number of employees: ");
int numEmployees = Integer.parseInt(scanner.nextLine());
for (int i = 0; i < numEmployees; i++) {
System.out.println("Enter details for Employee " + (i + 1));
System.out.print("Name: ");
String name = scanner.nextLine();
System.out.print("Employee ID: ");
String employeeID = scanner.nextLine();
System.out.print("Enter employee type (FullTime/PartTime/Contract): ");
String type = scanner.nextLine().toLowerCase();
switch (type) {
case "fulltime" -> {
System.out.print("Base Salary: ");
double baseSalary = Double.parseDouble(scanner.nextLine());
System.out.print("Overtime Hours: ");
double overtimeHours = Double.parseDouble(scanner.nextLine());
System.out.print("Overtime Rate: ");
double overtimeRate = Double.parseDouble(scanner.nextLine());
employees.add(new FullTimeEmployee(name, employeeID, baseSalary, overtimeHours, overtimeRate));
}
case "parttime" -> {
System.out.print("Hourly Rate: ");
double hourlyRate = Double.parseDouble(scanner.nextLine());
System.out.print("Hours Worked: ");
double hoursWorked = Double.parseDouble(scanner.nextLine());
employees.add(new PartTimeEmployee(name, employeeID, hourlyRate, hoursWorked));
}
case "contract" -> {
System.out.print("Base Salary: ");
double baseSalary = Double.parseDouble(scanner.nextLine());
System.out.print("Contract Bonus: ");
double contractBonus = Double.parseDouble(scanner.nextLine());
employees.add(new ContractEmployee(name, employeeID, baseSalary, contractBonus));
}
default -> System.out.println("Invalid employee type.");
}
}
System.out.println("\nGenerating Payslips...");
for (Employee emp : employees) {
emp.generatePayslip();
}
} catch (NumberFormatException e) {
System.out.println("Invalid input! Please enter numbers correctly.");
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
} finally {
scanner.close();
}
}
}