Skip to content

Commit

Permalink
Add Add,Subtract,Multiply many
Browse files Browse the repository at this point in the history
  • Loading branch information
AyoobMH authored and Rhymond committed Jul 29, 2024
1 parent bc15d4d commit 5009dbd
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
38 changes: 38 additions & 0 deletions money.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,20 @@ func (m *Money) Add(om *Money) (*Money, error) {
return &Money{amount: mutate.calc.add(m.amount, om.amount), currency: m.currency}, nil
}

func (m *Money) AddMany(om ...*Money) (*Money, error) {
k := New(0, m.currency.Code)

for i := 0; i < len(om); i++ {
if err := m.assertSameCurrency(om[i]); err != nil {
return nil, err
}

k.amount = mutate.calc.add(k.amount, om[i].amount)
}

return &Money{amount: mutate.calc.add(m.amount, k.amount), currency: m.currency}, nil
}

// Subtract returns new Money struct with value representing difference of Self and Other Money.
func (m *Money) Subtract(om *Money) (*Money, error) {
if err := m.assertSameCurrency(om); err != nil {
Expand All @@ -216,11 +230,35 @@ func (m *Money) Subtract(om *Money) (*Money, error) {
return &Money{amount: mutate.calc.subtract(m.amount, om.amount), currency: m.currency}, nil
}

func (m *Money) SubtractMany(om ...*Money) (*Money, error) {
k := New(0, m.currency.Code)

for i := 0; i < len(om); i++ {
if err := m.assertSameCurrency(om[i]); err != nil {
return nil, err
}

k.amount = mutate.calc.add(k.amount, om[i].amount)
}

return &Money{amount: mutate.calc.subtract(m.amount, k.amount), currency: m.currency}, nil
}

// Multiply returns new Money struct with value representing Self multiplied value by multiplier.
func (m *Money) Multiply(mul int64) *Money {
return &Money{amount: mutate.calc.multiply(m.amount, mul), currency: m.currency}
}

func (m *Money) MultiplyMany(mul ...int64) *Money {
k := New(1, m.currency.Code)

for i := 0; i < len(mul); i++ {
k.amount = mutate.calc.multiply(k.amount, mul[i])
}

return &Money{amount: mutate.calc.multiply(m.amount, k.amount), currency: m.currency}
}

// Round returns new Money struct with value rounded to nearest zero.
func (m *Money) Round() *Money {
return &Money{amount: mutate.calc.round(m.amount, m.currency.Fraction), currency: m.currency}
Expand Down
105 changes: 105 additions & 0 deletions money_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,47 @@ func TestMoney_Add2(t *testing.T) {
}
}

func TestMoney_AddMany(t *testing.T) {
tcs := []struct {
amount1 int64
amount2 int64
amount3 int64
expected int64
}{
{5, 5, 3, 13},
{10, 5, 4, 19},
{1, -1, 2, 2},
{3, -1, -4, -2},
}

for _, tc := range tcs {
mon1 := New(tc.amount1, EUR)
mon2 := New(tc.amount2, EUR)
mon3 := New(tc.amount3, EUR)
r, err := mon1.AddMany(mon2, mon3)

if err != nil {
t.Error(err)
}

if r.Amount() != tc.expected {
t.Errorf("Expected %d + %d + %d = %d got %d", tc.amount1, tc.amount2, tc.amount3,
tc.expected, r.amount)
}
}
}

func TestMoney_AddMany2(t *testing.T) {
mon1 := New(100, GBP)
mon2 := New(100, EUR)
mon3 := New(100, GBP)
r, err := mon1.AddMany(mon2, mon3)

if r != nil || err == nil {
t.Error("Expected err")
}
}

func TestMoney_Subtract(t *testing.T) {
tcs := []struct {
amount1 int64
Expand Down Expand Up @@ -376,6 +417,47 @@ func TestMoney_Subtract2(t *testing.T) {
}
}

func TestMoney_SubtractMany(t *testing.T) {
tcs := []struct {
amount1 int64
amount2 int64
amount3 int64
expected int64
}{
{5, 5, 3, -3},
{10, -5, 4, 11},
{1, -1, 2, 0},
{7, 1, -4, 10},
}

for _, tc := range tcs {
mon1 := New(tc.amount1, EUR)
mon2 := New(tc.amount2, EUR)
mon3 := New(tc.amount3, EUR)
r, err := mon1.SubtractMany(mon2, mon3)

if err != nil {
t.Error(err)
}

if r.Amount() != tc.expected {
t.Errorf("Expected (%d) - (%d) - (%d) = %d got %d", tc.amount1, tc.amount2, tc.amount3,
tc.expected, r.amount)
}
}
}

func TestMoney_SubtractMany2(t *testing.T) {
mon1 := New(100, GBP)
mon2 := New(100, EUR)
mon3 := New(100, GBP)
r, err := mon1.SubtractMany(mon2, mon3)

if r != nil || err == nil {
t.Error("Expected err")
}
}

func TestMoney_Multiply(t *testing.T) {
tcs := []struct {
amount int64
Expand All @@ -398,6 +480,29 @@ func TestMoney_Multiply(t *testing.T) {
}
}

func TestMoney_MultiplyMany(t *testing.T) {
tcs := []struct {
amount1 int64
amount2 int64
amount3 int64
expected int64
}{
{5, 5, 5, 125},
{10, 5, -3, -150},
{1, -1, 6, -6},
{1, 0, 2, 0},
}

for _, tc := range tcs {
mon1 := New(tc.amount1, EUR)
r := mon1.MultiplyMany(tc.amount2, tc.amount3)

if r.amount != tc.expected {
t.Errorf("Expected %d * %d * %d = %d got %d", tc.amount1, tc.amount2, tc.amount3, tc.expected, r.amount)
}
}
}

func TestMoney_Round(t *testing.T) {
tcs := []struct {
amount int64
Expand Down

0 comments on commit 5009dbd

Please sign in to comment.