Skip to Content
Learning Test-Driven Development
book

Learning Test-Driven Development

by Saleem Siddiqui
October 2021
Intermediate to advanced
277 pages
5h 48m
English
O'Reilly Media, Inc.
Content preview from Learning Test-Driven Development

Chapter 9. Currencies, Currencies, Everywhere

Small change, small wonders—these are the currency of my endurance and ultimately of my life.

Barbara Kingsolver

Here’s the current state of our evaluate feature vis-à-vis Money entities in a Portfolio:

  1. When converting a Money in a currency to the same currency, it returns the amount of the Money. This is correct: the exchange rate for any currency to itself is 1.

  2. In all other cases, the amount of the Money is multiplied by a fixed number (1.2). This is correct in a very limited sense: this rate ensures conversions from USD to EUR only. There is no way to modify this exchange rate or specify any other rate.

Our currency conversion code does one thing correctly and another thing almost correctly. It’s time to make it work correctly in both cases. In this chapter, we’ll introduce—at long last—the conversion of money from one currency into another using currency-specific exchange rates.

Making a Hash(map) of Things

What we need is a hashmap that allows us to look up exchange rates given a “from” currency and a “to” currency. The hashmap would be a representation of an exchange rate table we regularly see in banks and currency exchange counters at airports, as shown in Table 9-1.

Table 9-1. Exchange rate table
From To Rate

EUR

USD

1.2

USD

EUR

0.82

USD

KRW

1100

KRW

USD

0.00090

EUR

KRW

1344

KRW

EUR

0.00073

To read this table, use this pattern: given an amount in the “from” currency, multiply by the “rate” to get ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning Domain-Driven Design

Learning Domain-Driven Design

Vlad Khononov
Learning Go

Learning Go

Jon Bodner
Learning React, 2nd Edition

Learning React, 2nd Edition

Alex Banks, Eve Porcello

Publisher Resources

ISBN: 9781098106461Errata Page