Ruby Practice
Practicum - Currency Converter
Overview
In this practicum you will implement a currency converter: Given a from
currency, an amount in that currency, and a to
currency, you will provide the equivalent amount in the to
currency. Input to the program consists of lines containing
comma-separated values (CSV) of the form:
from_currency,amount,to_currency
where from_currency
and to_currency
are strings identifying the currencies to use (USD for U.S. dollars,
CAN for Canadian dollars, EUR for Euros, AUD for Australian dollars,
and GBP for Great Britain Pounds sterling and amount
is the amount to be converted. For example:
EUR,50,HRK
converts 50 Euros to Kuna and
GPB,33.75,USD
converts 33.75 British pounds to U.S. dollars.
You may assume that all input lines are well-formed
(have exactly 3 fields) and syntactically correct (the first and
third fields are supported currency Strings and the second
field is a String representing a number - use the to_f
method on String to convert this to a numeric value).
Program
Specification & Testing
You will first complete the three methods in currency_util.rb:
parse(line)
Parse the String line,
containing a CSV line, into an String array using the String
split
method with a comma delimiter (pattern) and return this array. Note that
this must work for any
String with any number of
fields.
to_dollars(currency,amount)
Convert the amount of the given currency to dollars. A hash,
mapping each currency to the dollar conversion rate, is provided.
Multiplying the amount by the appropriate rate gives the equivalent
dollars. Use the round
method in Float to round the result to 2 fractional digits.
from_dollars(currency,amount)
Convert the amount of dollars to the given currency. A hash,
mapping each currency to the dollar conversion rate, is provided.
Dividing the dollar amount by the appropriate rate gives the
equivalent amount of the currency. Use the round
method in Float to round the result to 2 fractional digits.
You must also test these three methods - see the test framework in currency_test.rb.
- You must add at
least one
more test each for to_dollars
and from_dollars;
these tests must
exercise behavior different from the ones provided. They do NOT
need to be as thorough in testing all cases as are the provided tests.
- You must add at least two
distinct tests for parse
testing two (2) different
behaviors. Input lines that all have three fields consisting of two
strings and a number are not
different from a behavior point of view, even if the strings and
numbers are different.
After completing the methods and unit testing them, you will
complete the main "driver" program in currency.rb
which provides for processing conversion requests from standard in and
displaying results to standard out.
Instructions
Download the files linked to above into a new directory named Practicum-1
located at the root directory of your Git repository (not in a sub-directory!).
For reference, here are links to the files again:
Your code should follow proper Ruby style guidelines (2 space
indentation per level; 80 characters max. per line, etc.). You need not
add any further documentation.
Submission
You only need to submit these files to your repository if required by your instructor.
- The three files must be in directory Practicum-1
at the top of your Git repository.
- You must add and commit and push your changes to have them show up
in the repository.
- We suggest adding. committing, and pushing after
each significant
addition or change
to maximize potential credit.