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

Feat/#308 Moeny 도메인 추가 #326

Merged
merged 9 commits into from
May 5, 2024
Merged
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ dependencies {
testAnnotationProcessor 'org.projectlombok:lombok'

// test container
testImplementation "org.testcontainers:junit-jupiter:1.16.3"
testImplementation 'org.testcontainers:mysql:1.16.3'
testImplementation 'org.testcontainers:junit-jupiter:1.19.7'
testImplementation 'org.testcontainers:mysql:1.19.7'
implementation 'org.testcontainers:localstack:1.19.7'

// Rest Docs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.math.BigInteger;
import java.util.Objects;

import codesquad.fineants.domain.common.money.Expression;
import codesquad.fineants.domain.common.money.Money;
import lombok.AccessLevel;
import lombok.Getter;
Expand Down Expand Up @@ -41,15 +42,15 @@ public Count add(Count count) {
return new Count(value.add(count.value));
}

public Money multiply(Money money) {
return money.multiply(value);
public Expression multiply(Money money) {
return money.times(value.intValue());
}

public boolean isZero() {
return value.compareTo(BigInteger.ZERO) == 0;
}

public Money division(Money numerator) {
public Expression division(Money numerator) {
return numerator.divide(value);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package codesquad.fineants.domain.common.money;

import static codesquad.fineants.domain.common.money.Currency.*;

import org.jetbrains.annotations.NotNull;

import codesquad.fineants.domain.common.count.Count;

public class AverageDivision implements Expression {

private final Expression division;
private final Count divisor;

public AverageDivision(Expression division, Count divisor) {
this.division = division;
this.divisor = divisor;
}

@Override
public Money reduce(Bank bank, Currency to) {
Expression result = divisor.division(bank.reduce(division, to));
return bank.reduce(result, to);
}

@Override
public Expression plus(Expression addend) {
return new Sum(this, addend);
}

@Override
public Expression minus(Expression subtrahend) {
return new Subtraction(this, subtrahend);
}

@Override
public Expression times(int multiplier) {
return new AverageDivision(division.times(multiplier), divisor);
}

@Override
public Expression divide(Count divisor) {
return new AverageDivision(this, divisor);
}

@Override
public RateDivision divide(Expression divisor) {
return new RateDivision(this, divisor);
}

@Override
public Percentage toPercentage(Bank bank, Currency to) {
return Percentage.from(reduce(bank, to).amount);
}

@Override
public int compareTo(@NotNull Expression o) {
Bank bank = Bank.getInstance();
Currency to = KRW;
Money won = this.reduce(bank, to);
Money won2 = o.reduce(bank, to);
return won.compareTo(won2);
}
}
38 changes: 38 additions & 0 deletions src/main/java/codesquad/fineants/domain/common/money/Bank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package codesquad.fineants.domain.common.money;

import java.util.Hashtable;

public final class Bank {

private static Bank instance;
private final Hashtable<Pair, Double> rates = new Hashtable<>();

Bank() {
}

public static Bank getInstance() {
if (instance == null) {
instance = new Bank();
}
return instance;
}

public Money reduce(Expression source, Currency to) {
return source.reduce(this, to);
}

public void addRate(Currency from, Currency to, double rate) {
rates.put(new Pair(from, to), rate);
}

double rate(Currency from, Currency to) {
if (from.equals(to)) {
return 1;
}
return rates.get(new Pair(from, to));
}

public Money toWon(Expression amount) {
return reduce(amount, Currency.KRW);
}
}
16 changes: 16 additions & 0 deletions src/main/java/codesquad/fineants/domain/common/money/Currency.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package codesquad.fineants.domain.common.money;

import lombok.Getter;

@Getter
public enum Currency {
USD("$"),
CHF("FR"),
KRW("₩");

private final String symbol;

Currency(String symbol) {
this.symbol = symbol;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package codesquad.fineants.domain.common.money;

import codesquad.fineants.domain.common.count.Count;

public interface Expression extends Comparable<Expression> {
Money reduce(Bank bank, Currency to);

Expression plus(Expression addend);

Expression minus(Expression subtrahend);

Expression times(int multiplier);

Expression divide(Count divisor);

RateDivision divide(Expression divisor);

Percentage toPercentage(Bank bank, Currency to);
}
Loading