-
Notifications
You must be signed in to change notification settings - Fork 0
/
bill.go
55 lines (43 loc) · 874 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
package main
import (
"fmt"
"os"
)
type bill struct {
name string
items map[string]float64
tip float64
}
func newBill(name string) bill {
b := bill{
name: name,
items: map[string]float64{},
tip: 0,
}
return b
}
func (b bill) format() string {
fs := "Bill breakdown: \n"
var total float64 = 0
for k, v := range b.items {
fs += fmt.Sprintf("%-25v ...$%v\n", k+":", v)
total += v
}
fs += fmt.Sprintf("%-25v ...$%0.2f\n", "tip :", b.tip)
fs += fmt.Sprintf("%-25v ...$%0.2f\n", "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
}
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 bills")
}