Skip to content

Instantly share code, notes, and snippets.

@renatoathaydes
Created April 17, 2013 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renatoathaydes/5402392 to your computer and use it in GitHub Desktop.
Save renatoathaydes/5402392 to your computer and use it in GitHub Desktop.
Converter of Arabic numbers to Roman numbers
import java.util.HashMap;
import java.util.Map;
public class RomanNumeral
{
private static final Map<Integer, String[]> romanCharsByDecimalPlace = new HashMap<Integer, String[]>()
{
{
put( 0, new String[]
{
"I", "V", "X"
}
);
put( 1, new String[]
{
"X", "L", "C"
}
);
put( 2, new String[]
{
"C", "D", "M"
}
);
}
};
public static String convert( int number )
{
String nStr = Integer.toString( number );
char[] parts = nStr.toCharArray();
String result = "";
for( int i = parts.length - 1; i >= 0; i-- )
{
result += convertDigit( Character.getNumericValue( parts[parts.length - i - 1] ), i );
}
return result;
}
public static String convertDigit( int digit, int decimalPlace )
{
if( digit <= 0 )
{
return "";
}
if( digit <= 3 )
{
return times( digit, romanCharsByDecimalPlace.get( decimalPlace )[0] );
}
else if( digit == 4 )
{
return romanCharsByDecimalPlace.get( decimalPlace )[0] +
romanCharsByDecimalPlace.get( decimalPlace )[1];
}
else if( digit <= 8 )
{
return romanCharsByDecimalPlace.get( decimalPlace )[1] +
times( digit - 5, romanCharsByDecimalPlace.get( decimalPlace )[0] );
}
else
{
return romanCharsByDecimalPlace.get( decimalPlace )[0] +
romanCharsByDecimalPlace.get( decimalPlace )[2];
}
}
private static String times( int factor, String text )
{
String res = "";
for( int i = 0; i < factor; i++ )
{
res += text;
}
return res;
}
}
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.*;
import java.util.Arrays;
import java.util.Collection;
import static junit.framework.Assert.assertEquals;
@RunWith( Parameterized.class )
public class RomanNumeralTest
{
private int number;
private String romanNumber;
public RomanNumeralTest( int number, String romanNumber )
{
this.number = number;
this.romanNumber = romanNumber;
}
@Parameters
public static Collection<Object[]> bunchOfPrimes()
{
return Arrays.asList( new Object[][]
{
{ 1, "I" },
{ 2, "II" },
{ 3, "III" },
{ 4, "IV" },
{ 5, "V" },
{ 6, "VI" },
{ 7, "VII" },
{ 8, "VIII" },
{ 9, "IX" },
{ 10, "X" },
{ 11, "XI" },
{ 13, "XIII" },
{ 14, "XIV" },
{ 15, "XV" },
{ 18, "XVIII" },
{ 20, "XX" },
{ 30, "XXX" },
{ 90, "XC" },
{ 100, "C" },
{ 101, "CI" },
} );
}
@Test
public void testConvert()
{
assertEquals( romanNumber, RomanNumeral.convert( number ) );
}
/*
@Test
public void oneShouldBeI()
{
assertEquals( "I", RomanNumeral.convert( 1 ) );
}
@Test
public void twoShouldBeII()
{
assertEquals( "II", RomanNumeral.convert( 2 ) );
}
@Test
public void fiveShouldBeV()
{
assertEquals( "V", RomanNumeral.convert( 5 ) );
}
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment