Currency Conversion
Mix.install([
{:kino, github: "livebook-dev/kino", override: true},
{:kino_lab, "~> 0.1.0-dev", github: "jonatanklosko/kino_lab"},
{:vega_lite, "~> 0.1.4"},
{:kino_vega_lite, "~> 0.1.1"},
{:benchee, "~> 0.1"},
{:ecto, "~> 3.7"},
{:math, "~> 0.7.0"},
{:faker, "~> 0.17.0"},
{:utils, path: "#{__DIR__}/../utils"},
{:tested_cell, git: "https://github.com/BrooklinJazz/tested_cell"}
])
Navigation
Currency Conversion
You are building an app which accepts payments. Originally, your app was built assuming it would only accept a single currency, however it expands to accept multiple currencies.
You are tasked with the assignment of converting one currency to another according to the following conversion rates.
-
Canadian Dollar (
:CAD
): $1.00 -
US Dollar (
:US
): $1.29 -
European Euro (
:EUR
): $1.39
While not a perfect solution, you may round currency conversions using round/1
.
For example, 500
:EUR
would become 360
:CAD
.
round(500 / 1.39)
Remember that currency should not be stored in a float,
instead money will be stored in a Money
struct.
> “If I had a dime for every time I’ve seen someone use FLOAT to store currency, I’d have $999.997634” – Bill Karwin
-
Create a
Money
struct with:amount
and:currency
. -
Create a
convert/2
function to convert a money struct from one currency to another. -
Create an
add/2
function to add one money struct of the same currency to another. -
Create a
subtract/2
function to subtract one money struct of the same currency to another. -
Create a
multiply
function to multiply one money struct by a number.
Examples
five_eur = Money.new(500, :EUR)
%Money{amount: 500, currency: :EUR}
Money.convert(five_eur, :CAD)
%Money{amount: 695, currency: :EUR}
ten_eur = Money.add(five_eur, five_eur)
%Money{amount: 10_00, currency: :EUR}
hundred_eur = Money.multiply(ten_eur, 10)
%Money{amount: 100_00, currency: :EUR}
ninety_nine_eur = Money.subtract(hundred_eur, ten_eur)
%Money{amount: 90, currency: :EUR}
Money.add(%Money{amount: 500, :EUR}, %Money{amount: 500, :CAD})
** (FunctionClauseError) no function clause matching in Money.add/2
Enter your solution below.
defmodule Money do
defstruct []
def new(amount, currency) do
end
def convert(money, currency) do
end
def add(money1, money2) do
end
def multiply(money1, money2) do
end
def subtract(money1, money2) do
end
end
Utils.feedback(:money, Money)
This exercise is inspired by the Elixir Money library.
Commit Your Progress
Run the following in your command line from the project folder to track and save your progress in a Git commit.
$ git add .
$ git commit -m "finish currency conversion exercise"