213 shaares
2 résultats
taggé
number
Je dois encore le travailler mais c'est un début de réflexion.
Objectif : utiliser la classe de cette manière :
RomanNumber toto = new RomanNumber(5);
System.out.println(toto.toString());
Dans le cas ou l'on souhaite une représentation textuelle, bien sûr.
import java.util.LinkedList;
import java.util.List;
public class RomanNumber {
private List<RomanCharacter> characters;
public RomanNumber(int arabicNumber) {
this.characters = new LinkedList<>();
this.convertArabicNumberToRomanNumber(arabicNumber);
}
private void convertArabicNumberToRomanNumber(int arabicNumber) {
int remains = arabicNumber;
for (RomanCharacter currentCharacter : RomanCharacter.getOrderedRomanNumbers()) {
while (this.arabicNumberIsGreaterThanRomanNumber(remains, currentCharacter)) {
this.characters.add(currentCharacter);
remains = remains - currentCharacter.toInt();
}
if (remains == 0) {
break;
}
}
}
private boolean arabicNumberIsGreaterThanRomanNumber(int arabicNumber, RomanCharacter romanNumber) {
return arabicNumber > 0 && arabicNumber >= romanNumber.toInt();
}
public String toString() {
StringBuilder textualVersion = new StringBuilder(this.characters.size());
for (RomanCharacter character : this.characters) {
textualVersion.append(character.toString());
}
return textualVersion.toString();
}
}
import java.util.*;
public enum RomanCharacter {
M(1000), CM(900), D(500), CD(400), C(100), XC(90), L(50), XL(40), X(10), IX(9), V(5), IV(4), I(1);
private final int correspondingArabicNumber;
RomanCharacter(int arabicNumber) {
this.correspondingArabicNumber = arabicNumber;
}
public int toInt() {
return this.correspondingArabicNumber;
}
public String toString() {
return this.name();
}
public static List<RomanCharacter> getOrderedRomanNumbers() {
return Arrays.asList(RomanCharacter.values());
}
}
Je me met une note ici, parce que je cherche régulièrement cette feature
mvn versions:set -DnewVersion=2.50.1-SNAPSHOT
It will adjust all pom versions, parent versions and dependency versions in a multi-module project.