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
6fc7b59
commit a23e075
Showing
3 changed files
with
154 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,41 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
|
||
public class FizzBuzz { | ||
|
||
private List<Talker> talkers; | ||
|
||
public FizzBuzz(){ | ||
this.talkers = new ArrayList<Talker>(); | ||
} | ||
|
||
public String fizzBuzzer(int numberToFizzBuzzer){ | ||
String numberFizzBuzzed = ""; | ||
|
||
for (Talker talker : this.talkers) | ||
numberFizzBuzzed += talker.sayWord(numberToFizzBuzzer); | ||
|
||
numberFizzBuzzed += sayNumber(numberToFizzBuzzer, numberFizzBuzzed); | ||
|
||
return numberFizzBuzzed; | ||
} | ||
|
||
private String sayNumber(int numberToFizzBuzzer, String numberFizzBuzzed){ | ||
if(numberFizzBuzzed.isEmpty()) | ||
return String.valueOf(numberToFizzBuzzer); | ||
return ""; | ||
} | ||
|
||
public boolean addTalker(Talker talker){ | ||
return this.talkers.add(talker); | ||
} | ||
|
||
public Talker deleteTalker(int index){ | ||
return this.talkers.remove(index); | ||
} | ||
|
||
public List<Talker> getTalkers() { | ||
return talkers; | ||
} | ||
} |
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,33 @@ | ||
|
||
public class Talker { | ||
|
||
private int divisor; | ||
private String word; | ||
|
||
public Talker(String word, int divisor){ | ||
this.word = word; | ||
this.divisor = divisor; | ||
} | ||
|
||
public String sayWord(int numberToCheck){ | ||
boolean isDivisible = numberToCheck % this.divisor == 0; | ||
boolean hasTheDivisor = containDivisor(numberToCheck); | ||
if(isDivisible || hasTheDivisor) | ||
return this.word; | ||
return ""; | ||
} | ||
|
||
private boolean containDivisor(int numberToCheck) { | ||
String number = String.valueOf(numberToCheck); | ||
String divisor = String.valueOf(this.divisor); | ||
return number.contains(divisor); | ||
} | ||
|
||
public int getDivisor() { | ||
return divisor; | ||
} | ||
|
||
public String getWord() { | ||
return word; | ||
} | ||
} |
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,80 @@ | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
|
||
|
||
public class FizzBuzzTest { | ||
|
||
private FizzBuzz fizzBuzz; | ||
private Talker fizzTalker; | ||
private Talker buzzTalker; | ||
|
||
@Before | ||
public void setUp(){ | ||
fizzBuzz = new FizzBuzz(); | ||
fizzTalker = new Talker("Fizz", 3); | ||
buzzTalker = new Talker("Buzz", 5); | ||
fizzBuzz.addTalker(fizzTalker); | ||
fizzBuzz.addTalker(buzzTalker); | ||
} | ||
|
||
@Test | ||
public void testFizzBuzzCaseNumber() throws Exception { | ||
String expected; | ||
|
||
expected = "1"; | ||
assertEquals(expected, fizzBuzz.fizzBuzzer(1)); | ||
|
||
expected = "2"; | ||
assertEquals(expected, fizzBuzz.fizzBuzzer(2)); | ||
|
||
expected = "4"; | ||
assertEquals(expected, fizzBuzz.fizzBuzzer(4)); | ||
|
||
expected = "7"; | ||
assertEquals(expected, fizzBuzz.fizzBuzzer(7)); | ||
} | ||
|
||
@Test | ||
public void testFizzBuzzCaseFizz() throws Exception { | ||
String expected = "Fizz"; | ||
|
||
assertEquals(expected, fizzBuzz.fizzBuzzer(3)); | ||
} | ||
|
||
@Test | ||
public void testFizzBuzzCaseBuzz() throws Exception { | ||
String expected = "Buzz"; | ||
|
||
assertEquals(expected, fizzBuzz.fizzBuzzer(5)); | ||
} | ||
|
||
@Test | ||
public void testFizzBuzzCaseFizzBuzz() throws Exception { | ||
String expected = "FizzBuzz"; | ||
|
||
assertEquals(expected, fizzBuzz.fizzBuzzer(15)); | ||
} | ||
|
||
@Test | ||
public void testFizzBuzzCaseFizzContainsDivisor() throws Exception { | ||
String expected = "Fizz"; | ||
|
||
assertEquals(expected, fizzBuzz.fizzBuzzer(131111)); | ||
} | ||
|
||
@Test | ||
public void testFizzBuzzCaseBuzzContainsDivisor() throws Exception { | ||
String expected = "Buzz"; | ||
|
||
assertEquals(expected, fizzBuzz.fizzBuzzer(1115111)); | ||
} | ||
|
||
@Test | ||
public void testFizzBuzzCaseFizzBuzzContainsDivisor() throws Exception { | ||
String expected = "FizzBuzz"; | ||
|
||
assertEquals(expected, fizzBuzz.fizzBuzzer(11135111)); | ||
} | ||
} |