O'Reilly logo

Test Driven Development: By Example by Kent Beck

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 16. Abstraction, Finally

$5 + 10 CHF = $10 if rate is 2:1$5 + $5 = $10Return Money from $5 + $5Bank.reduce(Money)Reduce Money with conversionReduce(Bank, String)Sum.plusExpression.times

We need to implement Sum.plus() to finish Expression.plus, and then we need Expression.times(), and then we're finished with the whole example. Here's the test for Sum.plus():

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);
					}
				

We could have created a Sum by adding fiveBucks and tenFrancs ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required