diff --git a/pricingCalculator/conversion/conversion.go b/pricingCalculator/conversion/conversion.go new file mode 100644 index 0000000..1742615 --- /dev/null +++ b/pricingCalculator/conversion/conversion.go @@ -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 +} \ No newline at end of file diff --git a/pricingCalculator/go.mod b/pricingCalculator/go.mod new file mode 100644 index 0000000..9a858fe --- /dev/null +++ b/pricingCalculator/go.mod @@ -0,0 +1,3 @@ +module example.com/pricing-calculator + +go 1.23.2 diff --git a/pricingCalculator/main.go b/pricingCalculator/main.go new file mode 100644 index 0000000..6443e71 --- /dev/null +++ b/pricingCalculator/main.go @@ -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) +} \ No newline at end of file diff --git a/pricingCalculator/prices/prices.go b/pricingCalculator/prices/prices.go new file mode 100644 index 0000000..91b0d3e --- /dev/null +++ b/pricingCalculator/prices/prices.go @@ -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, + ) +} \ No newline at end of file diff --git a/pricingCalculator/prices/prices.txt b/pricingCalculator/prices/prices.txt new file mode 100644 index 0000000..a8153df --- /dev/null +++ b/pricingCalculator/prices/prices.txt @@ -0,0 +1,4 @@ +9.99 +10.49 +15.89 +12 \ No newline at end of file