-
Notifications
You must be signed in to change notification settings - Fork 0
/
share-bill.go
117 lines (86 loc) · 2.54 KB
/
share-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
109
110
111
112
113
114
115
116
117
package main
import (
"fmt"
"os"
)
type shareBill struct {
name string
numbersOfPeople int
totalAmount float64
tip float64
}
func newShareBill(name string) shareBill {
sb := shareBill{
name: name,
}
return sb
}
// Create new bill
func createShareBill() shareBill {
var name string
fmt.Println("What is the name of the share bill?")
fmt.Scanln(&name)
sb := newShareBill(name)
fmt.Println("Created the share bill -", name)
return sb
}
func saveShareBill(sb *shareBill) {
data := []byte(sb.formatShareBill())
err := os.WriteFile("bills/share/"+sb.name+".txt", data, 0644)
if err != nil {
panic(err)
}
fmt.Println("Bill was saved to file")
}
// Format the bill
func (sb *shareBill) formatShareBill() string {
fs := "Share bill breakdown: \n"
// Total
fs += fmt.Sprintf("%-25v ...$%0.2f\n", "total amount:", sb.totalAmount)
// Tip
fs += fmt.Sprintf("%-25v ...$%0.2f\n", "tip:", sb.tip)
// Tip percentage
fs += fmt.Sprintf("%-25v ...%0.2f%%\n", "tip percentage:", sb.calculateTipPercentage())
// Total with tip
fs += fmt.Sprintf("%-25v ...$%0.2f\n", "total with tip:", sb.calculateTotal())
// Numbers of people
fs += fmt.Sprintf("%-25v ...%v\n", "numbers of people:", sb.numbersOfPeople)
// Total per person
fs += fmt.Sprintf("%-25v ...$%0.2f\n", "total per person:", sb.calculateTotalPerPerson())
// Tip per person
fs += fmt.Sprintf("%-25v ...$%0.2f\n", "tip per person:", sb.calculateTipPerPerson())
// Share per person
fs += fmt.Sprintf("%-25v ...$%0.2f\n", "share per person:", sb.calculateShare())
return fs
}
// Ask for numbers of people, total amount and tip
func promptOptionsShareBill(sb *shareBill) {
var totalAmount float64
fmt.Println("What is the total amount?")
fmt.Scanln(&totalAmount)
sb.totalAmount = totalAmount
var tip float64
fmt.Println("What is the tip amount?")
fmt.Scanln(&tip)
sb.tip = tip
var numbersOfPeople int
fmt.Println("How many people?")
fmt.Scanln(&numbersOfPeople)
sb.numbersOfPeople = numbersOfPeople
}
// Functions to calculate the share bill
func (sb *shareBill) calculateShare() float64 {
return (sb.totalAmount + sb.tip) / float64(sb.numbersOfPeople)
}
func (sb *shareBill) calculateTotal() float64 {
return sb.totalAmount + sb.tip
}
func (sb *shareBill) calculateTipPercentage() float64 {
return (sb.tip / sb.totalAmount) * 100
}
func (sb *shareBill) calculateTipPerPerson() float64 {
return sb.tip / float64(sb.numbersOfPeople)
}
func (sb *shareBill) calculateTotalPerPerson() float64 {
return sb.calculateTotal() / float64(sb.numbersOfPeople)
}