diff --git a/src/main/java/guru/springframework/Dollar.java b/src/main/java/guru/springframework/Dollar.java new file mode 100644 index 00000000..c8d6f1e0 --- /dev/null +++ b/src/main/java/guru/springframework/Dollar.java @@ -0,0 +1,12 @@ +package guru.springframework; + +public class Dollar extends Money{ + + public Dollar(int amount, String currency) { + super(amount, currency); + } + + public Money times(int multiplier) { + return Money.dollar(amount*multiplier); + } +} diff --git a/src/main/java/guru/springframework/Franc.java b/src/main/java/guru/springframework/Franc.java new file mode 100644 index 00000000..a346c7a4 --- /dev/null +++ b/src/main/java/guru/springframework/Franc.java @@ -0,0 +1,13 @@ +package guru.springframework; + +public class Franc extends Money{ + + public Franc(int amount, String currency) { + super(amount, currency); + } + + public Money times(int multiplier) { + return Money.franc(amount*multiplier); + } + +} diff --git a/src/main/java/guru/springframework/Money.java b/src/main/java/guru/springframework/Money.java new file mode 100644 index 00000000..f50cdee9 --- /dev/null +++ b/src/main/java/guru/springframework/Money.java @@ -0,0 +1,30 @@ +package guru.springframework; + +public abstract class Money { + private final String currency; + protected final int amount; + + public Money(int amount, String currency) { + this.amount = amount; + this.currency = currency; + } + + protected String currency() { + return currency; + } + + public abstract Money times(int multiplier); + + public static Money dollar(int amount) { + return new Dollar(amount, "USD"); + } + + public static Money franc(int amount) { + return new Franc(amount, "CHF"); + } + + public boolean equals(Object object) { + Money money = (Money) object; + return amount == money.amount && this.getClass().equals(object.getClass()); + } +} diff --git a/src/test/java/guru/springframework/MoneyTest.java b/src/test/java/guru/springframework/MoneyTest.java new file mode 100644 index 00000000..ee2c2538 --- /dev/null +++ b/src/test/java/guru/springframework/MoneyTest.java @@ -0,0 +1,43 @@ +package guru.springframework; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +public class MoneyTest { + + @Test + void testMultiplicationDollar() { + Money five = Money.dollar(5); + assertEquals(Money.dollar(10), five.times(2)); + assertEquals(Money.dollar(15), five.times(3)); + } + + @Test + void testEqualityDollar() { + assertEquals(Money.dollar(5), Money.dollar(5)); + assertNotEquals(Money.dollar(5), Money.dollar(10)); + assertNotEquals(Money.dollar(5),Money.franc(5)); + } + + + @Test + void testMultiplicationFranc() { + Money five = Money.franc(5); + assertEquals(Money.franc(10), five.times(2)); + assertEquals(Money.franc(15), five.times(3)); + } + + @Test + void testEqualityFranc() { + assertEquals(Money.franc(5), Money.franc(5)); + assertNotEquals(Money.franc(5), Money.franc(10)); + } + + @Test + void testCurrency() { + assertEquals("USD", Money.dollar(1).currency()); + assertEquals("CHF", Money.franc(1).currency()); + } +}