-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
executable file
·72 lines (57 loc) · 1.39 KB
/
main.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
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func getInput(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, _ := getInput("Create a new bill name: ", reader)
b := newBill(name)
fmt.Println("Created the bill: - ", b.name)
return b
}
func propmptOptions(b bill) {
reader := bufio.NewReader(os.Stdin)
opt, _ := getInput("Choose option (a - add item, s - save bill, t - add tip):", reader)
switch opt {
case "a":
name, _ := getInput("Item name: ", reader)
price, _ := getInput("Item price: ", reader)
p, err := strconv.ParseFloat(price, 64)
if err != nil {
fmt.Println("The price must be a number", err)
propmptOptions(b)
}
b.addItem(name, p)
fmt.Println("Item added - ", name, price)
propmptOptions(b)
case "t":
tip, _ := getInput("Enter tip amount: ", reader)
t, err := strconv.ParseFloat(tip, 64)
if err != nil {
fmt.Println("The tip must be a number", err)
propmptOptions(b)
}
b.updateTip(t)
fmt.Println("tip added - ", tip)
propmptOptions(b)
case "s":
fmt.Println("you chose to save the file - ", b.name)
b.save()
default:
fmt.Println("that what not a valid option...")
propmptOptions(b)
}
}
func main() {
mybill := createBill()
propmptOptions(mybill)
}