forked from matyb/java-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAboutEnums.java
More file actions
59 lines (49 loc) · 1.38 KB
/
AboutEnums.java
File metadata and controls
59 lines (49 loc) · 1.38 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
package beginner;
import com.sandwich.koan.Koan;
import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;
public class AboutEnums {
enum Colors {
Red, Blue, Green, Yellow // what happens if you add a ; here?
// What happens if you type Red() instead?
}
@Koan
public void basicEnums() {
Colors blue = Colors.Blue;
assertEquals(blue == Colors.Blue, __);
assertEquals(blue == Colors.Red, __);
assertEquals(blue instanceof Colors, __);
}
@Koan
public void basicEnumsAccess() {
Colors[] colorArray = Colors.values();
assertEquals(colorArray[2], __);
}
enum SkatSuits {
Clubs(12), Spades(11), Hearts(10), Diamonds(9);
SkatSuits(int v) { value = v; }
private int value;
}
@Koan
public void enumsWithAttributes() {
// value is private but we still can access it. Why?
// Try moving the enum outside the AboutEnum class... What do you expect?
// What happens?
assertEquals(SkatSuits.Clubs.value > SkatSuits.Spades.value, __);
}
enum OpticalMedia {
CD(650), DVD(4300), BluRay(50000);
OpticalMedia(int c) {
capacityInMegaBytes = c;
}
int capacityInMegaBytes;
int getCoolnessFactor() {
return (capacityInMegaBytes - 1000) * 10;
}
}
@Koan
public void enumsWithMethods() {
assertEquals(OpticalMedia.CD.getCoolnessFactor(), __);
assertEquals(OpticalMedia.BluRay.getCoolnessFactor(), __);
}
}