diff --git a/pom.xml b/pom.xml index 7219542..1f14117 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,26 @@ com.zipcodewilmington Dicey-Lab 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 7 + 7 + + + + + + + junit + junit + 4.12 + test + + \ No newline at end of file diff --git a/src/main/java/Bins.java b/src/main/java/Bins.java index b9da83e..e04ab11 100644 --- a/src/main/java/Bins.java +++ b/src/main/java/Bins.java @@ -1,4 +1,28 @@ +import java.util.HashMap; +import java.util.Map; public class Bins { +//map + Map mapOfBins = new HashMap<>(); + private Integer minimumNumber; + private Integer maximumNumber; + + + public Bins(int minimumNumber, int maximumNumber) { + this.minimumNumber = minimumNumber; + this.maximumNumber = maximumNumber; + for (int i = minimumNumber; i <= maximumNumber; i++) { + mapOfBins.put(i, 0); + } + } + public Integer getBin(int binNumber) { + return mapOfBins.get(binNumber); + } + public void incrementBin(Integer binNumber) { + if (binNumber >= this.minimumNumber && binNumber <= this.maximumNumber) { + Integer currentBinResult = this.getBin(binNumber); + this.mapOfBins.put(binNumber, currentBinResult + 1);// + } + } } diff --git a/src/main/java/Dice.java b/src/main/java/Dice.java index 2283c96..54aefb1 100644 --- a/src/main/java/Dice.java +++ b/src/main/java/Dice.java @@ -1,4 +1,24 @@ + + public class Dice { + private Integer numberOnDice; + + public Dice(Integer numberOnDice) { + this.numberOnDice = numberOnDice; + } + + public int tossAndSum(){ + int sum = 0; + for (int i = 0; i < numberOnDice; i++) { + sum += (int) (Math.random() * 6) + 1; + } + return sum; + + } + + public Integer getNumberOnDice() { + return numberOnDice; + } } diff --git a/src/main/java/Simulation.java b/src/main/java/Simulation.java index 73d86e8..60be154 100644 --- a/src/main/java/Simulation.java +++ b/src/main/java/Simulation.java @@ -1,4 +1,49 @@ public class Simulation { + private Integer numberOfDice; + private Integer numberOfTosses; + + + + + public Simulation(Integer numberOfDice, Integer numberOfTosses) { + this.numberOfDice = numberOfDice; + this.numberOfTosses = numberOfTosses; + } + + public static void main(String[] args) { + Simulation simulation = new Simulation(2, 10000); + simulation.printResults(simulation.runSimulation()); + } + + private Bins runSimulation() { + Dice dice = new Dice(numberOfDice); + Integer maxValue = numberOfDice * 6; + Bins bin = new Bins(numberOfDice, maxValue); + for (int i = 0; i < numberOfTosses; i++) { + bin.incrementBin(dice.tossAndSum());//increments to random numbers between start and end + } + return bin; + } + private void printResults(Bins bin) { + Integer maxValue = numberOfDice * 6; + //loop through results array and divide by the total number of tosses + for (int i = numberOfDice; i <= maxValue ; i++) { + System.out.print(String.format("%2d : %4d : %1.2f ", i, bin.getBin(i), binPercent(bin.getBin(i)))); + printStars(binPercent(bin.getBin(i))); + System.out.println(); + } + } + public void printStars(Double value) { + int numberOfStars = (int) (value * 100); + for (int i = 1; i <= numberOfStars; i++) { + System.out.print("*"); + } + } + + public double binPercent(Integer value) { + return Double.valueOf(value) / numberOfTosses; + } + diff --git a/src/test/java/BinsTest.java b/src/test/java/BinsTest.java new file mode 100644 index 0000000..a4f75f7 --- /dev/null +++ b/src/test/java/BinsTest.java @@ -0,0 +1,25 @@ +import org.junit.Assert; +import org.junit.Test; + + +public class BinsTest { + @Test + public void getBin() { + //given + Bins bins = new Bins(2, 12); + //When + int actual = bins.getBin(4); + + //Then + Assert.assertEquals(0, actual); + } + @Test + public void incrementTest(){ + Bins bin = new Bins(2,12); + Integer expected = 2; + bin.incrementBin(10); + bin.incrementBin(10); + + Assert.assertEquals(expected, bin.getBin(10)); + } +} diff --git a/src/test/java/DiceTest.java b/src/test/java/DiceTest.java new file mode 100644 index 0000000..77aaf63 --- /dev/null +++ b/src/test/java/DiceTest.java @@ -0,0 +1,19 @@ +import junit.framework.TestCase; +import org.junit.Assert; + +public class DiceTest extends TestCase { + + public void testTossAndSum() { + Dice dice = new Dice(1); + int result = dice.tossAndSum(); + //Assert.assertEquals(2,result); + Assert.assertTrue(result>0 && result <7); + } + + public void testGetNumberOnDice() { + Dice dice = new Dice(2); + Integer expected = dice.getNumberOnDice(); + Integer actual = 2; + Assert.assertEquals(expected, actual); + } +} \ No newline at end of file