Commit 26994b78 authored by Colin DAMON's avatar Colin DAMON

String calculator second edition

parent 525eb43f
......@@ -12,3 +12,4 @@ include:
- local: "/java-pagination-seven/.gitlab-ci.yml"
- local: "/a-brief-history-of-date/.gitlab-ci.yml"
- local: "/mustache-replacer/.gitlab-ci.yml"
- local: "/string-calculator-2/.gitlab-ci.yml"
......@@ -30,5 +30,6 @@ Ce dépôt Git a pour but de partager les différents ateliers pouvant être ré
| [GreenIT: simple coup de peinture ou réelle démarche écologique ?](https://www.youtube.com/watch?v=liTuZZTCpGc) | Discussion | |
| [Une annonce pour recruter un crafter - Caroline, Chloé, Ianic et Colin](https://www.youtube.com/watch?v=E5AX6Ar1Fog) | Discussion | |
| [PadBowl](https://gitlab.com/cdamon/padbowl) | Application | Enervé |
| [A Brief History of Date](/a-brief-history-of-date) | Kata | Moyen |
| [A Brief History of Date](/a-brief-history-of-date) | Kata | Moyenne |
| [Mustache Replacer](/mustache-replacer) | Kata | Facile |
| [String calculator 2](/string-calculator-2) | Kata | Moyenne |
package-string-calculator:
variables:
PROJECT_FOLDER: "string-calculator-2"
extends: .java
only:
refs:
- master
- merge_requests
changes:
- ".gitlab-common-ci.yml"
- "string-calculator-2/**/*"
# String calculator
Résolution, en live, de [StringCalculator](/stringCalculator).
- **Auteurs** : Colin DAMON
- **Date** : 06/11/2020
- **Langage** : Java
- **Niveau** : Moyen
- **Replay** : TODO
<?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>string-calculator-2</artifactId>
<name>StringCalculator2</name>
<developers>
<developer>
<email>cdamon@ippon.fr</email>
<name>Colin DAMON</name>
</developer>
</developers>
</project>
package fr.ippon.stringcalculator;
import java.math.BigDecimal;
import java.util.Arrays;
public final class StringCalculator {
private static final String SEPARATOR = ",|\n";
private StringCalculator() {}
public static String sum(String numbers) {
if (noNumbers(numbers)) {
return "0";
}
return Arrays.stream(numbers.split(SEPARATOR)).map(BigDecimal::new).reduce(BigDecimal.ZERO, BigDecimal::add).toPlainString();
}
private static boolean noNumbers(String numbers) {
return numbers == null || numbers.isBlank();
}
}
package fr.ippon.stringcalculator;
import static fr.ippon.stringcalculator.StringCalculator.*;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class StringCalculatorTest {
@Test
void shouldSumToZeroFromNullNumbers() {
assertThat(sum(null)).isEqualTo("0");
}
@Test
void shouldSumToZeroFromEmptyNumbers() {
assertThat(sum("")).isEqualTo("0");
}
@Test
void shouldSumToFourFromFour() {
assertThat(sum("4")).isEqualTo("4");
}
@Test
void shouldSumFourAndTwo() {
assertThat(sum("4,2")).isEqualTo("6");
}
@Test
void shouldSumOneZeroOneAndTwoZeroTwo() {
assertThat(sum("1.01,2.02")).isEqualTo("3.03");
}
@Test
void shouldUseNewlinesAsSeparator() {
assertThat(sum("1\n2,3")).isEqualTo("6");
}
}
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