Created
October 17, 2012 22:12
-
-
Save bamthomas/3908664 to your computer and use it in GitHub Desktop.
Chiffre Romain TDD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bam; | |
import static bam.ChiffreRomain.Chiffre.*; | |
import static java.lang.Math.abs; | |
import static java.util.Arrays.asList; | |
public class ChiffreRomain { | |
public final String chiffreRomain; | |
enum Chiffre { | |
Z(0, null), I(1, Z), V(5, I), X(10, I), L(50, X), C(100, X), D(500, C), M(1000, C); | |
final int chiffreArabe; | |
final Chiffre precedent; | |
Chiffre(int i, Chiffre precedent) { | |
this.chiffreArabe = i; | |
this.precedent = precedent; | |
} | |
} | |
public ChiffreRomain(int chiffreArabe) { | |
AccumulateurRomain ac = new AccumulateurRomain(chiffreArabe); | |
for (Chiffre lePalier : asList(M, D, C, L, X, V, I)) ac.cumule(lePalier); | |
this.chiffreRomain = ac.accumulateur.toString(); | |
} | |
static class AccumulateurRomain { | |
private int calculEnCours; | |
StringBuffer accumulateur = new StringBuffer(); | |
void cumule(Chiffre palier) { | |
int laDivisionEntiere = calculEnCours / palier.chiffreArabe; | |
accumule(laDivisionEntiere, palier); | |
if (calculEnCours >= palier.chiffreArabe - palier.precedent.chiffreArabe) { | |
accumule(-1, palier.precedent); | |
accumule(1, palier); | |
} | |
} | |
private void accumule(int fois, Chiffre romain) { | |
for (int i = 0; i < abs(fois); i++) accumulateur.append(romain); | |
calculEnCours -= fois * romain.chiffreArabe; | |
} | |
AccumulateurRomain(int chiffre) { this.calculEnCours = chiffre; } | |
} | |
@Override | |
public String toString() { return chiffreRomain; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bam; | |
import org.junit.Test; | |
import static junit.framework.Assert.assertEquals; | |
public class ChiffreRomainTest { | |
@Test | |
public void unDeuxTrois() throws Exception { | |
assertEquals("I", chiffreRomain(1)); | |
assertEquals("II", chiffreRomain(2)); | |
assertEquals("III", chiffreRomain(3)); | |
} | |
@Test | |
public void quatreEtCinq() throws Exception { | |
assertEquals("IV", chiffreRomain(4)); | |
assertEquals("V", chiffreRomain(5)); | |
} | |
@Test | |
public void sixSeptHuit() throws Exception { | |
assertEquals("VI", chiffreRomain(6)); | |
assertEquals("VII", chiffreRomain(7)); | |
assertEquals("VIII", chiffreRomain(8)); | |
} | |
@Test | |
public void neufDix() throws Exception { | |
assertEquals("IX", chiffreRomain(9)); | |
assertEquals("X", chiffreRomain(10)); | |
} | |
@Test | |
public void dixNeufEtVingt() throws Exception { | |
assertEquals("XIX", chiffreRomain(19)); | |
assertEquals("XX", chiffreRomain(20)); | |
} | |
@Test | |
public void quaranteNeufEtCinquante() throws Exception { | |
assertEquals("XL", chiffreRomain(40)); | |
assertEquals("XLV", chiffreRomain(45)); | |
assertEquals("XLIX", chiffreRomain(49)); | |
} | |
@Test | |
public void quatreVigtDixHuit() throws Exception { | |
assertEquals("XCVIII", chiffreRomain(98)); | |
} | |
@Test | |
public void cinqCent() throws Exception { | |
assertEquals("D", chiffreRomain(500)); | |
} | |
@Test | |
public void neufCentSoixanteEtOnze() throws Exception { | |
assertEquals("CMLXXI", chiffreRomain(971)); | |
} | |
String chiffreRomain(int arabe) { | |
return new ChiffreRomain(arabe).toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment