REFACTOR: split validation into value object
This commit is contained in:
parent
438221e2d7
commit
6e75926f79
1 changed files with 18 additions and 5 deletions
|
|
@ -1,4 +1,21 @@
|
||||||
|
class RomanNumeralInput {
|
||||||
|
static const _minValue = 1;
|
||||||
|
static const _maxValue = 3999;
|
||||||
|
|
||||||
|
final int value;
|
||||||
|
|
||||||
|
RomanNumeralInput(this.value) {
|
||||||
|
if (value < _minValue || value > _maxValue) {
|
||||||
|
throw ArgumentError(
|
||||||
|
'Roman numeral input must be between $_minValue and $_maxValue',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
String integerToRoman(int number) {
|
String integerToRoman(int number) {
|
||||||
|
final input = RomanNumeralInput(number);
|
||||||
|
|
||||||
const conversionRules = [
|
const conversionRules = [
|
||||||
(1000, 'M'),
|
(1000, 'M'),
|
||||||
(900, 'CM'),
|
(900, 'CM'),
|
||||||
|
|
@ -15,12 +32,8 @@ String integerToRoman(int number) {
|
||||||
(1, 'I'),
|
(1, 'I'),
|
||||||
];
|
];
|
||||||
|
|
||||||
if (number <= 0 || number > 3999) {
|
|
||||||
throw ArgumentError('Number must be between 1 and 3999');
|
|
||||||
}
|
|
||||||
|
|
||||||
final result = StringBuffer();
|
final result = StringBuffer();
|
||||||
var remaining = number;
|
var remaining = input.value;
|
||||||
|
|
||||||
for (final (value, symbol) in conversionRules) {
|
for (final (value, symbol) in conversionRules) {
|
||||||
while (remaining >= value) {
|
while (remaining >= value) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue