Last active
August 29, 2015 14:18
-
-
Save gicappa/a3995fb39d6ccdd09daf to your computer and use it in GitHub Desktop.
FizzBuzz with java 8
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.Arrays; | |
public class FizzBuzz { | |
public static String compute(Integer number) { | |
return Arrays.stream(Multipliers.values()) | |
.filter(m -> (number % m.multiple == 0)) | |
.map(Enum::name) | |
.reduce((s, r) -> s + r) | |
.orElse(number.toString()); | |
} | |
enum Multipliers { | |
fizz(3), buzz(5); | |
public final Integer multiple; | |
Multipliers(Integer multiple) { this.multiple = multiple; } | |
} | |
} |
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 org.junit.Test; | |
import static org.hamcrest.CoreMatchers.is; | |
import static org.junit.Assert.assertThat; | |
public class FizzBuzzSpec { | |
@Test | |
public void when_passing_a_number_it_returns_a_string() { | |
assertThat(FizzBuzz.compute(2), is("2")); | |
} | |
@Test | |
public void when_passing_a_multiple_of_three_it_returns_fizz() { | |
assertThat(FizzBuzz.compute(3), is("fizz")); | |
} | |
@Test | |
public void when_passing_a_multiple_of_five_it_returns_buzz() { | |
assertThat(FizzBuzz.compute(5), is("buzz")); | |
} | |
@Test | |
public void when_passing_a_multiple_of_fifteen_it_returns_fizzbuzz() { | |
assertThat(FizzBuzz.compute(15), is("fizzbuzz")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment