Skip to content

Commit

Permalink
Convert Amount struct into type alias. (#113)
Browse files Browse the repository at this point in the history
Convert amount field to AMount instead of point to amount.
Add IntelliJ files to .gitignore

Co-authored-by: Charly Fau <carlos.fau@altscore.ai>
  • Loading branch information
totemcaf and totemcaf committed Jul 25, 2022
1 parent ac8fc0b commit e9a67ed
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 85 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ _testmain.go
*.exe
*.test
*.prof

# InteeliJ files
.idea
*.iml
60 changes: 30 additions & 30 deletions calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,66 +4,66 @@ import "math"

type calculator struct{}

func (c *calculator) add(a, b *Amount) *Amount {
return &Amount{a.val + b.val}
func (c *calculator) add(a, b Amount) Amount {
return a + b
}

func (c *calculator) subtract(a, b *Amount) *Amount {
return &Amount{a.val - b.val}
func (c *calculator) subtract(a, b Amount) Amount {
return a - b
}

func (c *calculator) multiply(a *Amount, m int64) *Amount {
return &Amount{a.val * m}
func (c *calculator) multiply(a Amount, m int64) Amount {
return a * m
}

func (c *calculator) divide(a *Amount, d int64) *Amount {
return &Amount{a.val / d}
func (c *calculator) divide(a Amount, d int64) Amount {
return a / d
}

func (c *calculator) modulus(a *Amount, d int64) *Amount {
return &Amount{a.val % d}
func (c *calculator) modulus(a Amount, d int64) Amount {
return a % d
}

func (c *calculator) allocate(a *Amount, r, s int) *Amount {
return &Amount{a.val * int64(r) / int64(s)}
func (c *calculator) allocate(a Amount, r, s int) Amount {
return a * int64(r) / int64(s)
}

func (c *calculator) absolute(a *Amount) *Amount {
if a.val < 0 {
return &Amount{-a.val}
func (c *calculator) absolute(a Amount) Amount {
if a < 0 {
return -a
}

return &Amount{a.val}
return a
}

func (c *calculator) negative(a *Amount) *Amount {
if a.val > 0 {
return &Amount{-a.val}
func (c *calculator) negative(a Amount) Amount {
if a > 0 {
return -a
}

return &Amount{a.val}
return a
}

func (c *calculator) round(a *Amount, e int) *Amount {
if a.val == 0 {
return &Amount{0}
func (c *calculator) round(a Amount, e int) Amount {
if a == 0 {
return 0
}

absam := c.absolute(a)
exp := int64(math.Pow(10, float64(e)))
m := absam.val % exp
m := absam % exp

if m > (exp / 2) {
absam.val += exp
absam += exp
}

absam.val = (absam.val / exp) * exp
absam = (absam / exp) * exp

if a.val < 0 {
a.val = -absam.val
if a < 0 {
a = -absam
} else {
a.val = absam.val
a = absam
}

return &Amount{a.val}
return a
}
42 changes: 20 additions & 22 deletions money.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
// money.UnmarshalJSON = func (m *Money, b []byte) error { ... }
// money.MarshalJSON = func (m Money) ([]byte, error) { ... }
var (
// UnmarshalJSONFunc is injection point of json.Unmarshaller for money.Money
// UnmarshalJSON is injection point of json.Unmarshaller for money.Money
UnmarshalJSON = defaultUnmarshalJSON
// MarshalJSONFunc is injection point of json.Marshaller for money.Money
// MarshalJSON is injection point of json.Marshaller for money.Money
MarshalJSON = defaultMarshalJSON

// ErrCurrencyMismatch happens when two compared Money don't have the same currency.
Expand Down Expand Up @@ -67,22 +67,20 @@ func defaultMarshalJSON(m Money) ([]byte, error) {
return buff.Bytes(), nil
}

// Amount is a datastructure that stores the amount being used for calculations.
type Amount struct {
val int64
}
// Amount is a data structure that stores the amount being used for calculations.
type Amount = int64

// Money represents monetary value information, stores
// currency and amount value.
type Money struct {
amount *Amount
amount Amount
currency *Currency
}

// New creates and returns new instance of Money.
func New(amount int64, code string) *Money {
return &Money{
amount: &Amount{val: amount},
amount: amount,
currency: newCurrency(code).get(),
}
}
Expand All @@ -94,7 +92,7 @@ func (m *Money) Currency() *Currency {

// Amount returns a copy of the internal monetary value as an int64.
func (m *Money) Amount() int64 {
return m.amount.val
return m.amount
}

// SameCurrency check if given Money is equals by currency.
Expand All @@ -112,9 +110,9 @@ func (m *Money) assertSameCurrency(om *Money) error {

func (m *Money) compare(om *Money) int {
switch {
case m.amount.val > om.amount.val:
case m.amount > om.amount:
return 1
case m.amount.val < om.amount.val:
case m.amount < om.amount:
return -1
}

Expand Down Expand Up @@ -168,17 +166,17 @@ func (m *Money) LessThanOrEqual(om *Money) (bool, error) {

// IsZero returns boolean of whether the value of Money is equals to zero.
func (m *Money) IsZero() bool {
return m.amount.val == 0
return m.amount == 0
}

// IsPositive returns boolean of whether the value of Money is positive.
func (m *Money) IsPositive() bool {
return m.amount.val > 0
return m.amount > 0
}

// IsNegative returns boolean of whether the value of Money is negative.
func (m *Money) IsNegative() bool {
return m.amount.val < 0
return m.amount < 0
}

// Absolute returns new Money struct from given Money using absolute monetary value.
Expand Down Expand Up @@ -235,15 +233,15 @@ func (m *Money) Split(n int) ([]*Money, error) {
}

r := mutate.calc.modulus(m.amount, int64(n))
l := mutate.calc.absolute(r).val
l := mutate.calc.absolute(r)
// Add leftovers to the first parties.

v := int64(1)
if m.amount.val < 0 {
if m.amount < 0 {
v = -1
}
for p := 0; l != 0; p++ {
ms[p].amount = mutate.calc.add(ms[p].amount, &Amount{v})
ms[p].amount = mutate.calc.add(ms[p].amount, v)
l--
}

Expand Down Expand Up @@ -273,18 +271,18 @@ func (m *Money) Allocate(rs ...int) ([]*Money, error) {
}

ms = append(ms, party)
total += party.amount.val
total += party.amount
}

// Calculate leftover value and divide to first parties.
lo := m.amount.val - total
lo := m.amount - total
sub := int64(1)
if lo < 0 {
sub = -sub
}

for p := 0; lo != 0; p++ {
ms[p].amount = mutate.calc.add(ms[p].amount, &Amount{sub})
ms[p].amount = mutate.calc.add(ms[p].amount, sub)
lo -= sub
}

Expand All @@ -294,13 +292,13 @@ func (m *Money) Allocate(rs ...int) ([]*Money, error) {
// Display lets represent Money struct as string in given Currency value.
func (m *Money) Display() string {
c := m.currency.get()
return c.Formatter().Format(m.amount.val)
return c.Formatter().Format(m.amount)
}

// AsMajorUnits lets represent Money struct as subunits (float64) in given Currency value
func (m *Money) AsMajorUnits() float64 {
c := m.currency.get()
return c.Formatter().ToMajorUnits(m.amount.val)
return c.Formatter().ToMajorUnits(m.amount)
}

// UnmarshalJSON is implementation of json.Unmarshaller
Expand Down
Loading

0 comments on commit e9a67ed

Please sign in to comment.