File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .EnumSet ;
2
+
3
+ public class Carwash {
4
+ public enum Cycle {
5
+ UNDERBODY {
6
+ void action () { System .out .print ("Spraying the underbody" ); }
7
+ },
8
+ WHEELWASH {
9
+ void action () { System .out .print ("Washing the wheels" ); }
10
+ },
11
+ PREWASH {
12
+ void action () { System .out .print ("Loosening the dirt" ); }
13
+ },
14
+ BASIC {
15
+ void action () { System .out .print ("The basic wash" ); }
16
+ },
17
+ HOTWAX {
18
+ void action () { System .out .print ("Applying hot wax" ); }
19
+ },
20
+ RINSE {
21
+ void action () { System .out .print ("Rinsing" ); }
22
+ },
23
+ BLOWDRY {
24
+ void action () { System .out .print ("Blowing dry" ); }
25
+ };
26
+ abstract void action ();
27
+ }
28
+ EnumSet <Cycle > cycles =
29
+ EnumSet .of (Cycle .BASIC , Cycle .RINSE );
30
+ public void add (Cycle cycle ) { cycles .add (cycle ); }
31
+ public void washCar () {
32
+ for (Cycle c : cycles )
33
+ c .action ();
34
+ }
35
+ public String toString () { return cycles .toString (); }
36
+ public static void main (String [] args ) {
37
+ Carwash wash = new Carwash ();
38
+ System .out .print (wash );
39
+ wash .washCar ();
40
+ // Order of addition is unimportant:
41
+ wash .add (Cycle .BLOWDRY );
42
+ wash .add (Cycle .BLOWDRY ); // Duplicates ignored
43
+ wash .add (Cycle .RINSE );
44
+ wash .add (Cycle .HOTWAX );
45
+ System .out .print (wash );
46
+ wash .washCar ();
47
+ }
48
+ }
You can’t perform that action at this time.
0 commit comments