I am writing out a homework problem which I may have over-engineered with too many structs. It made sense at the time. I want to loop through Seasonal Discounts (through 2 different arrays- HighPrices, and LowPrices ) and just not sure if a) the set up is good or not, b) how to add the price in a less redundant manner.
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println(getPrice(2, "bananas"))
}
func getPrice(p float32, f string) float32 {
type HighPriceItems struct {
items []string
price float32
}
type LowPriceItems struct {
items []string
price float32
}
type seasonMatrix struct {
h HighPriceItems
l LowPriceItems
}
var seasonPrices = func() seasonMatrix {
var h = HighPriceItems{}
var l = LowPriceItems{}
var season = seasonMatrix{}
switch time.Now().Month() {
case time.March, time.April, time.May, time.June, time.July:
h := append(h.items, "apples", "oranges", "pears")
l := append(l.items, "bananas", "grapes")
season.h.items = h
season.h.price = 4
season.l.items = l
season.l.price = 2
return season
case time.August, time.September, time.October, time.November, time.December, time.January, time.February:
h := append(h.items, "bananas", "grapes")
l := append(l.items, "apples", "oranges", "pears")
season.h.items = h
season.h.price = 4
season.l.price = 2
season.l.items = l
return season
}
return season
}
const normalDiscount float32 = .75
var x = p * normalDiscount
var specials = seasonPrices()
var finalPrice float32
for _, n := range specials.h.items {
if f == n {
if specials.h.price > x {
finalPrice = specials.h.price
} else {
finalPrice = x
}
}
}
for _, n := range specials.l.items {
if f == n {
if specials.l.price > x {
finalPrice = specials.l.price
} else {
finalPrice = x
}
}
}
return finalPrice
}