generated from github/codespaces-blank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbill.go
111 lines (79 loc) · 1.77 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
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
}
// add item to bill
func (b *bill) addItem(name string, price float64) {
b.items[name] = price
}
// format the bill
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
}
// add tip
fs += fmt.Sprintf("%-25v ...$%v\n", "tip:", b.tip)
// add total
fs += fmt.Sprintf("%-25v ...$%0.2f", "total:", total+b.tip)
return fs
}
// update tip
func (b *bill) updateTip(tip float64) {
(*b).tip = tip
// b.tip = tip
}
func getbillInput(prompt string, r *bufio.Reader) (string, error) {
fmt.Print(prompt)
input, err := r.ReadString('\n')
return strings.TrimSpace(input), err
}
func createBill() bill {
reader := bufio.NewReader(os.Stdin)
name, _ := getbillInput("Create a new bill name: ", reader)
b := newBill(name)
fmt.Println("Created the bill -", b.name)
return b
}
func promptOptions(b bill) {
reader := bufio.NewReader(os.Stdin)
opt, _ := getbillInput("Choose option (a - add item, s - save bill, t - add tip): ", reader)
switch opt {
case "a":
name, _ := getbillInput("Item name: ", reader)
price, _ := getbillInput("Item price: ", reader)
fmt.Println(name, price)
case "t":
tip, _ := getbillInput("Enter tip amount ($): ", reader)
fmt.Println(tip)
case "s":
fmt.Println("you chose to save the bill")
default:
fmt.Println("That was not a valid option...")
promptOptions(b)
}
}
func main() {
mybill := createBill()
promptOptions(mybill)
}