-
-
Save datenreisender/1134163 to your computer and use it in GitHub Desktop.
Result of the roman numbers kata in Java
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
import static org.junit.Assert.*; | |
public class RomanNumbersTest { | |
enum Roman { | |
C(100), XC(90), L(50), XL(40), X(10), IX(9), V(5), IV(4), I(1); | |
final int decimal; | |
Roman(int decimal) { this.decimal = decimal; } | |
static String of(int n) { | |
String result = ""; | |
for (Roman num : values()) | |
while (n >= num.decimal) { | |
result += num.name(); | |
n -= num.decimal; | |
} | |
return result; | |
} | |
} | |
@org.junit.Test | |
public void testSimpleNumbers() { | |
assertEquals("I", Roman.of(1)); | |
assertEquals("II", Roman.of(2)); | |
assertEquals("III", Roman.of(3)); | |
assertEquals("IV", Roman.of(4)); | |
assertEquals("V", Roman.of(5)); | |
assertEquals("VI", Roman.of(6)); | |
assertEquals("VII", Roman.of(7)); | |
assertEquals("XXIII", Roman.of(23)); | |
assertEquals("LV", Roman.of(55)); | |
assertEquals("XLII", Roman.of(42)); | |
assertEquals("XCIX", Roman.of(99)); | |
assertEquals("CXI", Roman.of(111)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment