Guest User

Roman Numeral Kata

a guest
Oct 11th, 2011
1,097
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.90 KB | None | 0 0
  1. import java.util.Map;
  2. import java.util.SortedMap;
  3. import java.util.TreeMap;
  4.  
  5. /**
  6.  * @author deliveryninja
  7.  *
  8.  * Given a numeral from 1 to 3000 this will return the Roman
  9.  * numberal equivalent.
  10.  *  
  11.  * @param numeral
  12.  * @return
  13.  */
  14. public class Roman {
  15.    
  16.     public enum RomanNumeral{
  17.         M(1000),
  18.         CM(900),
  19.         D(500),
  20.         CD(400),
  21.         C(100),
  22.         XC(90),
  23.         L(50),
  24.         XL(40),
  25.         X(10),
  26.         IX(9),
  27.         V(5),
  28.         IV(4),
  29.         I(1);
  30.        
  31.         private int value;
  32.        
  33.         RomanNumeral(int value){
  34.             this.value = value;
  35.         }
  36.  
  37.         public int getIntegerValue() {
  38.             return value;
  39.         }      
  40.     }
  41.    
  42.     public String romanize(int numeral) {
  43.         StringBuffer result = new StringBuffer();
  44.         for(RomanNumeral romanNumeral : RomanNumeral.values()){
  45.             while(numeral >= romanNumeral.getIntegerValue()){
  46.                 result.append(romanNumeral);
  47.                 numeral -= romanNumeral.getIntegerValue();
  48.             }
  49.         }
  50.         return result.toString();
  51.     }
  52.  
  53. }
  54.  
  55.  
Advertisement
Add Comment
Please, Sign In to add comment