-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployee.java
More file actions
72 lines (63 loc) · 2.53 KB
/
Copy pathEmployee.java
File metadata and controls
72 lines (63 loc) · 2.53 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
package Phase3_ObjectOrientation.EmployeeApp;
/**
* Employee - the core CONTRACT every staff member must satisfy.
* <p>
*
* Implemented as a SEALED INTERFACE (Java 17+) so the company knows exactly
* which kinds of employees exist. Adding a new permitted record below will
* cause every exhaustive `switch (employee) { ... }` elsewhere to fail to
* compile until the new case is handled - a strong build-time safety net.
* <p>
*
* Why an interface for the root type?
* -----------------------------------
* - Employees vary widely (full-time, contractor, intern, ...) - we want a
* POLYMORPHIC type without forcing a deep class hierarchy.
* - Records make great variants: immutable, compact, equality for free.
* - The interface carries shared DEFAULT behaviour (annualSalary,
* formattedId) that every implementor inherits.
* <p>
*
* Interface members used here
* ---------------------------
* - abstract methods : id, name, monthlyPay
* - default methods : annualSalary, formattedId, summary
* - static methods : nextId - centralised id generator
* - private methods : pad - helper used by formattedId
* - constants : MAX_PAY - inherited public static final
* - permits clause : restricts implementors to known types
*/
public sealed interface Employee permits FullTimeEmployee, PartTimeEmployee, Contractor, Intern {
// ----- abstract contract every implementor must provide -----
int id();
String name();
double monthlyPay();
// ----- shared constant (implicit public static final) -----
double MAX_PAY = 1_000_000.0;
// ----- shared default behaviour -----
default double annualSalary() {
return monthlyPay() * 12;
}
default String formattedId() {
return "EMP-" + pad(id(), 5);
}
default String summary() {
return String.format("%s | %-30s | monthly=%9.2f | annual=%9.2f",
formattedId(), name(), monthlyPay(), annualSalary());
}
// ----- private helper (Java 9+) - hidden from outside callers -----
private static String pad(int value, int width) {
String s = String.valueOf(value);
return "0".repeat(Math.max(0, width - s.length())) + s;
}
// ----- static factory utility -----
static int nextId() {
return Counter.next();
}
/** A tiny private holder for the id counter - hidden inside the interface. */
final class Counter {
private Counter() {}
private static int n = 0;
static int next() { return ++n; }
}
}