trying to fix price
This commit is contained in:
parent
7feee7f8a3
commit
8194cea85e
10 changed files with 145 additions and 54 deletions
|
|
@ -5,7 +5,7 @@ import "time"
|
|||
type Cake struct {
|
||||
Name string
|
||||
ID int
|
||||
Price int // increments of 0.01
|
||||
Price Price // increments of 0.01
|
||||
Amount int
|
||||
}
|
||||
|
||||
|
|
@ -18,18 +18,22 @@ type Order struct {
|
|||
Date time.Time
|
||||
Accepted time.Time
|
||||
Status string
|
||||
Paid int // increments of 0.01
|
||||
Paid Price // increments of 0.01
|
||||
Cakes []Cake
|
||||
}
|
||||
|
||||
func (o *Order) Total() int {
|
||||
out := o.Paid * (-1)
|
||||
func (o *Order) Subtotal() Price {
|
||||
var out Price
|
||||
for _, e := range o.Cakes {
|
||||
out += e.Price * e.Amount
|
||||
out += e.Price * Price(e.Amount)
|
||||
}
|
||||
return out
|
||||
|
||||
}
|
||||
func (o *Order) Total() Price {
|
||||
return o.Subtotal() - o.Paid
|
||||
}
|
||||
|
||||
func (c *Cake) Total() int {
|
||||
return c.Amount * c.Price
|
||||
func (c *Cake) Total() Price {
|
||||
return Price(c.Amount) * c.Price
|
||||
}
|
||||
|
|
|
|||
33
models/price.go
Normal file
33
models/price.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Price int
|
||||
|
||||
func (in Price) String() string {
|
||||
if in%100 == 0 {
|
||||
return strconv.Itoa(int(in) / 100)
|
||||
} else {
|
||||
price := strconv.Itoa(int(in))
|
||||
i := len(price) - 2
|
||||
price = price[:i] + "." + price[i:]
|
||||
return price
|
||||
}
|
||||
}
|
||||
|
||||
func ParsePrice(in string) (Price, error) {
|
||||
if !strings.Contains(in, ".") {
|
||||
price, err := strconv.Atoi(in)
|
||||
return Price(price) * 100, err
|
||||
}
|
||||
before, after, _ := strings.Cut(in, ".")
|
||||
if len(after) != 2 {
|
||||
return 0, fmt.Errorf("Wrong format, requires exactly 2 decimal places after '.'")
|
||||
}
|
||||
price, err := strconv.Atoi(before + after)
|
||||
return Price(price), err
|
||||
}
|
||||
55
models/price_test.go
Normal file
55
models/price_test.go
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
package models
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestFormatPrice(t *testing.T) {
|
||||
var a Price
|
||||
var s string
|
||||
a = 12345678900
|
||||
s = a.String()
|
||||
if s != "123456789" {
|
||||
t.Fatal(a, s)
|
||||
}
|
||||
a = 123456789
|
||||
s = a.String()
|
||||
if s != "1234567.89" {
|
||||
t.Fatal(a, s)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParsePrice(t *testing.T) {
|
||||
var p Price
|
||||
var s string
|
||||
var err error
|
||||
s = "199"
|
||||
p, err = ParsePrice(s)
|
||||
if err != nil || p != 19900 {
|
||||
t.Fatal(s, p)
|
||||
}
|
||||
s = "1.99"
|
||||
p, err = ParsePrice(s)
|
||||
if err != nil || p != 199 {
|
||||
t.Fatal(s, p)
|
||||
}
|
||||
s = "1123"
|
||||
p, err = ParsePrice(s)
|
||||
if err != nil || p != 112300 {
|
||||
t.Fatal(s, p)
|
||||
}
|
||||
s = "1123.123"
|
||||
_, err = ParsePrice(s)
|
||||
if err == nil {
|
||||
t.Fatal(s, "should error")
|
||||
}
|
||||
s = "1123.3"
|
||||
_, err = ParsePrice(s)
|
||||
if err == nil {
|
||||
t.Fatal(s, "should error")
|
||||
}
|
||||
|
||||
s = "1123,33"
|
||||
_, err = ParsePrice(s)
|
||||
if err == nil {
|
||||
t.Fatal(s, "should error")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue