HTMLify

LeetCode - Apply Discount Every n Orders - Go
Views: 16 | Author: abh
type Cashier struct {
    customers_done int
    n int
    discount int
    products []int
    prices []int
}


func Constructor(n int, discount int, products []int, prices []int) Cashier {
    var cashier Cashier
    cashier.customers_done = 0
    cashier.n = n
    cashier.discount = discount
    cashier.products = products
    cashier.prices = prices
    return cashier
}


func (self *Cashier) GetBill(product []int, amount []int) float64 {
    self.customers_done++
    var bill float64
    for i:=0; i<len(product); i++ {
        var price int
        for _i, _product := range self.products {
            if _product == product[i] {
                price = self.prices[_i]
                break
            }
        }
        bill += float64(price * amount[i])
    }
    if self.customers_done % self.n == 0 {
        bill *= ((100.0 - float64(self.discount)) / 100.0)
    }
    return bill
}


/**
 * Your Cashier object will be instantiated and called as such:
 * obj := Constructor(n, discount, products, prices);
 * param_1 := obj.GetBill(product,amount);
 */

Comments