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

Add, Subtract, Multiply Many #117

Merged
merged 6 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
50 changes: 40 additions & 10 deletions money.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,26 +198,56 @@ func (m *Money) Negative() *Money {
}

// Add returns new Money struct with value representing sum of Self and Other Money.
func (m *Money) Add(om *Money) (*Money, error) {
if err := m.assertSameCurrency(om); err != nil {
return nil, err
func (m *Money) Add(om ...*Money) (*Money, error) {
AyoobMH marked this conversation as resolved.
Show resolved Hide resolved
if len(om) == 0 {
panic("At least one Money is required to add")
AyoobMH marked this conversation as resolved.
Show resolved Hide resolved
}

return &Money{amount: mutate.calc.add(m.amount, om.amount), currency: m.currency}, nil
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)
}

AyoobMH marked this conversation as resolved.
Show resolved Hide resolved
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 {
return nil, err
func (m *Money) Subtract(om ...*Money) (*Money, error) {
if len(om) == 0 {
panic("At least one Money is required to subtract")
AyoobMH marked this conversation as resolved.
Show resolved Hide resolved
}

return &Money{amount: mutate.calc.subtract(m.amount, om.amount), currency: m.currency}, nil
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) Multiply(mul ...int64) *Money {
if len(mul) == 0 {
panic("At least one multiplier is required to multiply")
AyoobMH marked this conversation as resolved.
Show resolved Hide resolved
}

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.
Expand Down
83 changes: 83 additions & 0 deletions money_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,36 @@ func TestMoney_Add2(t *testing.T) {
}
}

func TestMoney_Add3(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.Add(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_Subtract(t *testing.T) {
tcs := []struct {
amount1 int64
Expand Down Expand Up @@ -357,6 +387,36 @@ func TestMoney_Subtract2(t *testing.T) {
}
}

func TestMoney_Subtract3(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.Subtract(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_Multiply(t *testing.T) {
tcs := []struct {
amount int64
Expand All @@ -379,6 +439,29 @@ func TestMoney_Multiply(t *testing.T) {
}
}

func TestMoney_Multiply2(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.Multiply(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