diff --git a/pom.xml b/pom.xml index 7219542..bbf2229 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,32 @@ com.zipcodewilmington Dicey-Lab 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 7 + 7 + + + + + + + junit + junit + 4.13.1 + test + + + junit + junit + 4.13.1 + test + + \ No newline at end of file diff --git a/src/main/java/Bins.java b/src/main/java/Bins.java index b9da83e..456eb92 100644 --- a/src/main/java/Bins.java +++ b/src/main/java/Bins.java @@ -1,4 +1,34 @@ +import java.beans.Transient; +import java.util.Map; +import java.util.TreeMap; public class Bins { + private Integer binLow; + private Integer binHigh; + private Map binMap = new TreeMap<>(); + + public Bins(Integer binLow, Integer binHigh) { + this.binLow = binLow; + this.binHigh = binHigh; + + for (int i = binLow; i <= binHigh; i++) { + binMap.put(i, 0); + } + } + + public Integer getBin(Integer binNum) { + return binMap.get(binNum); + } + + public Map getBinMap() { + return binMap; + } + + public void incrementBin(Integer binNum) { + Integer qty = binMap.containsKey(binNum) ? binMap.get(binNum) : 0; + qty++; + binMap.put(binNum, qty); + } + } diff --git a/src/main/java/Dice.java b/src/main/java/Dice.java index 2283c96..efdb0ea 100644 --- a/src/main/java/Dice.java +++ b/src/main/java/Dice.java @@ -1,4 +1,26 @@ +import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; + + public class Dice { + private Integer numberOfDie; + + + public Dice(int numberOfDie) { + this.numberOfDie = numberOfDie; + } + public Integer tossAndSum(){ + int random; + Integer sum = 0; + for (int i = 0; i < numberOfDie; i++) { + random = ThreadLocalRandom.current().nextInt(1,7); + sum += random; + } + return sum; + } + public Integer getNumberOfDie() { + return numberOfDie; + } } diff --git a/src/main/java/Simulation.java b/src/main/java/Simulation.java index 73d86e8..6f3ef8a 100644 --- a/src/main/java/Simulation.java +++ b/src/main/java/Simulation.java @@ -1,5 +1,55 @@ +import java.util.Map; + public class Simulation { + private Integer numberOfDies; + private Integer numberOfTosses; + private Bins bins; + + public Simulation(Integer numberOfDies, Integer numberOfTosses) { + this.numberOfDies = numberOfDies; + this.numberOfTosses = numberOfTosses; + } + + public Bins runSimulation() { + Dice dice = new Dice(numberOfDies); + bins = new Bins(numberOfDies, numberOfDies * 6); + Integer total; + + for (int i = 0; i < numberOfTosses; i++) { + total = dice.tossAndSum(); + bins.incrementBin(total); + } + return bins; + } + + public void printResults() { + System.out.println("***"); + System.out.println("Simulation of " + numberOfDies + " dice tossed for " + numberOfTosses + " times."); + System.out.println("***\n"); + for (Map.Entry value : bins.getBinMap().entrySet()) { + Integer val = (Integer) value.getValue(); + Double percent = Double.valueOf(val) / numberOfTosses; + + String stringFormat = String.format("%2d : %8d : %.2f", value.getKey(), value.getValue(), percent); + System.out.println(stringFormat + " " + printStars(val)); + + } + } + public String printStars(Integer number) { + Integer dividedByTenThousand = number / 10000; + String stars = ""; + for (int i = 0; i < dividedByTenThousand; i++) { + stars += "*"; + } + return stars; + } + public Integer getNumberOfDies() { + return numberOfDies; + } + public Integer getNumberOfTosses() { + return numberOfTosses; + } } diff --git a/src/main/java/main.java b/src/main/java/main.java new file mode 100644 index 0000000..b0533e7 --- /dev/null +++ b/src/main/java/main.java @@ -0,0 +1,8 @@ +public class main { + public static void main(String[] args) { + Simulation simulation = new Simulation(2, 1_000_000); + simulation.runSimulation(); + simulation.printResults(); + + } +} diff --git a/src/test/java/SimulationTest.java b/src/test/java/SimulationTest.java new file mode 100644 index 0000000..75e95f6 --- /dev/null +++ b/src/test/java/SimulationTest.java @@ -0,0 +1,20 @@ +import org.junit.Assert; +import org.junit.Test; + +public class SimulationTest { + @Test + public void constructorTest(){ + //given + Simulation simulation = new Simulation(2, 1000000); + Integer expectedNumberOfDies = 2; + Integer expectedNumberOfTosses = 1000000; + //when + Integer actualNumberOfDies = simulation.getNumberOfDies(); + Integer actualNumberOfTosses = simulation.getNumberOfTosses(); + //then + Assert.assertEquals(expectedNumberOfDies, actualNumberOfDies); + Assert.assertEquals(expectedNumberOfTosses, actualNumberOfTosses); + } + + +} diff --git a/src/test/java/TestBins.java b/src/test/java/TestBins.java new file mode 100644 index 0000000..65e1357 --- /dev/null +++ b/src/test/java/TestBins.java @@ -0,0 +1,32 @@ +import org.junit.Assert; +import org.junit.Test; + +public class TestBins { + + @Test + public void constructorTest(){ + //given + Bins bins = new Bins(2, 12); + Integer expectedSize= 11; + //when + Integer actualSize = bins.getBinMap().size(); + //then + Assert.assertEquals(expectedSize, actualSize); + } + + @Test + public void incrementBinTest(){ + //given + Bins bins = new Bins(2, 12); + Integer expected = 1; + //when + bins.incrementBin(2); + Integer actual = bins.getBin(2); + //then + Assert.assertEquals(expected, actual); + + } + + + +} diff --git a/src/test/java/TestDice.java b/src/test/java/TestDice.java new file mode 100644 index 0000000..a686d78 --- /dev/null +++ b/src/test/java/TestDice.java @@ -0,0 +1,28 @@ +import org.junit.Assert; +import org.junit.Test; + +public class TestDice { + + + @Test + public void constructorTest(){ + //given + Dice dice = new Dice(5); + Integer expected = 5; + + //when + Integer actual = dice.getNumberOfDie(); + //then + Assert.assertEquals(expected, actual); + } + + @Test + public void tossAndSumTest(){ + //given + Dice dice = new Dice(2); + //when + Integer actual = dice.tossAndSum(); + //then + Assert.assertTrue(actual <= 12); // max sum of toss is 12 bec 2 dice * 6 + } +}