-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEnumMaps.java
More file actions
43 lines (33 loc) · 1.04 KB
/
EnumMaps.java
File metadata and controls
43 lines (33 loc) · 1.04 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
package enumerated;
import java.util.*;
import static enumerated.AlarmPoints.*;
/**
* RUN:
* javac enumerated/EnumMaps.java && java enumerated.EnumMaps
* OUTPUT:
* BATHROOM: Bathroom alert!
* KITCHEN: Kitchen fire!
* java.lang.NullPointerException
*/
public class EnumMaps {
public static void main(String[] args) {
EnumMap<AlarmPoints, Command> em = new EnumMap<AlarmPoints, Command>(AlarmPoints.class);
em.put(KITCHEN, new Command() {
public void action() { System.out.println("Kitchen fire!"); }
});
em.put(BATHROOM, new Command() {
public void action() { System.out.println("Bathroom alert!"); }
});
for (Map.Entry<AlarmPoints, Command> e : em.entrySet()) {
System.out.print(e.getKey() + ": ");
e.getValue().action();
}
try {
em.get(UTILITY).action();
}
catch (Exception e) {
System.out.println(e);
}
}
}
interface Command { void action(); }