Skip to content

Instantly share code, notes, and snippets.

@sbastn
Created July 29, 2011 08:52
Show Gist options
  • Save sbastn/1113463 to your computer and use it in GitHub Desktop.
Save sbastn/1113463 to your computer and use it in GitHub Desktop.
AgileAlicanteJuly2011
import static org.junit.Assert.*;
import java.util.HashMap;
import org.junit.Test;
public class FizzBuzzTest {
@Test
public void devuelveUno() throws Exception {
assertEquals("1", dime(1));
}
@Test
public void devuelveDOs() throws Exception {
assertEquals("2", dime(2));
}
@Test
public void devuelvefizz() throws Exception {
assertEquals("Fizz", dime(3));
}
@Test
public void devuelveCinco() throws Exception {
assertEquals("Buzz", dime(5));
}
@Test
public void recibe6devuelveFizz() throws Exception {
assertEquals("Fizz", dime(6));
}
@Test
public void recibe10devuelveBuzz() throws Exception {
assertEquals("Buzz", dime(10));
}
@Test
public void recibe_15_devuelve_FizzBuzz() throws Exception {
assertEquals("FizzBuzz", dime(15));
}
@Test
public void recibe35devuleveFizzBuzz() throws Exception {
assertEquals("FizzBuzz", dime(35));
}
@Test
public void learningMaps() throws Exception {
HashMap<Integer, String> numeros = new HashMap<Integer, String>();
numeros.put(3, "Fizz");
numeros.put(5, "Buzz");
assertTrue(3 == numeros.keySet().iterator().next());
}
@Test
public void recibe13devuelveFizz() throws Exception {
assertEquals("Fizz", dime(13));
}
@Test
public void recibe53devuelveFizz() throws Exception {
assertEquals("FizzBuzz", dime(53));
}
@Test
public void recibe60devuelveFizzBuzz() throws Exception {
assertEquals("FizzBuzz", dime(60));
}
@Test
public void recibe51devuelveFizzBuzz() throws Exception {
assertEquals("FizzBuzz", dime(51));
}
private String dime(Integer number) {
HashMap<Integer, String> numeros = new HashMap<Integer, String>();
numeros.put(3, "Fizz");
numeros.put(5, "Buzz");
String retorno = "";
for (Integer key : numeros.keySet()) {
if (number % key == 0 || String.valueOf(number).contains(key.toString())) {
retorno += numeros.get(key);
}
}
if (retorno.isEmpty()) {
retorno = number.toString();
}
return retorno;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment