-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPersonnelManagementSystem.java
More file actions
150 lines (118 loc) · 4.93 KB
/
Copy pathPersonnelManagementSystem.java
File metadata and controls
150 lines (118 loc) · 4.93 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
import java.util.Scanner;
// Base Class
class Employee {
private String name;
private int employeeId;
// Parameterized constructor
public Employee(String name, int employeeId) {
this.name = name;
this.employeeId = employeeId;
}
public String getName() { return name; }
public int getEmployeeId() { return employeeId; }
// Base method to calculate salary (designed to be overridden)
public double calculateSalary() {
return 0.0;
}
// Overloaded method to calculate salary with a bonus
public double calculateSalary(double bonus) {
return calculateSalary() + bonus;
}
// Display employee details
public void display() {
System.out.println("Employee ID: " + employeeId + " | Name: " + name);
}
}
// Subclass for Full-Time Employee
class FullTimeEmployee extends Employee {
private double monthlySalary;
// Constructor to initialize full-time employee details
public FullTimeEmployee(String name, int employeeId, double monthlySalary) {
super(name, employeeId);
this.monthlySalary = monthlySalary;
}
// Overriding the base method
@Override
public double calculateSalary() {
return monthlySalary;
}
}
// Subclass for Part-Time Employee
class PartTimeEmployee extends Employee {
private double hourlyRate;
private int hoursWorked;
// Constructor to initialize part-time employee details
public PartTimeEmployee(String name, int employeeId, double hourlyRate, int hoursWorked) {
super(name, employeeId);
this.hourlyRate = hourlyRate;
this.hoursWorked = hoursWorked;
}
// Overriding the base method
@Override
public double calculateSalary() {
return hourlyRate * hoursWorked;
}
}
// Main Driver Class
public class PersonnelManagementSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// --- 1. Full-Time Employee Input ---
System.out.println("--- Full-Time Employee Data Entry ---");
System.out.print("Enter Name: ");
String ftName = scanner.nextLine();
System.out.print("Enter ID: ");
int ftId = getValidInt(scanner);
System.out.print("Enter Base Monthly Salary: ");
double ftSalary = getValidDouble(scanner);
System.out.print("Enter Bonus Amount: ");
double ftBonus = getValidDouble(scanner);
FullTimeEmployee fullTime = new FullTimeEmployee(ftName, ftId, ftSalary);
// --- 2. Part-Time Employee Input ---
System.out.println("\n--- Part-Time Employee Data Entry ---");
scanner.nextLine(); // Consume the leftover newline from previous numeric inputs
System.out.print("Enter Name: ");
String ptName = scanner.nextLine();
System.out.print("Enter ID: ");
int ptId = getValidInt(scanner);
System.out.print("Enter Hourly Rate: ");
double ptRate = getValidDouble(scanner);
System.out.print("Enter Hours Worked: ");
int ptHours = getValidInt(scanner);
System.out.print("Enter Bonus Amount: ");
double ptBonus = getValidDouble(scanner);
PartTimeEmployee partTime = new PartTimeEmployee(ptName, ptId, ptRate, ptHours);
// --- 3. Display Results ---
System.out.println("\n========================================");
System.out.println(" EMPLOYEE DETAILS");
System.out.println("========================================");
// Demonstrate Overriding & Overloading for Full-Time
System.out.println("[Full-Time Employee]");
fullTime.display();
System.out.println("Base Salary: " + fullTime.calculateSalary());
System.out.println("Salary with Bonus: " + fullTime.calculateSalary(ftBonus));
System.out.println("----------------------------------------");
// Demonstrate Overriding & Overloading for Part-Time
System.out.println("[Part-Time Employee]");
partTime.display();
System.out.println("Base Salary (Rate x Hours): " + partTime.calculateSalary());
System.out.println("Salary with Bonus: " + partTime.calculateSalary(ptBonus));
System.out.println("========================================");
scanner.close();
}
// --- Helper Methods to Prevent Scanner Crashes ---
private static int getValidInt(Scanner scanner) {
while (!scanner.hasNextInt()) {
System.out.print("Invalid input. Please enter a valid whole number: ");
scanner.next(); // Consume invalid input
}
return scanner.nextInt();
}
private static double getValidDouble(Scanner scanner) {
while (!scanner.hasNextDouble()) {
System.out.print("Invalid input. Please enter a valid number: ");
scanner.next(); // Consume invalid input
}
return scanner.nextDouble();
}
}