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.180 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.179 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.178 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.177 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.176 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.175 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.174 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.173 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.172 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.171 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.170 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.169 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.167 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.166 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.165 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.164 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.163 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.162 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.161 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java
trackler-2.2.1.160 tracks/java/exercises/roman-numerals/.meta/src/reference/java/RomanNumeral.java