dongzouqie4220 2019-03-31 02:36
浏览 90
已采纳

我可以在一个函数上使用两个for循环,还是有更好的方法?

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
}
  • 写回答

1条回答 默认 最新

  • dph23577 2019-03-31 02:57
    关注

    There's no reason for HighPriceItem and LowPriceItem types.

    If you make it a single PriceItems, you'll be able to turn the 2 for loops at the end into a function over PriceItems and get rid of the duplicate code inside second for loop.

    Also in Go var specials = seasonPrices() is typically written as specials := seasonPrices()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集