Last active
May 1, 2016 10:49
-
-
Save drm317/9a8b1a0f4d9cf359970dcadc4a387a98 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
public class RouletteWheel { | |
private boolean bouncing = false; | |
private int ballNumber = 0; | |
private long elapsedSpinTime = 0; | |
private long spinStartTime = 0; | |
public boolean isBouncing() { | |
return bouncing; | |
} | |
public int getBallNumber() { | |
return ballNumber; | |
} | |
public void spin(long spinTime) { | |
this.spinStartTime = System.currentTimeMillis(); | |
do { | |
bounce(); | |
} while (elapsedSpinTime < spinTime); | |
this.bouncing = false; | |
} | |
private void bounce() { | |
this.bouncing = true; | |
generateRandomBallNumber(); | |
this.elapsedSpinTime = System.currentTimeMillis() - spinStartTime; | |
} | |
private void generateRandomBallNumber() { | |
this.ballNumber = new Random().nextInt(37); | |
} | |
public long getElapsedSpinTime() { | |
return elapsedSpinTime; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertFalse; | |
import static org.junit.Assert.assertTrue; | |
import static org.junit.Assert.fail; | |
import java.util.HashSet; | |
import java.util.Set; | |
import org.junit.Test; | |
public class RouletteWheelTest { | |
@Test | |
public void rouletteWheelBallInitiallyNotBouncing() { | |
RouletteWheel rouletteWheel = new RouletteWheel(); | |
assertFalse(rouletteWheel.isBouncing()); | |
} | |
@Test | |
public void rouletteBallStartsAtZero() { | |
RouletteWheel rouletteWheel = new RouletteWheel(); | |
assertEquals(0, rouletteWheel.getBallNumber()); | |
} | |
@Test | |
public void rouletteWheelSpinsForTwentySeconds() { | |
RouletteWheel rouletteWheel = new RouletteWheel(); | |
rouletteWheel.spin(20000); | |
assertEquals(20000, rouletteWheel.getElapsedSpinTime()); | |
} | |
@Test | |
public void rouletteWheelBallLandsBetweenZeroAndThirtySix() { | |
RouletteWheel rouletteWheel = new RouletteWheel(); | |
Set<Integer> ballNumberResults = new HashSet<Integer>(); | |
for (int i = 0; i < 1000; i++) { | |
rouletteWheel.spin(5); | |
int ballNumber = rouletteWheel.getBallNumber(); | |
if (ballNumber < 0 || ballNumber > 36) { | |
fail("Ball number out of range"); | |
} | |
ballNumberResults.add(Integer.valueOf(ballNumber)); | |
} | |
assertTrue(ballNumberResults.size() == 37); | |
} | |
@Test | |
public void rouletteWheelBallStopsBouncing() { | |
RouletteWheel rouletteWheel = new RouletteWheel(); | |
rouletteWheel.spin(5); | |
assertFalse(rouletteWheel.isBouncing()); | |
} | |
@Test | |
public void rouletteWheelStopsWhenElapsedTimeIsGreaterThanSpinTime() { | |
RouletteWheel rouletteWheel = new RouletteWheel(); | |
rouletteWheel.spin(-5); | |
assertFalse(rouletteWheel.isBouncing()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Kata suggested by @jjeffries1 https://docs.google.com/document/d/18mBn4R_DsuHPSuZHCqTgXrhXfyPJW-Gd46EJS_mWy2k/edit