forked from leavengood/donation_tracker
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
ee2108a
commit ee00f73
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} | ||
|
||
//============================================================================== |