Permalink
Browse files

Kata In Java

  • Loading branch information...
1 parent 2c6cac8 commit 61cbc8c6b1744c122b0ecd1b68ee056909cc0f3b Manuel Alonso committed Dec 3, 2011
Showing with 64 additions and 0 deletions.
  1. +43 −0 manute/src/com/kata/marzo/FizzBuzz.java
  2. +21 −0 manute/test/com/kata/marzo/FizzBuzzTest.java
@@ -0,0 +1,43 @@
+package com.kata.marzo;
+
+public class FizzBuzz {
+
+ private static final String NEW_LINE = "\n";
+ private static final String BUZZ = "Buzz";
+ private static final String FIZZ = "Fizz";
+
+ public String print() {
+ StringBuffer result = new StringBuffer();
+ for (int number = 1; number <= 100; number++) {
+ result.append(checkNumber(number));
+ result.append(NEW_LINE);
+ }
+ return result.toString();
+ }
+
+ private String checkNumber(int number) {
+
+ if (isFizzBuzz(number)) {
+ return FIZZ + BUZZ;
+ }
+
+ if (isDivisibleBy(number,3)) {
+ return FIZZ;
+ }
+ if (isDivisibleBy(number,5)) {
+ return BUZZ;
+ }
+
+ return String.valueOf(number);
+ }
+
+
+ private boolean isDivisibleBy(int number, int divisible) {
+ return number % divisible == 0;
+ }
+
+ private boolean isFizzBuzz(int number) {
+ return isDivisibleBy(number,3) && isDivisibleBy(number,5);
+ }
+
+}
@@ -0,0 +1,21 @@
+package com.kata.marzo;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+import com.kata.marzo.FizzBuzz;
+
+public class FizzBuzzTest {
+
+ private String print() {
+ FizzBuzz fizzBuzz = new FizzBuzz();
+ return fizzBuzz.print();
+ }
+
+ @Test
+ public void testFizzBuzzOn15FirstNumbers() {
+
+ assertEquals("1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\nFizzBuzz", print().substring(0,57));
+ }
+}

0 comments on commit 61cbc8c

Please sign in to comment.