Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Kata In Java

  • Loading branch information...
commit 61cbc8c6b1744c122b0ecd1b68ee056909cc0f3b 1 parent 2c6cac8
@manute manute authored
View
43 manute/src/com/kata/marzo/FizzBuzz.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);
+ }
+
+}
View
21 manute/test/com/kata/marzo/FizzBuzzTest.java
@@ -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));
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.