Description
This assignment will give you a chance to try out exceptions, while also learning about several interesting Java classes that can be used for financial calculations. In your 2019SPCS5004SV/assignment-5 GitHub repository is a class Money
that demonstrates their use.
This class assumes decimal currency, without unusual divisions like 1/5. Money
objects are immutable. The class uses BigDecimal for the amount, and Currency to indicate the currency of the amount. Storing units with a quantity makes it convenient to work with quantities that have multiple units. Other examples include distance in either English units (e.g. inches, feet, yards, miles), metric (e.g. milimeters, centimeters, meters, kilometers) or specialized units like angstroms (10-10 meters). This assignment will acquaint you with this technique
Currency
represents a currency. Currencies are identified by their ISO 4217 currency codes. The class is designed so that there is never more than one Currency
instance for any given currency. Therefore, there is no public constructor. You obtain a Currency
instance using the getInstance()
methods, including one that accepts a Locale
(e.g. Locale.US
) and one that accepts a country code (e.g. “en-US”).
BigDecimal
allows specifying amounts as double
, long
, String
, or as an unscaled value and an int
scale that can be negative or positive (e.g. 12000 or {12, -3}, 1234.56 or {123456, 2}). BigDecimal
can round to a number of decimal places. Money rounds to the number of decimal places for the Currency
using “banker’s rounding.”
This version of Money
creates instances that use the Currency
for the default Locale
. The constructors that include a Currency
are included but have only package visibility to facilitate testing. Your assignent is to full enable Money
operations in multiple currencies. There are several parts to this assignment.
-
Make the constructors that enable creating
Money
in multiple currencies public. -
Operations between currencies are not supported. You should create a new nested static class
MismatchedCurrencyException
that can be thrown in every Money operation on two or more currencies. You will be surprised to learn that the base for this exception will beRuntimeException
. This is one of those rare “exceptions” to the general rule that all developer-created exceptions should be checked exceptions.Money
is a lot likeInteger
,Double
, orBigDecimal
where it is also considered burdensome to declare and catch everyArithmeticException
orNumberFormatException
that can be thrown. So enjoy this rare opportunity to work with an unchecked exception! (Do add @throws to methods that can throw the exception, however.) -
Add a new method
Money.asCurrency(Currency
that converts an instance to one for the specified
aCurrency, double exchangeRate)Currency
by multiplying the instance amount by the given exchange rate, and returns the new instance with that amount and currency. -
Enhance the unit tests in
Money_test
to create and work withMoney
in multiple currencies, and to ensure thatMismatchedCurrencyException
is thrown where appropriate. Be sure to add a unit test for the newasCurrency()
currency conversion method.
Note: The unit tests on lines 44 and 55 work correctly under Java 8. However, under Java 10, the currency symbol for the U.S. Dollar in the UK locale has changed from “USD” to “US$”. If these tests fail on your computer, you should change the expected value to “US$”.