-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbill.go
64 lines (51 loc) · 984 Bytes
/
bill.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
import (
"fmt"
"os"
)
// next 19
type bill struct {
name string
items map[string]float64
tip float64
}
// make new bills
func newBill(name string) bill {
b := bill{
name: name,
items: map[string]float64{},
tip: 0,
}
return b
}
// format the bill
// receiver function
func (b *bill) format() string {
fs := "Bill breakdown: \n"
var total float64 = 0
// list items
for k, v := range b.items {
fs += fmt.Sprintf("%-25v ... $%v \n", k+":", v)
total += v
}
// tip
fs += fmt.Sprintf("%-25v ... $%v \n", "tip: ", b.tip)
// total
fs += fmt.Sprintf("%-25v ... $%0.2f", "total:", total+b.tip)
return fs
}
func (b *bill) updateTip(tip float64) {
b.tip = tip
}
func (b *bill) addItem(name string, price float64) {
b.items[name] = price
}
// save bill
func (b *bill) save() {
data := []byte(b.format())
err := os.WriteFile("bills/"+b.name+".txt", data, 0644)
if err != nil {
panic(err)
}
fmt.Println("bill was saved to file")
}