diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..101796c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ + +*.iml +out/** +.idea/** \ No newline at end of file diff --git a/Choix-implem.md b/Choix-implem.md new file mode 100644 index 0000000..3195e3f --- /dev/null +++ b/Choix-implem.md @@ -0,0 +1,13 @@ + +Une version de plus en java. + +Choix d'implémentation +====================== + +Sont privilégés la concision, la lisibilité et la performance. Pas besoin de sortir toute l'artillerie lourde pour +le moment. + +Tant que le besoin fonctionnel n'évolue pas, les 2 champs StringBuilder et int me permettent de simplifier les +signatures de méthodes. La contrepartie est que je ne teste pas le code méthode par méthode. + + diff --git a/src/FooBarQix.java b/src/FooBarQix.java new file mode 100644 index 0000000..079761d --- /dev/null +++ b/src/FooBarQix.java @@ -0,0 +1,79 @@ +import java.util.HashMap; +import java.util.Map; + +public class FooBarQix { + + + public static void main(String[] args) { + for (int i = 1; i < 100; i++) { + System.out.println(new FooBarQix(i).getReplacementLine()); + } + } + + private final int input; + private StringBuilder replacementLine; + + public FooBarQix(int toAnalyse) { + input = toAnalyse; + } + + public String getReplacementLine() { + replacementLine = new StringBuilder(); + appendNamesByDivisibles(); + appendNamesByContent(); + appendNumberIfNoName(); + return replacementLine.toString(); + } + + private void appendNamesByDivisibles() { + for (FBQEnum fbqEnum : FBQEnum.values()) { + if (isDivisibleBy(fbqEnum)) { + appendName(fbqEnum); + } + } + } + + private boolean isDivisibleBy(FBQEnum fbqEnum) { + return input % fbqEnum.value == 0; + } + + private void appendNamesByContent() { + for (char c : String.valueOf(input).toCharArray()) { + appendNameIfExistsReplacementFor(c); + } + } + + private void appendNameIfExistsReplacementFor(char c) { + FBQEnum fbqEnum = ST_VALUE_TO_FBQ.get(String.valueOf(c)); + if (fbqEnum != null) + appendName(fbqEnum); + } + + private void appendName(FBQEnum fbqEnum) { + replacementLine.append(fbqEnum.name()); + } + + private void appendNumberIfNoName() { + if (replacementLine.length() == 0) + replacementLine.append(input); + } + + private static enum FBQEnum { + Foo(3), Bar(5), Qix(7); + + private int value; + + FBQEnum(int value) { + assert value < 10; + this.value = value; + } + } + + private static final Map ST_VALUE_TO_FBQ = new HashMap(); + static { + for (FBQEnum fbqEnum : FBQEnum.values()) { + ST_VALUE_TO_FBQ.put(String.valueOf(fbqEnum.value), fbqEnum); + } + } + +} diff --git a/src/TestFooBarQuix.java b/src/TestFooBarQuix.java new file mode 100644 index 0000000..fad24f9 --- /dev/null +++ b/src/TestFooBarQuix.java @@ -0,0 +1,36 @@ +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + */ +public class TestFooBarQuix { + + @Test + public void highLevelTests(){ + assertIntToFooBarQix(1 , "1"); + assertIntToFooBarQix(2 , "2"); + assertIntToFooBarQix(3 , "FooFoo"); + assertIntToFooBarQix(6 , "Foo"); + assertIntToFooBarQix(13 , "Foo"); + assertIntToFooBarQix(15 , "FooBarBar"); + assertIntToFooBarQix(33 , "FooFooFoo"); + assertIntToFooBarQix(21 , "FooQix"); + assertIntToFooBarQix(51 , "FooBar"); + assertIntToFooBarQix(53 , "BarFoo"); + } + + private void assertIntToFooBarQix(int i, String fooBar) { + assertEquals(new FooBarQix(i).getReplacementLine(), fooBar); + } + + @Test + public void perf(){ + long startTime = System.currentTimeMillis(); + for (int i = 1; i < 1000001; i++) { + new FooBarQix(i).getReplacementLine(); + } + System.out.println("1 000 000 en " + (System.currentTimeMillis() - startTime) + " ms."); + } + +}