Created
October 23, 2010 11:20
-
-
Save ketankhairnar/642097 to your computer and use it in GitHub Desktop.
romanConverter
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 dojo1_Roman; | |
public class RomanConverter | |
{ | |
public enum Digit | |
{ | |
UNIT, DECIMAL, HUNDRED, THOUSAND | |
}; | |
private static String[] units = | |
{ "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X" }; | |
private static String[] tens = | |
{ "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC", "C" }; | |
private static String[] hundreds = | |
{ "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "DM", "M" }; | |
private static String[] thousands = | |
{ "M", "MM", "MMM", "IV", "V", "VI", "VII", "VIII", "IX", "X" };// can't display bar on numbers higher than 3K | |
public static int unitCount = 0; | |
public static int tenCount = 0; | |
public static int hundredCount = 0; | |
public static int thousandCount = 0; | |
public static void processInput(int input) throws Exception | |
{ | |
try | |
{ | |
System.out.println("input is : " + input); | |
thousandCount = input / 1000; | |
hundredCount = input / 100 - (thousandCount * 10); | |
tenCount = input / 10 - (thousandCount * 100 + hundredCount * 10); | |
int minus = thousandCount * 1000 + hundredCount * 100 + tenCount * 10; | |
unitCount = input - minus; | |
System.out.println("thousand: " + thousandCount); | |
System.out.println("hundred: " + hundredCount); | |
System.out.println("ten: " + tenCount); | |
System.out.println("unit: " + unitCount); | |
} | |
catch (Exception e) | |
{ | |
if (e instanceof NumberFormatException) | |
{ | |
System.out.println("Improper Input : Not a Number"); | |
} | |
else | |
{ | |
System.out.println("Improper Input : No argument passed"); | |
} | |
throw e; | |
} | |
} | |
public static String getDisplayString(int input) throws Exception | |
{ | |
StringBuffer sbfResult = new StringBuffer(); | |
processInput(input); | |
append(sbfResult, Digit.THOUSAND); | |
append(sbfResult, Digit.HUNDRED); | |
append(sbfResult, Digit.DECIMAL); | |
append(sbfResult, Digit.UNIT); | |
return sbfResult.toString(); | |
} | |
private static void append(StringBuffer sbfResult, Digit digit) | |
{ | |
switch (digit) | |
{ | |
case UNIT: | |
sbfResult.append(units[unitCount - 1]); | |
break; | |
case DECIMAL: | |
sbfResult.append(tens[tenCount - 1]); | |
break; | |
case HUNDRED: | |
sbfResult.append(hundreds[hundredCount - 1]); | |
break; | |
case THOUSAND: | |
sbfResult.append(thousands[thousandCount - 1]); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment