-
Notifications
You must be signed in to change notification settings - Fork 0
/
userinput.go
76 lines (58 loc) · 1.38 KB
/
userinput.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
package main
import (
"os"
"fmt"
"bufio"
"strings"
"strconv"
)
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 promptOptions(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")
promptOptions(b)
}
b.addItem(name, p)
fmt.Println("Item added -", name, price)
promptOptions(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")
promptOptions(b)
}
b.updateTip(t)
fmt.Println("tip added - ", tip)
promptOptions(b)
case "s":
b.save()
fmt.Println("you saved the file - ", b)
default:
fmt.Println("that was not a valid option...")
promptOptions(b)
}
}
func main(){
mybill := createBill()
promptOptions(mybill)
fmt.Println(mybill)
}