Skip to content

Instantly share code, notes, and snippets.

@marvinhosea
Last active October 7, 2022 06:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save marvinhosea/0d80107bf5a3f881a42976d2ddff17b5 to your computer and use it in GitHub Desktop.
invoice_data.go
package internal
import "errors"
type InvoiceData struct {
Title string
Quantity int64
Price int64
TotalAmount int64
}
func (d *InvoiceData) CalculateTotalAmount() int64 {
totalAmount := d.Quantity * d.Price
return totalAmount
}
func (d *InvoiceData) ReturnItemTotalAmount() float64 {
totalAmount := d.CalculateTotalAmount()
converted := float64(totalAmount) / 100
return converted
}
func (d *InvoiceData) ReturnItemPrice() float64 {
converted := float64(d.Price) / 100
return converted
}
func NewInvoiceData(title string, qty int64, price interface{}) (*InvoiceData, error) {
var convertedPrice int64
switch priceValue := price.(type) {
case int64:
convertedPrice = priceValue * 100
case int:
convertedPrice = int64(priceValue * 100)
case float32:
convertedPrice = int64(priceValue * 100)
case float64:
convertedPrice = int64(priceValue * 100)
default:
return nil, errors.New("type not permitted")
}
return &InvoiceData{
Title: title,
Quantity: qty,
Price: convertedPrice,
}, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment