Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
57 lines (46 loc) · 1.59 KB
/
Copy pathMain.java
File metadata and controls
57 lines (46 loc) · 1.59 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
class Employee {
private int employeeId;
private String name;
private double salary;
public Employee(int employeeId, String name, double salary) {
this.employeeId = employeeId;
this.name = name;
this.salary = salary;
}
// Package-private or public method with role check
public void setSalary(double newSalary, String role) {
if ("manager".equals(role) && newSalary >= 0) {
this.salary = newSalary;
System.out.println("Salary updated to: " + this.salary);
} else {
System.out.println("Access Denied for role: " + role);
}
}
public double getSalary() {
return salary;
}
}
class Manager {
private String role = "manager";
public void updateSalary(Employee emp, double newSalary) {
emp.setSalary(newSalary, role);
}
}
class Auditor {
private String role = "auditor";
public void tryToUpdateSalary(Employee emp, double newSalary) {
emp.setSalary(newSalary, role); // Will fail due to access control
}
}
public class Main {
public static void main(String[] args) {
Employee emp = new Employee(101, "Alice", 50000);
Manager manager = new Manager();
Auditor auditor = new Auditor();
System.out.println("\nAuditor tries to update salary:");
auditor.tryToUpdateSalary(emp, 60000); // ❌ Should be denied
System.out.println("\nManager tries to update salary:");
manager.updateSalary(emp, 60000); // ✅ Should work
System.out.println("\nFinal Salary: " + emp.getSalary());
}
}