Exercise: Money value object - 6
Program an unchangeable money value object to represent amounts of money in a specific currency. Only write the code that you need to fulfill the task at hand.
-
Create the basic structure for a money
value
object that supports the currencies EUR and USD. You could use anenum
to represent the currency. Always use anint
for the (cent) amount. Only add the methods to your value object that you really need. -
Please remember the automated tests! If you do not want to or cannot write automated tests, then write a control program that demonstrates all the features of your solution in a comprehensible way (console output is sufficient).
-
Create a method
addTo(Money $that): Money
to add two money amounts of the same currency. -
Create a method
multiplyWith(float $factor): Money
to multiply the amount of money. -
Add an
equals()
method that returns a boolean value. This method should only returntrue
if both amount and currency are the same in both compared value objects. -
Extend your solution to also support GBP and add a method to subtract money amounts.