Преглед на файлове

dice class and test are done

yauhenip преди 6 години
родител
ревизия
861b59df52
променени са 2 файла, в които са добавени 78 реда и са изтрити 0 реда
  1. 38
    0
      src/main/java/io/zipcoder/casino/Dice.java
  2. 40
    0
      src/test/java/io/zipcoder/casino/DiceTest.java

+ 38
- 0
src/main/java/io/zipcoder/casino/Dice.java Целия файл

@@ -0,0 +1,38 @@
1
+package io.zipcoder.casino;
2
+
3
+import java.util.Random;
4
+
5
+    public class Dice
6
+    {
7
+        private int dice;
8
+        private Random randomGenerator;
9
+
10
+        public Dice(int numberOfDice)
11
+        {
12
+            dice = numberOfDice;
13
+        }
14
+
15
+        public int getSum()
16
+        {
17
+            int sumOfTosses = 0;
18
+            for (int i=0; i<dice; i++)
19
+            {
20
+                randomGenerator = new Random();
21
+                int resultOfToss = randomGenerator.nextInt(6)+1;
22
+                sumOfTosses += resultOfToss;
23
+            }
24
+            return sumOfTosses;
25
+        }
26
+
27
+        public int[] getEach()
28
+        {
29
+            int[] tosses = new int[dice];
30
+            for (int i=0; i<dice; i++)
31
+            {
32
+                randomGenerator = new Random();
33
+                tosses[i] = randomGenerator.nextInt(6)+1;
34
+            }
35
+            return tosses;
36
+        }
37
+    }
38
+

+ 40
- 0
src/test/java/io/zipcoder/casino/DiceTest.java Целия файл

@@ -0,0 +1,40 @@
1
+package io.zipcoder.casino;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Before;
5
+import org.junit.Test;
6
+
7
+public class DiceTest {
8
+
9
+    @Test
10
+    public void getSum1(){
11
+        Dice dice = new Dice(2);
12
+        int expected = 6;
13
+        int actual = dice.getSum();
14
+        Assert.assertEquals(expected, actual, 6);
15
+    }
16
+
17
+    @Test
18
+    public void getSum2(){
19
+        Dice dice = new Dice(5);
20
+        int expected = 13;
21
+        int actual = dice.getSum();
22
+        Assert.assertEquals(expected, actual, 12);
23
+    }
24
+
25
+    @Test
26
+    public void getEach1(){
27
+        Dice dice = new Dice(2);
28
+        int expected = 3;
29
+        int actual = dice.getEach()[1];
30
+        Assert.assertEquals(expected, actual, 3);
31
+    }
32
+
33
+    @Test
34
+    public void getEach2(){
35
+        Dice dice = new Dice(6);
36
+        int expected = 3;
37
+        int actual = dice.getEach()[3];
38
+        Assert.assertEquals(expected, actual, 3);
39
+    }
40
+}