Skip to content

Instantly share code, notes, and snippets.

@datenreisender
Forked from sanitz/RomanNumbersTest.java
Created August 9, 2011 14:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save datenreisender/1134163 to your computer and use it in GitHub Desktop.
Save datenreisender/1134163 to your computer and use it in GitHub Desktop.
Result of the roman numbers kata in Java
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