Skip to content

Commit

Permalink
Add a CurrencyCount type with GrandTotal method
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
leavengood committed Nov 16, 2014
1 parent ee2108a commit ee00f73
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
8 changes: 8 additions & 0 deletions summary.go
Original file line number Diff line number Diff line change
@@ -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
}
38 changes: 38 additions & 0 deletions summary_test.go
Original file line number Diff line number Diff line change
@@ -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))
}

//==============================================================================

0 comments on commit ee00f73

Please sign in to comment.