Commit 91a88727 authored by Colin DAMON's avatar Colin DAMON

Diamond kata in java

parent 6a3ab8f3
......@@ -37,3 +37,4 @@ include:
- local: "/try-monade/.gitlab-ci.yml"
- local: "/java-puzzles/.gitlab-ci.yml"
- local: "/java-monades/.gitlab-ci.yml"
- local: "/java-diamond/.gitlab-ci.yml"
package-java-diamond:
variables:
PROJECT_FOLDER: "java-diamond"
extends: .java
only:
refs:
- master
- merge_requests
changes:
- ".gitlab-common-ci.yml"
- "java-diamond/**/*"
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<version>1.0.0</version>
<groupId>fr.ippon.kata</groupId>
<artifactId>java-parent</artifactId>
<relativePath>../java-parent</relativePath>
</parent>
<version>1.0.0-SNAPSHOT</version>
<artifactId>diamond</artifactId>
<name>Diamond</name>
<developers>
<developer>
<name>Sara TABHABIK</name>
</developer>
<developer>
<email>cdamon@ippon.fr</email>
<name>Colin DAMON</name>
</developer>
</developers>
</project>
# Diamond
Résolution en Java du kata [Diamond](https://codingdojo.org/kata/Diamond/).
- **Auteurs** : Sara TABHABIK et Colin DAMON
- **Date** : 20/05/2021
- **Langage** : Java
- **Niveau** : Moyen
- **Replay** : [Twitch](https://www.twitch.tv/videos/1028994816)
package fr.craft.kata;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.IntFunction;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Diamond {
public static List<String> of(char size) {
List<String> diamond = IntStream
.range((int) 'A', ((int) size) + 1)
.mapToObj(toLine(size))
.collect(Collectors.toList());
List<String> secondPart = new ArrayList<>(diamond);
Collections.reverse(secondPart);
secondPart.remove(0);
diamond.addAll(secondPart);
return diamond;
}
private static IntFunction<String> toLine(char size) {
return current -> Line.of((char) current, size)
.representation();
}
}
package fr.craft.kata;
public record Line(String representation) {
public static Line of(char character, char size) {
String outerSpaces = outerSpaces(character, size);
if (character == 'A') {
return new Line(outerSpaces + "A" + outerSpaces);
}
return new Line(outerSpaces + character
+ innerSpaces(character) + character + outerSpaces);
}
private static String outerSpaces(char character,
char size) {
return " ".repeat(size - character);
}
private static String innerSpaces(char character) {
return " ".repeat(((character - 'A') - 1) * 2 + 1);
}
}
package fr.craft.kata;
import static org.assertj.core.api.Assertions.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import org.junit.jupiter.api.Test;
class DiamondTest {
@Test
void shouldDrawDiamondOfA() {
assertThat(Diamond.of('A')).isEqualTo(diamond('A'));
}
@Test
void shouldDrawDiamondOfB() {
assertThat(Diamond.of('B')).isEqualTo(diamond('B'));
}
@Test
void shouldDrawDiamondOfC() {
assertThat(Diamond.of('C')).isEqualTo(diamond('C'));
}
@Test
void shouldDrawDiamondOfD() {
assertThat(Diamond.of('D')).isEqualTo(diamond('D'));
}
private List<String> diamond(char file) {
try {
return Files.readAllLines(Paths
.get("src/test/resources/" + String.valueOf(file)
.toLowerCase() + ".txt"));
} catch (IOException e) {
throw new AssertionError(e.getMessage(), e);
}
}
}
package fr.craft.kata;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
class LineTest {
@Test
void shouldGetLetterALineForSizeA() {
assertThat(Line.of('A', 'A')
.representation()).isEqualTo("A");
}
@Test
void shouldGetLetterALineForSizeB() {
assertThat(Line.of('A', 'B')
.representation()).isEqualTo(" A ");
}
@Test
void shouldGetLetterBLineForSizeB() {
assertThat(Line.of('B', 'B')
.representation()).isEqualTo("B B");
}
@Test
void shouldGetLetterBLineForSizeC() {
assertThat(Line.of('B', 'C')
.representation()).isEqualTo(" B B ");
}
@Test
void shouldGetLetterDLineForSizeD() {
assertThat(Line.of('D', 'D')
.representation())
.isEqualTo("D" + " ".repeat(5) + "D");
}
}
A
\ No newline at end of file
A
B B
A
\ No newline at end of file
A
B B
C C
B B
A
\ No newline at end of file
A
B B
C C
D D
C C
B B
A
\ No newline at end of file
......@@ -43,6 +43,7 @@ Un kata de code est un petit exercice pensé pour s'entrainer jusqu'à maitriser
- [Bowling Game](/bowling-game)
- [Tennis Refactoring kata](/tennis/refactoring)
- [Puzzles](java-puzzles)
- [Diamond](java-diamond)
### Énervé
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment