드디어 이 모든 작업의 시초인 $5 + 10CHF에 대한 테스트를 추가할 준비가 됐다.
@Test
public void testMixedAddition() {
Expression fiveBucks = Money.dollar(5);
Expression tenFrancs = Money.franc(10);
Bank bank = new Bank();
bank.addRate("CHF", "USD", 2);
Money result = bank.reduce(fiveBucks(tenFrancs), "USD");
assertEquals(Money.dollar(10), result);
}
하지만 현재는 에러가 많다.
천천히 하나씩 고쳐나가자.
@Test
public void testMixedAddition() {
Money fiveBucks = Money.dollar(5);
Money tenFrancs = Money.franc(10);
Bank bank = new Bank();
bank.addRate("CHF", "USD", 2);
Money result = bank.reduce(fiveBucks(tenFrancs), "USD");
assertEquals(Money.dollar(10), result);
}
// Sum
public Money reduce(Bank bank, String to) {
int amount = augend.reduce(bank, to).amount + addend.reduce(bank, to).amount;
return new Money(amount, to);
}
통과한다. 이제 Expression이어야 하는 Money들을 없앨 수 있다.
@Test
public void testMixedAddition() {
Expression fiveBucks = Money.dollar(5);
Expression tenFrancs = Money.franc(10);
Bank bank = new Bank();
bank.addRate("CHF", "USD", 2);
Money result = bank.reduce(fiveBucks.plus(tenFrancs), "USD", bank);
assertEquals(Money.dollar(10), result);
}
// Money
class Money implements Expression{
protected int amount;
protected String currency;
Money(int amount, String currency) {
this.amount = amount;
this.currency = currency;
}
public String toString() {
return amount + " " + currency;
}
Expression times(int multiplier) {
return new Money(amount * multiplier, currency);
}
String currency() {
return currency;
}
public boolean equals(Object object) {
Money money = (Money) object;
return amount == money.amount && currency().equals(money.currency());
}
public Money reduce(Bank bank, String to) {
int rate = bank.rate(currency, to);
return new Money(amount / rate, to);
}
public Expression plus(Expression addend) {
return new Sum(this, addend);
}
static Money dollar(int amount) {
return new Money(amount, "USD");
}
static Money franc(int amount) {
return new Money(amount, "CHF");
}
}
// Sum
public class Sum implements Expression{
Expression augend;
Expression addend;
public Sum(Expression augend, Expression addend) {
this.augend = augend;
this.addend = addend;
}
public Money reduce(Bank bank, String to) {
int amount = augend.reduce(bank, to).amount + addend.reduce(bank, to).amount;
return new Money(amount, to);
}
public Expression plus(Expression addend) {
return null;
}
}
// Expression
public interface Expression {
Money reduce(Bank bank, String to);
Expression plus(Expression addend);
}
우리는 지금까지