|
| 1 | +package beginner; |
| 2 | + |
| 3 | +import com.sandwich.koan.Koan; |
| 4 | +import static com.sandwich.koan.constant.KoanConstants.__; |
| 5 | +import static com.sandwich.util.Assert.assertEquals; |
| 6 | + |
| 7 | +public class AboutEnums { |
| 8 | + |
| 9 | + |
| 10 | + enum Colors { |
| 11 | + Red, Blue, Green, Yellow // what happens if you add a ; here? |
| 12 | + // What happens if you type Red() instead? |
| 13 | + } |
| 14 | + |
| 15 | + @Koan |
| 16 | + public void basicEnums() { |
| 17 | + Colors blue = Colors.Blue; |
| 18 | + assertEquals(blue == Colors.Blue, __); |
| 19 | + assertEquals(blue == Colors.Red, __); |
| 20 | + assertEquals(blue instanceof Colors, __); |
| 21 | + } |
| 22 | + |
| 23 | + @Koan |
| 24 | + public void basicEnumsAccess() { |
| 25 | + Colors[] colorArray = Colors.values(); |
| 26 | + assertEquals(colorArray[2], __); |
| 27 | + } |
| 28 | + |
| 29 | + enum SkatSuits { |
| 30 | + Clubs(12), Spades(11), Hearts(10), Dimonds(9); |
| 31 | + SkatSuits(int v) { value = v; } |
| 32 | + private int value; |
| 33 | + } |
| 34 | + |
| 35 | + @Koan |
| 36 | + public void enumsWithAttributes() { |
| 37 | + // value is private but we still can access it. Why? |
| 38 | + // Try moving the enum outside the AboutEnum class... What do you expect? |
| 39 | + // What happens? |
| 40 | + assertEquals(SkatSuits.Clubs.value > SkatSuits.Spades.value, __); |
| 41 | + } |
| 42 | + |
| 43 | + enum OpticalMedia { |
| 44 | + CD(650), DVD(4300), BluRay(50000); |
| 45 | + OpticalMedia(int c) { |
| 46 | + capacityInMegaBytes = c; |
| 47 | + } |
| 48 | + int capacityInMegaBytes; |
| 49 | + int getCoolnessFactor() { |
| 50 | + return (capacityInMegaBytes - 1000) * 10; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Koan |
| 55 | + public void enumsWithMethods() { |
| 56 | + assertEquals(OpticalMedia.CD.getCoolnessFactor(), __); |
| 57 | + assertEquals(OpticalMedia.BluRay.getCoolnessFactor(), __); |
| 58 | + } |
| 59 | +} |
0 commit comments