forked from 12meses12katas/Marzo-FizzBuzz
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
495cf3e
commit dc14a35
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package es.rromero.kata3; | ||
|
||
public class FizzBuzz { | ||
|
||
private static final String FIZZ = "Fizz"; | ||
private static final String BUZZ = "Buzz"; | ||
|
||
public String sayFizzBuzz(int num) { | ||
StringBuilder sb = new StringBuilder(); | ||
String sNum = Integer.toString(num); | ||
if(num % 3 == 0 || sNum.contains("3")) { | ||
sb.append(FIZZ); | ||
} | ||
if(num % 5 == 0 || sNum.contains("5")) { | ||
sb.append(BUZZ); | ||
} | ||
if(sb.length() == 0) { | ||
sb.append(sNum); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
public String printFrom1to100() { | ||
StringBuilder sb = new StringBuilder(); | ||
for(int i = 1; i<=100; i++) { | ||
sb.append(sayFizzBuzz(i)).append("\n"); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
public static void main(String[] args) { | ||
FizzBuzz fb = new FizzBuzz(); | ||
System.out.println(fb.printFrom1to100()); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package es.rromero.kata3; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import org.junit.Test; | ||
|
||
/** | ||
* @author rromero | ||
* | ||
*/ | ||
public class TestFizzBuzz { | ||
|
||
@Test | ||
public void testSayNumber() { | ||
FizzBuzz fb = new FizzBuzz(); | ||
assertEquals("1",fb.sayFizzBuzz(1)); | ||
assertEquals("2",fb.sayFizzBuzz(2)); | ||
assertEquals("4",fb.sayFizzBuzz(4)); | ||
assertEquals("41",fb.sayFizzBuzz(41)); | ||
} | ||
|
||
@Test | ||
public void testSayFizz() { | ||
FizzBuzz fb = new FizzBuzz(); | ||
assertEquals("Fizz",fb.sayFizzBuzz(3)); | ||
assertEquals("Fizz",fb.sayFizzBuzz(9)); | ||
assertEquals("Fizz",fb.sayFizzBuzz(93)); | ||
assertEquals("Fizz",fb.sayFizzBuzz(31)); | ||
} | ||
|
||
@Test | ||
public void testSayBuzz() { | ||
FizzBuzz fb = new FizzBuzz(); | ||
assertEquals("Buzz",fb.sayFizzBuzz(5)); | ||
assertEquals("Buzz",fb.sayFizzBuzz(10)); | ||
assertEquals("Buzz",fb.sayFizzBuzz(59)); | ||
} | ||
|
||
@Test | ||
public void testSayFizzBuzz() { | ||
FizzBuzz fb = new FizzBuzz(); | ||
assertEquals("FizzBuzz",fb.sayFizzBuzz(35)); | ||
assertEquals("FizzBuzz",fb.sayFizzBuzz(15)); | ||
assertEquals("FizzBuzz",fb.sayFizzBuzz(55333555)); | ||
} | ||
} |