Money

Implementing a decent Money class is tricky. It must include the amount of money and the currency. This means that a service for obtaining the latest exchange rates must also be provided. Of course Money and Currency are value types and so the canonical form for value types must be employed.

Demo

Here's the test driver: TestMoney.scala

Here's the output:

m1 + m2 = 192.59259259259258 EUR
m3 + m2 = 7242.857142857143 INR
m1 < m2 = false

Notes

·       If a and b are money objects, then a + b has the same currency as a. This means it must multiply b's amount by the exchange rate.

Design

Notes

Exchange rates change minute by minute, so we define conversion calculator as a trait that could be implemented by an adapter for such a service.

To test our code we provide a mock converter that uses a map that gives the exchange rates to US dollars: MockConverter.scala.

Implementation

Complete the implementation of the design.