Chapter 14. Change

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

Change is worth embracing (especially if you have a book out with “embrace change” in the title). Here, however, we are thinking about a much simpler form of change—we have two francs and we want a dollar. That sounds like a test case already:

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

When I convert francs to dollars, I divide by two. (We're still studiously ignoring all of those nasty numerical problems.) We can make the bar ...

Get Test Driven Development: By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.