Each line on the bill is a Line: a name, a unit Price and a quantity Qty. First write the method Cost() on Line to return that line's cost (price times quantity); then write Receipt(lines) to add up the whole bill with it. An empty bill gives 0.
Receipt([]Line{{"pencil", 2, 5}, {"notebook", 8, 1}})→18Receipt([]Line{})→0func Receipt(lines []Line) intpackage main
type Line struct {
Name string
Price int
Qty int
}
func (l Line) Cost() int {
// your code here: return price times quantity
return 0
}
func Receipt(lines []Line) int {
total := 0
for _, l := range lines {
// your code here: add this line's Cost() to total
}
return total
}
Run tries the examples. When they look right, Submit.