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 all 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 @@ -199,26 +199,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(ms ...*Money) (*Money, error) {
if len(ms) == 0 {
return m, nil
}

return &Money{amount: mutate.calc.add(m.amount, om.amount), currency: m.currency}, nil
k := New(0, m.currency.Code)

for _, m2 := range ms {
if err := m.assertSameCurrency(m2); err != nil {
return nil, err
}

k.amount = mutate.calc.add(k.amount, m2.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 {
return nil, err
func (m *Money) Subtract(ms ...*Money) (*Money, error) {
if len(ms) == 0 {
return m, nil
}

return &Money{amount: mutate.calc.subtract(m.amount, om.amount), currency: m.currency}, nil
k := New(0, m.currency.Code)

for _, m2 := range ms {
if err := m.assertSameCurrency(m2); err != nil {
return nil, err
}

k.amount = mutate.calc.add(k.amount, m2.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(muls ...int64) *Money {
if len(muls) == 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 _, m2 := range muls {
k.amount = mutate.calc.multiply(k.amount, m2)
}

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
109 changes: 109 additions & 0 deletions money_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,49 @@ 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_Add4(t *testing.T) {
m := New(100, EUR)
r, err := m.Add()

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

if r.amount != 100 {
t.Error("Expected amount to be 100")
}
}

func TestMoney_Subtract(t *testing.T) {
tcs := []struct {
amount1 int64
Expand Down Expand Up @@ -376,6 +419,49 @@ 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_Subtract4(t *testing.T) {
m := New(100, EUR)
r, err := m.Subtract()

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

if r.amount != 100 {
t.Error("Expected amount to be 100")
}
}

func TestMoney_Multiply(t *testing.T) {
tcs := []struct {
amount int64
Expand All @@ -398,6 +484,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
Loading