-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP43_StudentClassEncapsulation.java
More file actions
56 lines (46 loc) · 1.57 KB
/
Copy pathP43_StudentClassEncapsulation.java
File metadata and controls
56 lines (46 loc) · 1.57 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
package programs;
/**
* ============================================================
* PROGRAM 43: Class Encapsulation with Getters & Setters
* ============================================================
* Problem: WAP to create an encapsulated `EmployeeRecord` class
* with private fields, validation rules in setters, and formatted getters.
* ============================================================
*/
class EmployeeRecord {
private String empId;
private String name;
private double salary;
public EmployeeRecord(String empId, String name, double salary) {
this.empId = empId;
setName(name);
setSalary(salary);
}
public String getEmpId() { return empId; }
public String getName() { return name; }
public double getSalary() { return salary; }
public void setName(String name) {
if (name != null && !name.trim().isEmpty()) {
this.name = name.trim();
}
}
public void setSalary(double salary) {
if (salary >= 0) {
this.salary = salary;
} else {
System.out.println(" ❌ Salary cannot be negative.");
}
}
public void display() {
System.out.printf(" Employee[%s: %s | Salary: $%,.2f]%n", empId, name, salary);
}
}
public class P43_StudentClassEncapsulation {
public static void main(String[] args) {
EmployeeRecord emp = new EmployeeRecord("EMP-101", "Yodha Raja", 85000.0);
emp.display();
emp.setSalary(-500); // rejected
emp.setSalary(95000.0); // accepted
emp.display();
}
}