Skip to content

Commit

Permalink
remove panic
Browse files Browse the repository at this point in the history
  • Loading branch information
AyoobMH authored and Rhymond committed Jul 29, 2024
1 parent 31c7a88 commit f9d20a6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
32 changes: 16 additions & 16 deletions money.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,53 +199,53 @@ 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 len(om) == 0 {
panic("At least one Money is required to add")
func (m *Money) Add(ms ...*Money) (*Money, error) {
if len(ms) == 0 {
return m, nil
}

k := New(0, m.currency.Code)

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

k.amount = mutate.calc.add(k.amount, om[i].amount)
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 len(om) == 0 {
panic("At least one Money is required to subtract")
func (m *Money) Subtract(ms ...*Money) (*Money, error) {
if len(ms) == 0 {
return m, nil
}

k := New(0, m.currency.Code)

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

k.amount = mutate.calc.add(k.amount, om[i].amount)
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 {
if len(mul) == 0 {
func (m *Money) Multiply(muls ...int64) *Money {
if len(muls) == 0 {
panic("At least one multiplier is required to multiply")
}

k := New(1, m.currency.Code)

for i := 0; i < len(mul); i++ {
k.amount = mutate.calc.multiply(k.amount, mul[i])
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}
Expand Down
26 changes: 26 additions & 0 deletions money_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,19 @@ func TestMoney_Add3(t *testing.T) {
}
}

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 @@ -436,6 +449,19 @@ func TestMoney_Subtract3(t *testing.T) {
}
}

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 Down

0 comments on commit f9d20a6

Please sign in to comment.