From now on, we would like to handle operations between amounts in different currencies:
- 5 USD + 10 EUR = 17 USD
- 1 USD + 1100 KRW = 2200 KRW
We need to bring a new business concept to our implementation: a Portfolio
.
Basically, a Portfolio
contains a list of amounts in various currencies.
Taking our first example above, it could contain 5 USD
and 10 EUR
.
We should be able to evaluate the full Portfolio
amount in a given currency:
USD
=>17 USD
EUR
=>14,1 EUR
KRW
=>18940 KRW
- etc.
Your mission in this iteration is to implement the Portfolio
.
Don't forget to write your code using TDD
.
Small reminder regarding it: the Red
step is either a test failure or a compilation error.
Please take some time to read how to generate Code From Usage.
There are different strategies when you want to go from a red
state to a green
state:
Fake it until you make it
Obvious implementation
Triangulation
We recommend to use Fake it until you make it
for this first one.
Write a new failing test.
Use Fake it until you make it
once again.
Now that you have 2 test cases, you can now use Triangulation
and refactor the production code to get you closer to the "final" solution.
Your 2 test cases will act as a safety net for your refactoring.
Always think about what could happen at the edge of your system.
- From your code implementation, what happens if we add in the same
Portfolio
many amounts of the same currency?- We need to increase our confidence by adding a new test on it:
5 USD + 10 EUR + 4 EUR = 21.8 USD
for example
- We need to increase our confidence by adding a new test on it:
- Regarding the current implementation, we may improve error handling and throws a global exception with the details of all the missing exchange rates
- Instead of throwing an
Exception
on the first one missing
- Instead of throwing an
Write this new [failing] test.
If we have multiple missing exchange rates we return the information only for the first missing one... Let's improve it: write a new test that will drive you to the final implementation.
The test could look like this: 1 EUR + 1 USD + 1 KRW = ? EUR
During this iteration we have implemented a Portfolio
that allows to add different amounts in different currencies.
Take a look at your test cases, any duplication?
Always put the same attention on your test code than on your production code