-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbill.go
64 lines (52 loc) · 1.17 KB
/
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"
)
// Bill struct to hold bill data
type bill struct {
name string
items map[string]float64
tip float64
}
// Function to create a new bill
func newBill(name string) bill {
b := bill{
name: name,
items: map[string]float64{},
tip: 0,
}
return b
}
// Method to format the bill as a string
func (b *bill) format() string {
fs := "Bill Breakdown: \n\n"
var total float64 = 0
// List the items
for k, v := range b.items {
fs += fmt.Sprintf("%-25v ...$%v \n", k+":", v)
total += v
}
// Tip
fs += fmt.Sprintf("\n%-25v ...$%0.2f \n", "Tip:", b.tip)
// Total
fs += fmt.Sprintf("\n%-25v ...$%0.2f", "Total:", total+b.tip)
return fs
}
// Method to update the tip
func (b *bill) updateTip(tip float64) {
b.tip = tip
}
// Method to add an item to the bill
func (b *bill) addItem(name string, price float64) {
b.items[name] = price
}
// Method to save the bill to a file
func (b *bill) saveBill() {
data := []byte(b.format())
err := os.WriteFile("bills/"+b.name+".txt", data, 0644)
if err != nil {
panic(err)
}
fmt.Println("The bill was saved successfully!")
}