Created
April 8, 2019 18:43
-
-
Save ygrenzinger/8e91413abfca16c5171383b3d7ed2fdb to your computer and use it in GitHub Desktop.
FooBarQix Polytech
This file contains 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 com.course.polytech; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
public class FooBarQix { | |
private static Map<Integer, String> NUMBER_2_STRING = new HashMap<>(); | |
static { | |
NUMBER_2_STRING.put(3, "Foo"); | |
NUMBER_2_STRING.put(5, "Bar"); | |
NUMBER_2_STRING.put(7, "Qix"); | |
} | |
public static String convert(int number) { | |
String result = NUMBER_2_STRING.keySet() | |
.stream() | |
.map(k -> convert(number, k)) | |
.collect(Collectors.joining()); | |
return result.isEmpty() ? String.valueOf(number) : result; | |
} | |
private static String convert(int number, Integer key) { | |
return manageDivisibility(number, key) + manageContaining(number, key); | |
} | |
private static String manageContaining(int number, Integer value) { | |
char charForNumber = Character.forDigit(value, 10); | |
return String.valueOf(number) | |
.chars() | |
.filter(x -> charForNumber == x) | |
.mapToObj(c -> NUMBER_2_STRING.get(value)) | |
.collect(Collectors.joining()); | |
} | |
private static String manageDivisibility(int number, Integer value) { | |
if (number % value == 0) { | |
return NUMBER_2_STRING.get(value); | |
} | |
return ""; | |
} | |
public static void main(String[] args) { | |
IntStream.range(1, 100).forEach(number -> { | |
System.out.println(convert(number)); | |
}); | |
} | |
} |
This file contains 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 com.course.polytech | |
import java.util.HashMap | |
import java.util.stream.Collectors | |
import java.util.stream.IntStream | |
object FooBarQix { | |
private val NUMBER_2_STRING = HashMap<Int, String>() | |
init { | |
NUMBER_2_STRING[3] = "Foo" | |
NUMBER_2_STRING[5] = "Bar" | |
NUMBER_2_STRING[7] = "Qix" | |
} | |
fun convert(number: Int): String { | |
val result = NUMBER_2_STRING.keys.joinToString { k -> convert(number, k) } | |
return if (result.isEmpty()) number.toString() else result | |
} | |
private fun convert(number: Int, key: Int): String { | |
return manageDivisibility(number, key) + manageContaining(number, key) | |
} | |
private fun manageContaining(number: Int, value: Int): String { | |
val charForNumber = Character.forDigit(value, 10) | |
return number.toString() | |
.chars() | |
.filter { x -> charForNumber.toInt() == x } | |
.mapToObj<String> { c -> NUMBER_2_STRING[value] } | |
.collect(Collectors.joining()) | |
} | |
private fun manageDivisibility(number: Int, value: Int): String { | |
return if (number % value == 0) { | |
NUMBER_2_STRING[value]!! | |
} else "" | |
} | |
@JvmStatic | |
fun main() { | |
IntStream.range(1, 100).forEach { number -> println(convert(number)) } | |
} | |
} |
This file contains 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 com.course.polytech; | |
import org.junit.Test; | |
import static org.assertj.core.api.Assertions.assertThat; | |
public class FooBarQixTest { | |
@Test | |
public void should_return_number() { | |
assertThat(FooBarQix.convert(1)).isEqualTo("1"); | |
} | |
@Test | |
public void if_divisible_by_3_should_return_foo() { | |
assertThat(FooBarQix.convert(9)).isEqualTo("Foo"); | |
} | |
@Test | |
public void if_divisible_by_5_should_return_bar() { | |
assertThat(FooBarQix.convert(10)).isEqualTo("Bar"); | |
} | |
@Test | |
public void if_divisible_by_7_should_return_bar() { | |
assertThat(FooBarQix.convert(14)).isEqualTo("Qix"); | |
} | |
@Test | |
public void should_return_FooBarQix() { | |
assertThat(FooBarQix.convert(75)).isEqualTo("FooBarBarQix"); | |
} | |
@Test | |
public void should_return_BarQix() { | |
assertThat(FooBarQix.convert(35)).isEqualTo("FooBarBarQix"); | |
} | |
@Test | |
public void should_return_FooFooFoo() { | |
assertThat(FooBarQix.convert(33)).isEqualTo("FooFooFoo"); | |
} | |
@Test | |
public void should_return_QixQixQix() { | |
assertThat(FooBarQix.convert(77)).isEqualTo("QixQixQix"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment