Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kata marzo FizzBuzz en java #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions pereta/src/FizzBuzz.java
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;
}
}
33 changes: 33 additions & 0 deletions pereta/src/Talker.java
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;
}
}
80 changes: 80 additions & 0 deletions pereta/test/FizzBuzzTest.java
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));
}
}