From ec361e28fe4d1d95d5d2b8a0ee21aafacd5b52ca Mon Sep 17 00:00:00 2001 From: Patrick Corbett Date: Mon, 22 Apr 2024 18:09:09 +0200 Subject: [PATCH] #21 - New Currency "Franc" - Basic Test Driven Development approach, Write test first, make it compile and then flesh it out --- src/main/java/guru/springframework/Franc.java | 24 +++++++++++++++++ .../java/guru/springframework/MoneyTest.java | 26 ++++++++++++++++--- 2 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 src/main/java/guru/springframework/Franc.java diff --git a/src/main/java/guru/springframework/Franc.java b/src/main/java/guru/springframework/Franc.java new file mode 100644 index 00000000..6e01e72d --- /dev/null +++ b/src/main/java/guru/springframework/Franc.java @@ -0,0 +1,24 @@ +package guru.springframework; + +/** + * @author Patrick Corbett + */ +public class Franc { + + private int amount; + + public Franc(int pAmount) { + amount = pAmount; + } + + Franc times(int pMultiplier) { + return new Franc(amount * pMultiplier); + } + + public boolean equals(Object object) { + // Manual equals, left like this as equals with null or other class will be done later + Franc franc = (Franc) object; + return amount == franc.amount; + } + +} diff --git a/src/test/java/guru/springframework/MoneyTest.java b/src/test/java/guru/springframework/MoneyTest.java index ee9e3630..d068cd00 100644 --- a/src/test/java/guru/springframework/MoneyTest.java +++ b/src/test/java/guru/springframework/MoneyTest.java @@ -3,6 +3,9 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + /** * @author Patrick Corbett */ @@ -12,15 +15,30 @@ class MoneyTest { void testMultiplication() { Dollar five = new Dollar(5); Dollar product = five.times(2); - Assertions.assertEquals(new Dollar(10), product); + assertEquals(new Dollar(10), product); product = five.times(3); - Assertions.assertEquals(new Dollar(15), product); + assertEquals(new Dollar(15), product); } @Test void testEquality() { - Assertions.assertEquals(new Dollar(5), new Dollar(5)); - Assertions.assertNotEquals(new Dollar(5), new Dollar(8)); + assertEquals(new Dollar(5), new Dollar(5)); + assertNotEquals(new Dollar(5), new Dollar(8)); + } + + @Test + void testMultiplicationFranc() { + Franc five = new Franc(5); + Franc product = five.times(2); + assertEquals(new Franc(10), product); + product = five.times(3); + assertEquals(new Franc(15), product); + } + + @Test + void testEqualityFranc() { + assertEquals(new Franc(5), new Franc(5)); + assertNotEquals(new Franc(5), new Franc(8)); } }