Assignment 6

1. We need a Money class for financial transactions. Each Money object needs to know its currency and a magnitude. While developing the class for this problem you can use a string for the currency. One should be able to create money using:

```Money currency: ‘USD’ amount: 12.34s2
```
Or
```
12.34s2 asMoney: ‘USD’.
```
Money acts like an arithmetic object as you can add money, multiply money by numbers and divide money by money to get a number. For problem one just support adding money of the same currency. The following operations are valid and need to be supported:

 Operation Result Money * Number Money Money + Money Money Money - Money Money Money / Money Number Money / Number Money

When an operation results in Money return a new Money object with the result. Once a Money object is created do not change the amount or currency of the object. One problem in Money arithmetic operations is round off error. What is the correct result of 0.03 USD / 2? There is no standard solution to this issue. For this class we will just use the default behavior of FixedPoint numbers.

The following operations are not valid and should result in an exception being raised.

 Invalid Operations Money - Number Number / Money Money * Money Money + Number

Problem one is primarily an exercise in double-dispatching. Implement the = operator to return true only if two Money objects are of the same currency and have the same value. Don’t forget the hash operator. Money objects should support the operators >, <, <=, >= on objects with the same currency.

Don’t forget to write unit tests for your Money class.

2. Now to add currency conversion. We need a method convertTo: to the Money class that returns a new Money object with the amount converted to a different currency. So for example:

```| dollar euros |
dollar := Money currency: ‘USD’ amount: 1.0s2.
euros := dollar convertTo: ‘EUR’.
```

Conversion of currency raises a number of issues.

First the currency exchange rates to change frequently. The table below contains the currencies we will support, the symbols for each currencies and the current exchange rate (as of Friday March 21).

Second we have to address the issue of the fractional digits supported by the currency. For example in this country ordinary currency operations do not handle amounts less than a cent. Converting currency can result in amounts less than supported by the currency. In all currency exchanges round down to smallest unit supported by the currency. (See the FixedPoint class comment.) In the example above using the rates below the amount in euros variable should be 0.94.

Third is number of exchange rates needed to support N different currencies. The table below contains 9 different currencies, which leads to 9*8 = 72 different pairs of possible currencies change rates. The number of these rates can be reduced to 9 by converting all currencies to USD first. For example to convert 2 CAD to Euros, first convert

```2 CAD / 1.4905 = 1.34 USD
```

The convert the 1.34 USD to Euros

```1.34 USD * 0.948317 = 1.27 EUR
```
This does allow currency traders to pocket the round off error twice.

Fourth is that different currencies support different number of fractional digits. For example the Japanese Yen supports no fractional digits.

You may find it useful to use a Currency object to hold information about a currency.

Modify your Money code so +, - and / work with different currencies. For example:

```| dollar euros |
dollar := Money currency: ‘USD’ amount: 1.0s2.
euros := Money currency: ‘EUR amount: 0.95.
dollar + euros
```
The operation dollar + euros should return the sum in USD. The operation euros + dollar should return the sum in EUR. That is the left operand determines the currency of the result.

Provide unit tests for your currency exchanges.

 Currency Symbol Fractional Digits 1 USD Equals Canadian Dollar CAD 2 1.4905 Chinese Yuan CNY 2 9.2773 EURO EUR 2 0.948317 Indian Rupee INR 2 47.66 Japanese Yen JPY 0 121.42 Mexican Peso MXN 2 10.7575 Pakistan Rupee PKR 2 58.0958 Thailand THB 2 43.07 US Dollar USD 2 1

What to turn in

Turn in one copy of the source code for your tests, classes you create and methods you add to exiting classes. The best way to do this is to put all your code in a parcel. To insure that you have all the code, save the parcel, parcel it in to a clean image and run your tests. If you do not turn in all your code you will lose points and will not add them later if you complain that you just forgot. Keeping all your code together is part of this assignment. If you print a file out of your code do not use the XML file out. You will 30 points if you do. Do not separate the code into code for problem one and problem two. You may end up changing the code from problem one to meet problem two. Just turn in the final version of the code. Please avoid using Word to edit and printout your source code. Word changes the formatting and capitalization of words. If this happens you will lose points.