Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
twitch
live-coding-fr
Commits
95bad854
Commit
95bad854
authored
Jul 23, 2020
by
Colin DAMON
Browse files
Live implementation
parent
ec0d2184
Changes
3
Hide whitespace changes
Inline
Side-by-side
README.md
View file @
95bad854
...
@@ -23,3 +23,7 @@ Ce dépôt Git a pour but de partager les différents ateliers pouvant être ré
...
@@ -23,3 +23,7 @@ Ce dépôt Git a pour but de partager les différents ateliers pouvant être ré
*
Niveau: Débutant
*
Niveau: Débutant
*
Lien:
[
Roman Numerals avec Julie et Colin
](
https://www.youtube.com/watch?v=CWJqq_k7hDY
)
*
Lien:
[
Roman Numerals avec Julie et Colin
](
https://www.youtube.com/watch?v=CWJqq_k7hDY
)
*
Go To:
[
RomanNumerals
](
/romanNumerals
)
*
Go To:
[
RomanNumerals
](
/romanNumerals
)
*
**[Presque String Calculator](https://codingdojo.org/kata/StringCalculator/)**
: Par Marina FILLATRE et Colin DAMON le 23/07/2020
*
Niveau: Moyen
*
Lien:
[
Agility && TDD, Marina et Colin
](
https://www.twitch.tv/videos/688222419
)
*
Go To:
[
stringCalculator
](
/stringCalculator
)
stringCalculator/src/main/java/fr/ippon/stringcalculator/Calculator.java
0 → 100644
View file @
95bad854
package
fr.ippon.stringcalculator
;
import
java.math.BigDecimal
;
import
java.util.function.BinaryOperator
;
public
class
Calculator
{
public
static
String
sum
(
String
first
,
String
second
)
{
return
operate
(
first
,
second
,
BigDecimal:
:
add
);
}
public
static
String
substract
(
String
first
,
String
second
)
{
return
operate
(
first
,
second
,
BigDecimal:
:
subtract
);
}
private
static
String
operate
(
String
first
,
String
second
,
BinaryOperator
<
BigDecimal
>
operation
)
{
notNull
(
first
);
notNull
(
second
);
return
operation
.
apply
(
toBigDecimal
(
first
),
toBigDecimal
(
second
))
.
toPlainString
();
}
private
static
BigDecimal
toBigDecimal
(
String
value
)
{
if
(
value
.
isBlank
())
{
return
BigDecimal
.
ZERO
;
}
return
new
BigDecimal
(
value
.
replace
(
","
,
"."
));
}
private
static
void
notNull
(
String
first
)
{
if
(
first
==
null
)
{
throw
new
IllegalArgumentException
();
}
}
}
stringCalculator/src/test/java/fr/ippon/stringcalculator/CalculatorTest.java
0 → 100644
View file @
95bad854
package
fr.ippon.stringcalculator
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.*;
import
org.junit.jupiter.api.Test
;
public
class
CalculatorTest
{
@Test
void
shouldNotSumWithNullFirstInput
()
{
assertThatThrownBy
(()
->
Calculator
.
sum
(
null
,
"3"
))
.
isExactlyInstanceOf
(
IllegalArgumentException
.
class
);
}
@Test
void
shouldNotSumWithNullSecondInput
()
{
assertThatThrownBy
(()
->
Calculator
.
sum
(
"3"
,
null
))
.
isExactlyInstanceOf
(
IllegalArgumentException
.
class
);
}
@Test
void
shouldSumEmptyInputs
()
{
assertThat
(
Calculator
.
sum
(
""
,
""
)).
isEqualTo
(
"0"
);
}
@Test
void
shouldSumIntegerWithEmptyFirstInput
()
{
assertThat
(
Calculator
.
sum
(
""
,
"4"
)).
isEqualTo
(
"4"
);
}
@Test
void
shouldSumIntegerWithEmptySecondInput
()
{
assertThat
(
Calculator
.
sum
(
"4"
,
""
)).
isEqualTo
(
"4"
);
}
@Test
void
shouldSumIntegers
()
{
assertThat
(
Calculator
.
sum
(
"1"
,
"2"
)).
isEqualTo
(
"3"
);
}
@Test
void
shouldSumFloats
()
{
assertThat
(
Calculator
.
sum
(
"1.01"
,
"2.02"
))
.
isEqualTo
(
"3.03"
);
}
@Test
void
shouldSumCommaSeparatedFloats
()
{
assertThat
(
Calculator
.
sum
(
"1,01"
,
"2,02"
))
.
isEqualTo
(
"3.03"
);
}
@Test
void
shouldSumNegativeCommaSeparatedFloats
()
{
assertThat
(
Calculator
.
sum
(
"-1,01"
,
"-2,02"
))
.
isEqualTo
(
"-3.03"
);
}
@Test
void
shouldNotSubstractNullInputs
()
{
assertThatThrownBy
(
()
->
Calculator
.
substract
(
null
,
"3"
))
.
isExactlyInstanceOf
(
IllegalArgumentException
.
class
);
}
@Test
void
shouldNotSubstractWithNullSecondInputs
()
{
assertThatThrownBy
(
()
->
Calculator
.
substract
(
"3"
,
null
))
.
isExactlyInstanceOf
(
IllegalArgumentException
.
class
);
}
@Test
void
shouldSubstractCommaSeparatedFloats
()
{
assertThat
(
Calculator
.
substract
(
"1,01"
,
"2,02"
))
.
isEqualTo
(
"-1.01"
);
}
@Test
void
shouldSumTooBigIntegers
()
{
assertThat
(
Calculator
.
sum
(
String
.
valueOf
(
Integer
.
MAX_VALUE
),
"2,02"
))
.
isEqualTo
(
"2147483649.02"
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment