Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
}
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());
}
}
@drm317

This comment has been minimized.

Show comment
Hide comment
Owner

drm317 commented Apr 19, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment