diff --git a/pom.xml b/pom.xml index 7219542..ee02794 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ com.zipcodewilmington Dicey-Lab 1.0-SNAPSHOT + + + junit + junit + 4.13.1 + test + + + + 11 + 11 + \ No newline at end of file diff --git a/src/main/java/Bins.java b/src/main/java/Bins.java index b9da83e..ff78e79 100644 --- a/src/main/java/Bins.java +++ b/src/main/java/Bins.java @@ -1,4 +1,26 @@ +import java.util.HashMap; +import java.util.Map; public class Bins { + private Integer lower; + private Integer upper; + private Map binsMap = new HashMap<>(); + + public Bins(Integer lower, Integer upper) { + this.lower = lower; + this.upper = upper; + + for (int i = lower; i <= upper; i++) { + binsMap.put(i, 0); + } + } + + public Integer getBin(int bin) { + return binsMap.get(bin); + } + + public void incrementBin(int bin) { + binsMap.put(bin, getBin(bin)+1); + } } diff --git a/src/main/java/Dice.java b/src/main/java/Dice.java index 2283c96..07e1e4c 100644 --- a/src/main/java/Dice.java +++ b/src/main/java/Dice.java @@ -1,4 +1,23 @@ +import java.util.Random; + public class Dice { + private Integer numberOfDie; + + public Dice(int numberOfDie) { + this.numberOfDie = numberOfDie; + } + + public Integer tossAndSum() { + Random random = new Random(); + Integer sum = 0; + for (int i = 0; i < numberOfDie; i++) { + sum += random.nextInt(6) + 1; + } + return sum; + } + public Integer getNumOfDie() { + return this.numberOfDie; + } } diff --git a/src/main/java/MainApplication.java b/src/main/java/MainApplication.java new file mode 100644 index 0000000..2d541b4 --- /dev/null +++ b/src/main/java/MainApplication.java @@ -0,0 +1,8 @@ +public class MainApplication { + public static void main(String[] args) { + Simulation sim = new Simulation(2, 1000000); + + sim.runSimulation(); + sim.printResults(); + } +} diff --git a/src/main/java/Simulation.java b/src/main/java/Simulation.java index 73d86e8..48629c4 100644 --- a/src/main/java/Simulation.java +++ b/src/main/java/Simulation.java @@ -1,5 +1,61 @@ public class Simulation { + private Integer numberOfDie; + private Integer numberOfTosses; + private Bins bins; + public Simulation(Integer numberOfDie, Integer numberOfTosses) { + this.numberOfDie = numberOfDie; + this.numberOfTosses = numberOfTosses; + } + + public Integer getNumOfDie() { + return this.numberOfDie; + } + + public Integer getNumOfTosses() { + return this.numberOfTosses; + } + + public Bins runSimulation() { + Dice dice = new Dice(numberOfDie); + bins = new Bins(numberOfDie, numberOfDie*6); + Integer result; + + for (int i = 0; i < numberOfTosses; i++) { + result = dice.tossAndSum(); + bins.incrementBin(result); + } + return bins; + } + + public void printResults() { + + System.out.println(new StringBuilder() + .append("\n***") + .append("\nSimulation of " + numberOfDie + " dice tossed " + numberOfTosses + " times.") + .append("\n***") + + printBins(bins)); + + } + + + private String printBins(Bins bins) { + String binLine = ""; + Double binPercent; + Integer stars = 0; + + for(int i = numberOfDie; i <= (numberOfDie*6); i++) { + binPercent = ((double) bins.getBin(i)) / numberOfTosses; + binLine += String.format("\n%2d : %8d : %.2f ", i, bins.getBin(i), binPercent); + + stars = (100 * bins.getBin(i)) / numberOfTosses; + + for (int j = 0; j < stars; j++) { + binLine += "*"; + } + } + return binLine; + } } diff --git a/src/test/java/BinsTest.java b/src/test/java/BinsTest.java new file mode 100644 index 0000000..8dcef99 --- /dev/null +++ b/src/test/java/BinsTest.java @@ -0,0 +1,34 @@ +import org.junit.Assert; +import org.junit.Test; + +public class BinsTest { + + @Test + public void incrementBinTest() { + //Given + Bins bins = new Bins(2, 12); + Integer expected = 1; + + //When + bins.incrementBin(10); + Integer actual = bins.getBin(10); + + //Then + Assert.assertEquals(expected, actual); + } + + @Test + public void binsConstructorTest() { + //Given + Integer lower = 2; + Integer upper = 12; + Integer expected = 0; + + //When + Bins bins = new Bins(lower, upper); + Integer actual = bins.getBin(2); + + //Then + Assert.assertEquals(expected, actual); + } +} diff --git a/src/test/java/DiceTest.java b/src/test/java/DiceTest.java new file mode 100644 index 0000000..6a3eec6 --- /dev/null +++ b/src/test/java/DiceTest.java @@ -0,0 +1,42 @@ +import org.junit.Assert; +import org.junit.Test; + +public class DiceTest{ + + @Test + public void diceConstructorTest() { + //Given + Integer expectedNumOfDie = 8; + + //When + Dice dice = new Dice(expectedNumOfDie); + Integer actual = dice.getNumOfDie(); + + //Then + Assert.assertEquals(expectedNumOfDie, actual); + } + + @Test + public void tossAndSumTwoDiceTest() { + //Given + Dice dice = new Dice(2); + + //When + Integer toss = dice.tossAndSum(); + + //Then + Assert.assertTrue((toss > 1) && (toss < 13)); + } + + @Test + public void tossAndSumFiveDiceTest() { + //Given + Dice dice = new Dice(5); + + //When + Integer toss = dice.tossAndSum(); + + //Then + Assert.assertTrue((toss > 4) && (toss < 31)); + } +} diff --git a/src/test/java/SimulationTest.java b/src/test/java/SimulationTest.java new file mode 100644 index 0000000..ec1769f --- /dev/null +++ b/src/test/java/SimulationTest.java @@ -0,0 +1,22 @@ +import org.junit.Assert; +import org.junit.Test; + +public class SimulationTest { + + @Test + public void simulationConstructorTest() { + //Given + Integer numberOfDie = 2; + Integer numberOfTosses = 10000; + + //When + Simulation sim = new Simulation(numberOfDie, numberOfTosses); + Integer actualNumOfDie = sim.getNumOfDie(); + Integer actualNumOfTosses = sim.getNumOfTosses(); + + //Then + Assert.assertEquals(numberOfDie, actualNumOfDie); + Assert.assertEquals(numberOfTosses, actualNumOfTosses); + } + +}