-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployee.java
More file actions
72 lines (61 loc) · 2.61 KB
/
Copy pathEmployee.java
File metadata and controls
72 lines (61 loc) · 2.61 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
class Employee {
private static int totalEmployees = 0; // Static variable to track total employees
private int employeeID;
private String name;
private String department;
private double salary; // Private variable to ensure encapsulation
// Default constructor with default values
public Employee() {
this.employeeID = 0;
this.name = "Unknown";
this.department = "Not Assigned";
this.salary = 0.0;
totalEmployees++; // Increment total employees count
}
// Parameterized constructor
public Employee(int employeeID, String name, String department, double salary) {
this.employeeID = employeeID;
this.name = name;
this.department = department;
this.salary = salary;
totalEmployees++; // Increment total employees count
}
// Getter method to access private salary variable
public double getSalary() {
return this.salary;
}
// Method to calculate salary
public double calculateSalary() {
return this.salary; // Returning the salary of the employee
}
// Method to display employee details
public void displayEmployeeInfo() {
System.out.println("Employee ID: " + this.employeeID);
System.out.println("Name: " + this.name);
System.out.println("Department: " + this.department);
System.out.println("Salary: " + this.salary);
System.out.println("------------------------------");
}
// Static method to display total employees
public static void displayTotalEmployees() {
System.out.println("Total Employees: " + totalEmployees);
}
public static void main(String[] args) {
// Creating employees using default constructor
Employee emp1 = new Employee();
// Creating employees using parameterized constructor
Employee emp2 = new Employee(101, "Alice", "HR", 50000);
Employee emp3 = new Employee(102, "Bob", "IT", 60000);
Employee emp4 = new Employee(103, "Charlie", "Finance", 70000);
// Displaying employee details
emp1.displayEmployeeInfo();
emp2.displayEmployeeInfo();
emp3.displayEmployeeInfo();
emp4.displayEmployeeInfo();
// Displaying total number of employees
Employee.displayTotalEmployees();
// Displaying salary details using getter and calculateSalary method
System.out.println("Salary of " + emp2.name + ": " + emp2.getSalary());
System.out.println("Salary of " + emp3.name + ": " + emp3.calculateSalary());
}
}