Sha256: c88c12d7aebc0a9aa41fc89ef24d6e1306d63475662ed3e43065eec09c90ace2

Contents?: true

Size: 1.36 KB

Versions: 396

Compression:

Stored size: 1.36 KB

Contents

import java.util.Map;
import java.util.LinkedHashMap;

public class RomanNumeral {

    private int arabicNumeral;

    private static final Map<Integer, String> arabicToRomanConversions = new LinkedHashMap<>();

    static {
        arabicToRomanConversions.put(1000, "M");
        arabicToRomanConversions.put(900, "CM");
        arabicToRomanConversions.put(500, "D");
        arabicToRomanConversions.put(400, "CD");
        arabicToRomanConversions.put(100, "C");
        arabicToRomanConversions.put(90, "XC");
        arabicToRomanConversions.put(50, "L");
        arabicToRomanConversions.put(40, "XL");
        arabicToRomanConversions.put(10, "X");
        arabicToRomanConversions.put(9, "IX");
        arabicToRomanConversions.put(5, "V");
        arabicToRomanConversions.put(4, "IV");
        arabicToRomanConversions.put(1, "I");
    }

    public RomanNumeral(int arabicNumeral) {
        this.arabicNumeral = arabicNumeral;
    }


    public String getRomanNumeral() {
        StringBuilder romanNumeral = new StringBuilder();

        for (Map.Entry<Integer, String> conversion : arabicToRomanConversions.entrySet()) {
            while (arabicNumeral / conversion.getKey() > 0) {
                arabicNumeral -= conversion.getKey();

                romanNumeral.append(conversion.getValue());
            }
        }

        return romanNumeral.toString();
    }
}

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.118 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.117 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.116 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.115 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.114 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.113 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.111 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.110 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.109 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.108 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.107 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.106 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.105 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.104 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.103 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.102 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.101 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.100 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.99 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java