001package beginner;
002
003
004import com.sandwich.koan.Koan;
005
006import static com.sandwich.koan.constant.KoanConstants.__;
007import static com.sandwich.util.Assert.assertEquals;
008
009
010public class AboutConditionals {
011
012    @Koan
013    public void basicIf() {
014        int x = 1;
015        if (true) {
016            x++;
017        }
018        assertEquals(x, __);
019    }
020
021    @Koan
022    public void basicIfElse() {
023        int x = 1;
024        boolean secretBoolean = false;
025        if (secretBoolean) {
026            x++;
027        } else {
028            x--;
029        }
030        assertEquals(x, __);
031    }
032
033    @Koan
034    public void basicIfElseIfElse() {
035        int x = 1;
036        boolean secretBoolean = false;
037        boolean otherBooleanCondition = true;
038        if (secretBoolean) {
039            x++;
040        } else if (otherBooleanCondition) {
041            x = 10;
042        } else {
043            x--;
044        }
045        assertEquals(x, __);
046    }
047
048    @Koan
049    public void nestedIfsWithoutCurlysAreReallyMisleading() {
050        // Why are these ugly you ask? Well, try for yourself
051        int x = 1;
052        boolean secretBoolean = false;
053        boolean otherBooleanCondition = true;
054        // Ifs without curly braces are ugly and not recommended but still valid:
055        if (secretBoolean) {
056            x++;
057        }
058        if (otherBooleanCondition) {
059            x = 10;
060        } else {
061            x--;
062        }
063        // Where does this else belong to!?
064        assertEquals(x, __);
065    }
066
067    @Koan
068    public void basicSwitchStatement() {
069        int i = 1;
070        String result = "Basic ";
071        switch (i) {
072            case 1:
073                result += "One";
074                break;
075            case 2:
076                result += "Two";
077                break;
078            default:
079                result += "Nothing";
080        }
081        assertEquals(result, __);
082    }
083
084    @Koan
085    public void switchStatementFallThrough() {
086        int i = 1;
087        String result = "Basic ";
088        switch (i) {
089            case 1:
090                result += "One";
091            case 2:
092                result += "Two";
093            default:
094                result += "Nothing";
095        }
096        assertEquals(result, __);
097    }
098
099    @Koan
100    public void switchStatementCrazyFallThrough() {
101        int i = 5;
102        String result = "Basic ";
103        switch (i) {
104            case 1:
105                result += "One";
106            default:
107                result += "Nothing";
108            case 2:
109                result += "Two";
110        }
111        assertEquals(result, __);
112    }
113
114    @Koan
115    public void switchStatementConstants() {
116        int i = 5;
117        // What happens if you remove the 'final' modifier?
118        // What does this mean for case values?
119        final int caseOne = 1;
120        String result = "Basic ";
121        switch (i) {
122            case caseOne:
123                result += "One";
124                break;
125            default:
126                result += "Nothing";
127        }
128        assertEquals(result, __);
129    }
130
131    @Koan
132    public void switchStatementSwitchValues() {
133        // Try different (primitive) types for 'c'
134        // Which types do compile?
135        // Does boxing work?
136        char c = 'a';
137        String result = "Basic ";
138        switch (c) {
139            case 'a':
140                result += "One";
141                break;
142            default:
143                result += "Nothing";
144        }
145        assertEquals(result, __);
146    }
147
148    @Koan
149    public void shortCircuit() {
150        int i = 1;
151        int a = 6;
152        // Why did we use a variable here?
153        // What happens if you replace 'a' with '6' below?
154        if ((a < 9) || (++i < 8)) i = i + 1;
155        assertEquals(i, __);
156    }
157}