Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial Add Multi-Currency Money #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/java/guru/springframework/Dollar.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
13 changes: 13 additions & 0 deletions src/main/java/guru/springframework/Franc.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
30 changes: 30 additions & 0 deletions src/main/java/guru/springframework/Money.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
43 changes: 43 additions & 0 deletions src/test/java/guru/springframework/MoneyTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}