Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dean Fitzgerald - Currency Converter #7

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions currency_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from decimal import Decimal

rates = [("USD", "EUR", 0.74), ("EUR", "JPY", 145.949)]
cents = Decimal("0.01")


def convert(rates, value, from_code, to_code):
"""Take a monetary value in one currency and return a value in another
currency"""
if from_code == to_code:
return str(value) + " " + to_code
else:
returned_list = [(origin, destination, rate)
for (origin, destination, rate)
in rates if from_code == origin]
returned_tuple = returned_list[0]
returned_rate = returned_tuple[2]
new_value = returned_rate * value
return str(new_value) + " " + to_code


def convert_reverse(rates, value, from_code, to_code):
returned_list = [(destination, origin, rate)
for (destination, origin, rate)
in rates if to_code == destination]
returned_tuple = returned_list[0]
returned_rate = returned_tuple[2]
new_value = Decimal(value / returned_rate).quantize(cents)
return str(new_value) + " " + to_code
10 changes: 10 additions & 0 deletions more_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@



# returned_list = [(origin, destination, rate) for (origin, destination, rate)
# in rates if from_code == origin]
# returned_tuple = returned_list[0]
# returned_rate = returned_tuple[2]
# new_value = returned_rate * value
# return new_value
# #return ("{} {}".format(return_value, to_code))
24 changes: 24 additions & 0 deletions test_currency_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import currency_converter as cc
rates = [("USD", "EUR", 0.74), ("EUR", "JPY", 145.949)]


def test_one_to_one():
assert cc.convert(rates, 1, "USD", "USD") == "1 USD"


def test_conversion_simple():
assert cc.convert(rates, 1, "USD", "EUR") == "0.74 EUR"


def test_conversion():
assert cc.convert(rates, 0.5, "USD", "EUR") == "0.37 EUR"
assert cc.convert(rates, 2, "EUR", "JPY") == "291.898 JPY"


def test_conversion_2_dollars():
assert cc.convert(rates, 2, "USD", "EUR") == "1.48 EUR"


def test_reverse():
assert cc.convert_reverse(rates, 2, "EUR", "USD") == "2.70 USD"
assert cc.convert_reverse(rates, 145.949, "JPY", "EUR") == "1.00 EUR"