Skip to content

Commit 801bbdf

Browse files
committed
add fizzbuzz from 1 to 100 feature
1 parent abc8cd5 commit 801bbdf

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/main/java/edu/thoughtworks/training/FizzBuzz.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package edu.thoughtworks.training;
22

33
public class FizzBuzz {
4+
private static final String END_OF_LINE = "\n";
5+
46
public String represent(int number) {
5-
String representation = "";
6-
if (isDivisibleBy3(number)) representation += "Fizz";
7-
if (isDivisibleBy5(number)) representation += "Buzz";
8-
if (!isDivisibleBy3(number) && !isDivisibleBy5(number)) representation = String.valueOf(number);
9-
return representation;
7+
StringBuilder representation = new StringBuilder();
8+
if (isDivisibleBy3(number)) representation.append("Fizz");
9+
if (isDivisibleBy5(number)) representation.append("Buzz");
10+
if (!isDivisibleBy3(number) && !isDivisibleBy5(number)) representation.append(number);
11+
return representation.toString();
1012
}
1113

1214
private boolean isDivisibleBy5(int number) {
@@ -22,6 +24,9 @@ private boolean isDivisibleBy(int number, int i) {
2224
}
2325

2426
public String fizzBuzz() {
25-
return null;
27+
StringBuilder fizzBuzz = new StringBuilder();
28+
for (int i = 1; i <= 100; ++i) fizzBuzz.append(represent(i)).append(END_OF_LINE);
29+
return fizzBuzz.toString();
2630
}
31+
2732
}

src/test/java/edu/thoughtworks/training/FizzBuzzShould.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public class FizzBuzzShould {
107107
"97\n" +
108108
"98\n" +
109109
"Fizz\n" +
110-
"Buzz";
110+
"Buzz\n";
111111

112112
@Test
113113
public void returnFizzWhenNumberIsDivisibleBy3() {

0 commit comments

Comments
 (0)