Created
July 7, 2016 20:54
-
-
Save alebaffa/d27dc493d319982c7b4869173ff30a86 to your computer and use it in GitHub Desktop.
Roman Numeral 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
public class Number implements Comparable<Number> { | |
private int arabic; | |
private String roman; | |
public Number(int arabicIn, String romanIn) { | |
arabic = arabicIn; | |
roman = romanIn; | |
} | |
public int getArabic() { | |
return this.arabic; | |
} | |
public String getRoman() { | |
return this.roman; | |
} | |
@Override | |
public int compareTo(Number number) { | |
if(this.getArabic() > number.getArabic()) | |
return 1; | |
else if(this.getArabic() < number.getArabic()) | |
return -1; | |
else return 0; | |
} | |
} |
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 java.util.*; | |
public class RomanNumbers { | |
private static List<Number> dictionary = new ArrayList<>(); | |
static { | |
dictionary.add(new Number(50, "L")); | |
dictionary.add(new Number(40, "XL")); | |
dictionary.add(new Number(10, "X")); | |
dictionary.add(new Number(9, "IX")); | |
dictionary.add(new Number(5, "V")); | |
dictionary.add(new Number(4, "IV")); | |
dictionary.add(new Number(1, "I")); | |
} | |
public String convertToRoman(int arabicInput) { | |
StringBuffer result = new StringBuffer(); | |
for(Number item : dictionary) { | |
while (arabicInput >= item.getArabic()) { | |
result.append(item.getRoman()); | |
arabicInput -= item.getArabic(); | |
} | |
} | |
return result.toString(); | |
} | |
} |
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
public class RomanNumbersTest { | |
@Test | |
public void convertArabicToRomanNumbers() { | |
RomanNumbers converter = new RomanNumbers(); | |
assertThat(converter.convertToRoman(1), is(equalTo("I"))); | |
assertThat(converter.convertToRoman(2), is(equalTo("II"))); | |
assertThat(converter.convertToRoman(3), is(equalTo("III"))); | |
assertThat(converter.convertToRoman(4), is(equalTo("IV"))); | |
assertThat(converter.convertToRoman(5), is(equalTo("V"))); | |
assertThat(converter.convertToRoman(6), is(equalTo("VI"))); | |
assertThat(converter.convertToRoman(7), is(equalTo("VII"))); | |
assertThat(converter.convertToRoman(9), is(equalTo("IX"))); | |
assertThat(converter.convertToRoman(10), is(equalTo("X"))); | |
assertThat(converter.convertToRoman(11), is(equalTo("XI"))); | |
assertThat(converter.convertToRoman(40), is(equalTo("XL"))); | |
assertThat(converter.convertToRoman(50), is(equalTo("L"))); | |
assertThat(converter.convertToRoman(55), is(equalTo("LV"))); | |
assertThat(converter.convertToRoman(60), is(equalTo("LX"))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment