皇家方舟

TDD 程序代码(13)

TestMoney.java:
package com;

import junit.framework.TestCase;

public class TestMoney extends TestCase {
 public void testMultiplication() {
  Money five = Money.dollar(5);
  assertEquals(Money.dollar(10), five.times(2));
  assertEquals(Money.dollar(15), five.times(3));
 }

 public void testEqualsObject() {
  assertTrue(Money.dollar(5).equals(Money.dollar(5)));
  assertFalse(Money.dollar(5).equals(Money.dollar(6)));
  assertFalse(Money.franc(5).equals(Money.dollar(5)));
 }

 public void testCurrency() {
  assertEquals("USD", Money.dollar(1).currency());
  assertEquals("CHF", Money.franc(1).currency());
 }

 public void testSimpleAddition() {
  Money five = Money.dollar(5);
  Expression result = five.plus(five);
  Sum sum = (Sum) result;
  Bank bank = new Bank();
  Money reduced = bank.reduce(sum, "USD");
  assertEquals(Money.dollar(10), reduced);
 }

 public void testPlusReturnSum() {
  Money five = Money.dollar(5);
  Expression result = five.plus(five);
  Sum sum = (Sum) result;
  assertEquals(five, sum.augend);
  assertEquals(five, sum.addend);
 }

 public void testReduceSum() {
  Expression sum = new Sum(Money.dollar(3), Money.dollar(4));
  Bank bank = new Bank();
  Money result = bank.reduce(sum, "USD");
  assertEquals(Money.dollar(7), result);
 }
 public void testReduceMoney(){
  Bank bank = new Bank();
  Money result = bank.reduce(Money.dollar(1), "USD");
  assertEquals(Money.dollar(1),result);
 }
}



posted on 2006-11-06 23:50 阿辉 阅读(477) 评论(9)  编辑  收藏 所属分类: 学习日志

Feedback

# re: TDD 程序代码(13) 2006-11-06 23:50 阿辉

Money.java:
package com;

public class Money implements Expression {

protected String currency;

protected int amount;

public Money(int amount, String currency) {
this.amount = amount;
this.currency = currency;
}

public Money(int amount) {
this.amount = amount;
}

public boolean equals(Object object) {
Money money = (Money) object;
return this.amount == money.amount
&& this.currency.equals(money.currency());
}

public static Money dollar(int amount) {
return new Money(amount, "USD");
}

public static Money franc(int amount) {
return new Money(amount, "CHF");
}

public Money times(int mutiplier) {
return new Money(this.amount * mutiplier, this.currency);
}

public String currency() {
return currency;
}

public String toString() {
return this.currency + "" + this.amount;
}

public Expression plus(Money addend) {
return new Sum(this, addend);
}
public Money reduce(String currency){
return this;
}
}  回复  更多评论   

# re: TDD 程序代码(13) 2006-11-06 23:50 阿辉

Bank.java:
package com;

public class Bank {

public Money reduce(Expression expression, String currency) {
return expression.reduce(currency);
}

}
  回复  更多评论   

# re: TDD 程序代码(13) 2006-11-06 23:51 阿辉

Expression.java:
package com;

public interface Expression {
Money reduce(String currency);
}
  回复  更多评论   

# re: TDD 程序代码(13) 2006-11-06 23:51 阿辉

Sum.java:

package com;

public class Sum implements Expression {
Money augend;

Money addend;

public Sum(Money augend, Money addend) {
this.augend = augend;
this.addend = addend;
}

public Money reduce(String currency) {
int amount = augend.amount + addend.amount;
return new Money(amount, currency);
}
}
  回复  更多评论   

# TestMoney.java 最后一章 2006-11-09 15:58 皇家方舟

package com;


import junit.framework.TestCase;


public class TestMoney extends TestCase
{
public void testMultiplication()
{
Money five = Money.dollar(5);
assertEquals(Money.dollar(10), five.times(2));
assertEquals(Money.dollar(15), five.times(3));
}


public void testEqualsObject()
{
assertTrue(Money.dollar(5).equals(Money.dollar(5)));
assertFalse(Money.dollar(5).equals(Money.dollar(6)));
assertFalse(Money.franc(5).equals(Money.dollar(5)));
}


public void testCurrency()
{
assertEquals("USD", Money.dollar(1).currency());
assertEquals("CHF", Money.franc(1).currency());
}


public void testSimpleAddition()
{
Money five = Money.dollar(5);
Expression result = five.plus(five);
Sum sum = (Sum)result;
Bank bank = new Bank();
Money reduced = bank.reduce(sum, "USD");
assertEquals(Money.dollar(10), reduced);
}


public void testPlusReturnSum()
{
Money five = Money.dollar(5);
Expression result = five.plus(five);
Sum sum = (Sum)result;
assertEquals(five, sum.augend);
assertEquals(five, sum.addend);
}


public void testReduceSum()
{
Expression sum = new Sum(Money.dollar(3), Money.dollar(4));
Bank bank = new Bank();
Money result = bank.reduce(sum, "USD");
assertEquals(Money.dollar(7), result);
}


public void testReduceMoney()
{
Bank bank = new Bank();
Money result = bank.reduce(Money.dollar(1), "USD");
assertEquals(Money.dollar(1), result);
}


public void testReduceMoneyDifferentCurrency()
{
Bank bank = new Bank();
bank.addRate("CHF", "USD", 2);
Money result = bank.reduce(Money.franc(4), "USD");
assertEquals(Money.dollar(4 / 2), result);
}


public void testIdentityRate()
{
assertEquals(1, new Bank().rate("USD", "USD"));
}


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");
assertEquals(Money.dollar(10), result);
}


public void testSumplusMoney()
{
Expression fiveBucks = Money.dollar(5);
Expression tenFrancs = Money.franc(10);
Bank bank = new Bank();
bank.addRate("CHF", "USD", 2);
Expression sum = new Sum(fiveBucks, tenFrancs).plus(fiveBucks);
Money result = bank.reduce(sum, "USD");
assertEquals(Money.dollar(15), result);
}


public void testSumTimes()
{
Expression fiveBucks = Money.dollar(5);
Expression tenFrancs = Money.franc(10);
Bank bank = new Bank();
bank.addRate("CHF", "USD", 2);
Expression sum = new Sum(fiveBucks, tenFrancs).times(2);
Money result = bank.reduce(sum, "USD");
assertEquals(Money.dollar(20), result);
}
}
  回复  更多评论   

# Expression.java 最后一章 2006-11-09 15:58 皇家方舟

package com;


public interface Expression
{
Money reduce(Bank bank, String currency);


Expression plus(Expression tenFrancs);


Expression times(int multiplier);
}
  回复  更多评论   

# Money.java 最后一章 2006-11-09 15:59 皇家方舟

package com;


public class Money implements Expression
{

protected String currency;

protected int amount;


public Money(int amount, String currency)
{
this.amount = amount;
this.currency = currency;
}


public Money(int amount)
{
this.amount = amount;
}


public boolean equals(Object object)
{
Money money = (Money)object;
return this.amount == money.amount
&& this.currency.equals(money.currency());
}


public static Money dollar(int amount)
{
return new Money(amount, "USD");
}


public static Money franc(int amount)
{
return new Money(amount, "CHF");
}


public Expression times(int mutiplier)
{
return new Money(this.amount * mutiplier, this.currency);
}


public String currency()
{
return currency;
}


public String toString()
{
return this.currency + "" + this.amount;
}


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


public Money reduce(Bank bank, String to)
{
int rate = bank.rate(this.currency, to);
return new Money(amount / rate, to);
}
}
  回复  更多评论   

# Bank.java 最后一章 2006-11-09 15:59 皇家方舟

package com;


import java.util.Hashtable;


public class Bank
{
private Hashtable<Pair, Integer> rates = new Hashtable<Pair, Integer>();


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


public void addRate(String from, String to, int rate)
{
rates.put(new Pair(from, to), new Integer(rate));
}


int rate(String from, String to)
{
if(from.equals(to))
return 1;
Integer rate = (Integer)rates.get(new Pair(from, to));
return rate.intValue();
}


private class Pair
{
private String from;
private String to;


Pair(String from, String to)
{
this.from = from;
this.to = to;
}


public boolean equals(Object object)
{
Pair pair = (Pair)object;
return from.equals(pair.from) && to.equals(pair.to);
}


public int hashCode()
{
return 0;
}
}
}
  回复  更多评论   

# Sum.java 最后一章 2006-11-09 16:00 皇家方舟

package com;


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 new Sum(this, addend);
}


public Expression times(int multiplier)
{
return new Sum(augend.times(multiplier), addend.times(multiplier));
}
}
  回复  更多评论   



只有注册用户登录后才能发表评论。


网站导航:
 

My Links

Blog Stats

常用链接

留言簿(1)

随笔分类

随笔档案

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜