From ee00f736cc3b2ef753e99ca8de99d16f01a9cd51 Mon Sep 17 00:00:00 2001 From: Ryan Leavengood Date: Sun, 16 Nov 2014 16:57:57 -0500 Subject: [PATCH] Add a CurrencyCount type with GrandTotal method This hold totals in different currencies (currently expected to just be USD and EUR), and GrandTotal provides a total in USD given a conversion rate from EUR to USD. --- summary.go | 8 ++++++++ summary_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 summary.go create mode 100644 summary_test.go diff --git a/summary.go b/summary.go new file mode 100644 index 0000000..660eec3 --- /dev/null +++ b/summary.go @@ -0,0 +1,8 @@ +package main + +// Use a map because of multiple currencies +type CurrencyCount map[string]float32 + +func (c CurrencyCount) GrandTotal(eurToUsdRate float32) float32 { + return c["USD"] + c["EUR"]*eurToUsdRate +} diff --git a/summary_test.go b/summary_test.go new file mode 100644 index 0000000..322d5a3 --- /dev/null +++ b/summary_test.go @@ -0,0 +1,38 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +//============================================================================== +// CurrencyCount.GrandTotal +//============================================================================== + +const eurToUsdRate = 1.25 + +func TestGrandTotalWithEmptyMap(t *testing.T) { + cc := make(CurrencyCount) + + assert.Equal(t, 0, cc.GrandTotal(eurToUsdRate)) +} + +func TestGrandTotalWithJustUSD(t *testing.T) { + cc := CurrencyCount{ + "USD": 34.56, + } + + assert.Equal(t, 34.56, cc.GrandTotal(eurToUsdRate)) +} + +func TestGrandTotalWithJustUSDAndEUR(t *testing.T) { + cc := CurrencyCount{ + "USD": 34.56, + "EUR": 10.00, + } + + assert.Equal(t, 47.06, cc.GrandTotal(eurToUsdRate)) +} + +//==============================================================================