yauhenip 6 anni fa
parent
commit
61f57c6fb7

+ 2
- 2
src/main/java/io/zipcoder/casino/GameFactory.java Vedi File

@@ -1,7 +1,7 @@
1 1
 package io.zipcoder.casino;
2 2
 
3 3
 
4
-
4
+/*
5 5
 public class GameFactory{
6 6
     public GameFactory(){
7 7
 
@@ -20,4 +20,4 @@ public class GameFactory{
20 20
     public static void createHighLow(Game game){
21 21
         game = HighLow.startGame();
22 22
     }
23
-}
23
+} */

+ 61
- 30
src/main/java/io/zipcoder/casino/HighLow.java Vedi File

@@ -14,37 +14,62 @@ public class HighLow extends DiceGame{
14 14
 
15 15
     private int askForWager() {
16 16
         int wager = 0;
17
-        boolean flag = true;
17
+        String input;
18 18
         Console.output("Current bankroll: " + this.player.getBankroll());
19
-        while (flag) {
20
-            try {
21
-                wager = Console.askForInteger("How much would you like to bet?");
22
-                if (wager <= 0 || wager > this.player.getBankroll()) {
23
-                    Console.output("Wrong input! Try again!");
24
-                } else {
25
-                    flag = false;
26
-                }
27
-            } catch(NumberFormatException e) {
28
-                Console.output("Wrong input! Try again!");
19
+        boolean state = true;
20
+        do {
21
+            input = Console.askForInput("How much would you like to bet?");
22
+            if (checkIfInteger(input) && checkBankroll(Integer.parseInt(input))) {
23
+                wager = Console.numberFromString(input);
24
+                state = false;
29 25
             }
30
-        }
31
-        this.player.subtractFromBankroll(wager);
26
+        } while (state);
27
+        bet(wager);
32 28
         return wager;
33 29
     }
34 30
 
35
-    public void bet(int bet) {
31
+    public boolean checkBankroll(int wager) {
32
+        boolean state;
33
+        if (wager <= 0 || wager > this.player.getBankroll()) {
34
+            Console.output("Wrong input! Try again!");
35
+            state = false;
36
+        } else {
37
+            state = true;
38
+        }
39
+        return state;
40
+    }
36 41
 
42
+    public boolean checkIfInteger(String input) {
43
+        boolean state;
44
+        if (!input.matches("\\d+")) {
45
+            Console.output("Wrong input! Try again!");
46
+            state = false;
47
+        } else {
48
+            state = true;
49
+        }
50
+        return state;
51
+    }
52
+
53
+    public void bet(int bet) {
54
+            this.player.subtractFromBankroll(bet);
37 55
     }
38 56
 
39
-    public String makeBet() {
40
-        betCondition = Console.askForInput("What are you betting on?  (H / L / 7)");
41
-        if (betCondition.toLowerCase().equals("h") ||
42
-            betCondition.toLowerCase().equals("l") ||
43
-            betCondition.toLowerCase().equals("7")) {
44
-                return betCondition;
57
+    public String checkInput(String input, String[] values) {
58
+        for (String currentValue : values) {
59
+            if (currentValue.toLowerCase().equals(input.toLowerCase())) {
60
+                return currentValue.toLowerCase();
61
+            }
45 62
         }
46 63
         Console.output("Wrong input. Please try again");
47
-        return makeBet();
64
+        return null;
65
+    }
66
+
67
+    public void makeBet() {
68
+        do {
69
+            String input = Console.askForInput("What are you betting on?  (H / L / 7)");
70
+            String[] values = {"h", "l", "7"};
71
+            betCondition = checkInput(input, values);
72
+        } while (betCondition == null);
48 73
     }
49 74
 
50 75
     public int rollDice() {
@@ -66,18 +91,24 @@ public class HighLow extends DiceGame{
66 91
     }
67 92
 
68 93
     public boolean askToContinue() {
69
-        if (this.player.getBankroll() == 0) {
70
-            Console.output("You're bankrupt! Please come back after pay day!");
94
+        if (!checkBankrupt()) {
71 95
             return false;
72 96
         }
73
-        String input = Console.askForInput("Would you like to continue?  (Y / N)");
74
-        if (input.toLowerCase().equals("y")) {
75
-            return true;
76
-        } else if (input.toLowerCase().equals("n")) {
97
+        String input;
98
+        do {
99
+            input = Console.askForInput("Would you like to continue?  (Y / N)");
100
+            String[] values = {"Y", "N"};
101
+            input = checkInput(input, values);
102
+        } while (input == null);
103
+        return input.equals("y");
104
+    }
105
+
106
+    public boolean checkBankrupt() {
107
+        if (this.player.getBankroll() == 0) {
108
+            Console.output("You're bankrupt! Please come back after pay day!");
77 109
             return false;
78 110
         }
79
-        Console.output("Wrong input. Please try again");
80
-        return askToContinue();
111
+        return true;
81 112
     }
82 113
 
83 114
     public void winBet(int winnings) {
@@ -93,7 +124,7 @@ public class HighLow extends DiceGame{
93 124
         Console.output("You're playing High-Low dice game");
94 125
         while (isPlaying) {
95 126
             totalBet         = askForWager();
96
-            betCondition     = makeBet();
127
+            makeBet();
97 128
             rolledDiceResult = rollDice();
98 129
             declareWinner();
99 130
             isPlaying        = askToContinue();

+ 97
- 20
src/test/java/io/zipcoder/casino/HighLowTest.java Vedi File

@@ -18,28 +18,105 @@ public class HighLowTest {
18 18
     }
19 19
 
20 20
     @Test
21
-    public void askForWager() {
22
-        int expected = 10;
23
-
24
-        int wager = 0;
25
-        boolean flag = true;
26
-        Console.output("Current bankroll: " + this.player.getBankroll());
27
-        while (flag) {
28
-            try {
29
-                wager = 10;
30
-                if (wager <= 0 || wager > this.player.getBankroll()) {
31
-                    Console.output("Wrong input! Try again!");
32
-                } else {
33
-                    flag = false;
34
-                }
35
-            } catch(NumberFormatException e) {
36
-                Console.output("Wrong input! Try again!");
37
-            }
38
-        }
39
-        this.player.subtractFromBankroll(wager);
40
-        int actual = wager;
21
+    public void checkInput1() {
22
+        String expected = null;
23
+        String[] values = {"fghA", "fgh","dsf"};
24
+        String actual = game.checkInput("zsdfe", values);
25
+        assertEquals(expected, actual);
26
+    }
27
+
28
+    @Test
29
+    public void checkInput2() {
30
+        String expected = "7";
31
+        String[] values = {"7", "6","7"};
32
+        String actual = game.checkInput("7", values);
33
+        assertEquals(expected, actual);
34
+    }
41 35
 
36
+    @Test
37
+    public void checkInput3() {
38
+        String expected = "h";
39
+        String[] values = {"b", "h","f"};
40
+        String actual = game.checkInput("H", values);
41
+        assertEquals(expected, actual);
42
+    }
43
+
44
+    @Test
45
+    public void checkInput4() {
46
+        String expected = "fgha";
47
+        String[] values = {"fghA", "fgh","dsf"};
48
+        String actual = game.checkInput("FGhA", values);
42 49
         assertEquals(expected, actual);
43 50
     }
44 51
 
52
+    @Test
53
+    public void checkBankrupt1() {
54
+        boolean expected = true;
55
+        boolean actual = game.checkBankrupt();
56
+        assertEquals(expected, actual);
57
+    }
58
+
59
+    @Test
60
+    public void checkBankrupt2() {
61
+        game.bet(500);
62
+        boolean expected = true;
63
+        boolean actual = game.checkBankrupt();
64
+        assertEquals(expected, actual);
65
+    }
66
+
67
+    @Test
68
+    public void checkBankrupt3() {
69
+        game.bet(500);
70
+        game.bet(500);
71
+        //boolean expected = false;
72
+        boolean actual = game.checkBankrupt();
73
+        assertEquals(false, actual);
74
+    }
75
+
76
+    @Test
77
+    public void checkBankrupt4() {
78
+        game.bet(1000);
79
+        boolean actual = game.checkBankrupt();
80
+        assertEquals(false, actual);
81
+    }
82
+
83
+    @Test
84
+    public void checkBankrupt5() {
85
+        player.subtractFromBankroll(1000);
86
+        boolean actual = game.checkBankrupt();
87
+        assertEquals(false, actual);
88
+    }
89
+
90
+    @Test
91
+    public void checkBankroll1() {
92
+        boolean actual = game.checkBankroll(-900);
93
+        assertEquals(false, actual);
94
+    }
95
+
96
+    @Test
97
+    public void checkBankroll2() {
98
+        boolean actual = game.checkBankroll(1100);
99
+        assertEquals(false, actual);
100
+    }
101
+
102
+    @Test
103
+    public void checkBankroll3() {
104
+        boolean actual = game.checkBankroll(900);
105
+        assertEquals(true, actual);
106
+    }
107
+
108
+    @Test
109
+    public void checkIfInteger1() {
110
+        boolean actual = game.checkIfInteger("90324");
111
+        assertEquals(true, actual);
112
+    }
113
+
114
+    @Test
115
+    public void checkIfInteger2() {
116
+        boolean actual = game.checkIfInteger("90dfg24");
117
+        assertEquals(false, actual);
118
+    }
119
+
120
+
121
+
45 122
 }