Created
February 11, 2013 12:52
-
-
Save fsarradin/4754263 to your computer and use it in GitHub Desktop.
Digit kata (format numbers for digital display). Java 7
Dependencies: Guava 13.0.1, junit 4.11, fest-assert-core 2.0M8
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
package test.kata_digit; | |
import java.util.ArrayList; | |
import java.util.List; | |
import com.google.common.base.Joiner; | |
public class Digits { | |
private static final int HEIGHT = 5; | |
private static final Joiner LINE_JOINER = Joiner.on("\n"); | |
private static final Joiner COL_JOINER = Joiner.on(" "); | |
private static final String[] ZERO = { | |
" - ", | |
"| |", | |
"| |", | |
"| |", | |
" - " | |
}; | |
private static final String[] ONE = { | |
" |", | |
" |", | |
" |", | |
" |", | |
" |" | |
}; | |
private static final String[] TWO = { | |
"-- ", | |
" |", | |
" - ", | |
"| ", | |
" --" | |
}; | |
private static final String[] THREE = { | |
"-- ", | |
" |", | |
" -|", | |
" |", | |
"-- " | |
}; | |
private static final String[] FOUR = { | |
"| |", | |
"| |", | |
" -|", | |
" |", | |
" |" | |
}; | |
private static final String[] FIVE = { | |
" --", | |
"| ", | |
" - ", | |
" |", | |
"-- " | |
}; | |
private static final String[] SIX = { | |
"| ", | |
"| ", | |
"|- ", | |
"| |", | |
" - " | |
}; | |
private static final String[] SEVEN = { | |
"---", | |
" |", | |
" |", | |
" |", | |
" |" | |
}; | |
private static final String[] EIGHT = { | |
" - ", | |
"| |", | |
"|-|", | |
"| |", | |
" - " | |
}; | |
private static final String[] NINE = { | |
" - ", | |
"| |", | |
" -|", | |
" |", | |
" - " | |
}; | |
private static final String[][] DIGITS = { | |
ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE | |
}; | |
public static String toDigit(int value) { | |
String stringValue = String.valueOf(value); | |
List<String> result = new ArrayList<>(); | |
for (int row = 0; row < HEIGHT; row++) { | |
List<String> digitRow = new ArrayList<>(); | |
for (byte c : stringValue.getBytes()) { | |
int digit = c - '0'; | |
digitRow.add(DIGITS[digit][row]); | |
} | |
result.add(COL_JOINER.join(digitRow)); | |
} | |
return LINE_JOINER.join(result); | |
} | |
} |
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
package test.kata_digit; | |
import static org.fest.assertions.api.Assertions.assertThat; | |
import org.junit.Test; | |
public class DigitTest { | |
@Test | |
public void should_get_digital_one_when_1() throws Exception { | |
String digits = Digits.toDigit(1); | |
assertThat(digits).isEqualTo( | |
" |\n" + | |
" |\n" + | |
" |\n" + | |
" |\n" + | |
" |" | |
); | |
} | |
@Test | |
public void should_get_digital_one_two_when_12() throws Exception { | |
String digits = Digits.toDigit(12); | |
assertThat(digits).isEqualTo( | |
" | -- \n" + | |
" | |\n" + | |
" | - \n" + | |
" | | \n" + | |
" | --" | |
); | |
} | |
@Test | |
public void should_get_digital_five_one_two_when_512() throws Exception { | |
String digits = Digits.toDigit(512); | |
assertThat(digits).isEqualTo( | |
" -- | -- \n" + | |
"| | |\n" + | |
" - | - \n" + | |
" | | | \n" + | |
"-- | --" | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment