public class ShortRomanNumeral
{
internal struct RomanMapper
{
public int Value { get; set; }
public string Presentation { get; set; }
}
private const int MinShortRomanValue = 0;
private const int MaxShortRomanValue = 3999;
private const string zeroRomanPresentation = "Nulla";
private readonly RomanMapper[] shortRomanMappers =
{
new RomanMapper(){Value = 1000, Presentation = "M"},
new RomanMapper()Value = 900, Presentation = "CM"},
new RomanMapper(){Value = 500, Presentation = "D"},
new RomanMapper(){Value = 400, Presentation = "CD"},
new RomanMapper(){Value = 100, Presentation = "C"},
new RomanMapper(){Value = 90, Presentation = "XC"},
new RomanMapper(){Value = 50, Presentation = "L"},
new RomanMapper(){Value = 40, Presentation = "XL"},
new RomanMapper(){Value = 10, Presentation = "X"},
new RomanMapper(){Value = 9, Presentation = "IX"},
new RomanMapper(){Value = 5, Presentation = "V"},
new RomanMapper()Value = 4, Presentation = "IV"},
new RomanMapper(){Value = 1, Presentation = "I"}
};
private readonly int value;
private readonly string presentation;
public ShortRomanNumeral(int value)
{
if (value < MinShortRomanValue || value > MaxShortRomanValue)
{
throw new ArgumentException();
}
else
{
this.value = value;
presentation = CalculateRomanPresentation();
}
}
private string CalculateRomanPresentation()
{
if (value > 0)
{
return NonZeroRomanPresentation();
}
return zeroRomanPresentation;
}
private string NonZeroRomanPresentation()
{
int currentValue = value;
StringBuilder presentation = new StringBuilder();
foreach (RomanMapper romanMapper in shortRomanMappers)
{
while (currentValue - romanMapper.Value >= 0)
{
currentValue -= romanMapper.Value;
presentation.Append(romanMapper.Presentation);
}
}
return presentation.ToString();
}
public override string ToString()
{
return presentation;
}
}