Adding pricing calculator
This commit is contained in:
20
pricingCalculator/conversion/conversion.go
Normal file
20
pricingCalculator/conversion/conversion.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package conversion
|
||||
|
||||
import "errors"
|
||||
import "strconv"
|
||||
|
||||
func StringsToFloats(strings []string) ([]float64, error) {
|
||||
var floats []float64
|
||||
|
||||
for _, stringVal := range strings {
|
||||
floatVal, err := strconv.ParseFloat(stringVal, 64)
|
||||
|
||||
if err != nil {
|
||||
return nil, errors.New("Failed to convert string to float")
|
||||
}
|
||||
|
||||
floats = append(floats, floatVal)
|
||||
}
|
||||
|
||||
return floats, nil
|
||||
}
|
||||
3
pricingCalculator/go.mod
Normal file
3
pricingCalculator/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module example.com/pricing-calculator
|
||||
|
||||
go 1.23.2
|
||||
16
pricingCalculator/main.go
Normal file
16
pricingCalculator/main.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"example.com/price-calculator/prices"
|
||||
)
|
||||
|
||||
func main() {
|
||||
taxRates := []float64(0, 0.7, 0.1, 0.15)
|
||||
|
||||
for _, taxRate := range taxRates {
|
||||
priceJob := prices.NewTaxIncludedPriceJob(taxRate)
|
||||
priceJob.Process()
|
||||
}
|
||||
|
||||
fmt.Println(result)
|
||||
}
|
||||
72
pricingCalculator/prices/prices.go
Normal file
72
pricingCalculator/prices/prices.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package prices
|
||||
|
||||
import "fmt"
|
||||
import "os"
|
||||
import "bufio"
|
||||
import "strconv"
|
||||
import "example.com/pricing-calculator/conversion"
|
||||
|
||||
type TaxIncludedPriceJob struct {
|
||||
TaxRate float64
|
||||
InputPrices []float64
|
||||
TaxIncludedPrices map[string]float64
|
||||
}
|
||||
|
||||
func (job *TaxIncludedPriceJob) LoadData() {
|
||||
file, err := os.Open("prices.txt")
|
||||
|
||||
if err != nil (
|
||||
fmt.Println("An error occurred!")
|
||||
fmt.Println(err)
|
||||
return
|
||||
)
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
var lines []string
|
||||
|
||||
for scanner.Scan() {
|
||||
lines = append(lines, scanner.Text())
|
||||
}
|
||||
|
||||
err := scanner.Err()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("An error occurred!")
|
||||
fmt.Println(err)
|
||||
file.Close()
|
||||
return
|
||||
}
|
||||
|
||||
prices, err := conversion.StringsToFloats(lines)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Converting string to float failed")
|
||||
fmt.Println(err)
|
||||
file.Close()
|
||||
return
|
||||
}
|
||||
|
||||
job.InputPrices = prices
|
||||
file.Close()
|
||||
}
|
||||
|
||||
func (job TaxIncludedPriceJob) Process() {
|
||||
job.LoadData()
|
||||
|
||||
result := make(map[string]string)
|
||||
|
||||
for _, price := range job.InputPrices {
|
||||
taxIncludedPrice := price * (1 + job.TaxRate)
|
||||
result[fmt.Sprintf("%.2f", price)] = fmt.Sprintf("%.2f", taxIncludedPrice)
|
||||
}
|
||||
|
||||
fmt.Println(result)
|
||||
}
|
||||
|
||||
func NewTaxIncludedPriceJob(taxRate float64) *TaxIncludedPriceJob {
|
||||
return &TaxIncludedPriceJob(
|
||||
InputPrices: []float64(10,20,30),
|
||||
TaxRate: taxRate,
|
||||
)
|
||||
}
|
||||
4
pricingCalculator/prices/prices.txt
Normal file
4
pricingCalculator/prices/prices.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
9.99
|
||||
10.49
|
||||
15.89
|
||||
12
|
||||
Reference in New Issue
Block a user