-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployee.java
More file actions
135 lines (113 loc) · 4.06 KB
/
Employee.java
File metadata and controls
135 lines (113 loc) · 4.06 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
package workers;
import java.io.Serializable;
import java.util.List;
public abstract class Employee extends Exception implements Comparable<Employee> ,Serializable{
private static final long serialVersionUID = 123456L; // Added for serialization
private String id;
private String firstName;
private String lastName;
private String phoneNumber;
private String emailAddress;
private Gender gender;
private double basicMonthlySalary;
public Employee(String id, String firstName, String lastName, String phoneNumber, String emailAddress, Gender gender, double basicMonthlySalary) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.gender = gender;
try {
setBasicMonthlySalary(basicMonthlySalary);
setPhoneNumber(phoneNumber);
setEmailAddress(emailAddress);
} catch (IllegalSalary | IllegalPhoneNumber | IllegalEmail e) {
this.basicMonthlySalary =5000.0;
this.phoneNumber = null;
this.emailAddress = null;
}
}
public Employee() { // Default constructor with default values
this.id = "";
this.firstName = "";
this.lastName = "";
this.phoneNumber = "";
this.emailAddress = "";
this.gender = Gender.MALE; //Default Gender
this.basicMonthlySalary = 5000.0; } //Minimum salary
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) throws IllegalPhoneNumber {
if (phoneNumber.matches("\\d{10}")) {
this.phoneNumber = phoneNumber;
} else {
throw new IllegalPhoneNumber("Invalid phone number. Phone number should be a 10-digit number."); }
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) throws IllegalEmail {
// Check if the email address contains '@', and ends with '.com'
if (emailAddress.contains("@") && (emailAddress.endsWith(".com")||emailAddress.endsWith(".co.il"))) {
this.emailAddress = emailAddress;
} else {
throw new IllegalEmail("Invalid email address format.");
}
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) { //We used an enum to check the correctness
this.gender = gender;
}
public double getBasicMonthlySalary() {
return basicMonthlySalary;
}
public void setBasicMonthlySalary(double basicMonthlySalary) throws IllegalSalary {
if (basicMonthlySalary <= 0) {
throw new IllegalArgumentException("Invalid basic monthly salary. Salary should be a positive value.");
}
this.basicMonthlySalary = basicMonthlySalary;
}
public abstract double calculateMonthlyIncome();
@Override
public String toString() {
return "ID: " + id +
", Name: " + firstName + " " + lastName +
", Phone Number: " + phoneNumber +
", Email Address: " + emailAddress +
", Gender: " + gender +
// " ,Basic Monthly Salary: $" + basicMonthlySalary +
", Total monthly income: $" + calculateMonthlyIncome();
}
@Override
public boolean equals(Object other) {
return (other instanceof Employee)
&& (this.id == ((Employee)other).getId());
}
@Override
public int compareTo(Employee other) {
if (this.lastName.equals(other.lastName))
return this.firstName.compareTo(other.firstName);
else {
return this.lastName.compareTo(other.lastName);
}
}
}