-
-
Save marvinhosea/0d80107bf5a3f881a42976d2ddff17b5 to your computer and use it in GitHub Desktop.
invoice_data.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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