-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (108 loc) · 3.4 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"fmt"
"gogon/helper"
"sync"
"time"
)
const confTicket int = 50
var name = "Dafrin"
const tiket int = 50
var remainingTickets uint = 50
var bookings = make([]UserData, 0)
type UserData struct {
firstName string
lastname string
email string
numberOfTickets uint
}
var wg = sync.WaitGroup{}
func main() {
// refrensi : https://pkg.go.dev/fmt#pkg-overview
greetUsers()
// call function getUserInput
firstName, lastname, email, userTiket := getUserInput()
// call function validation
isValidName, isValidEmail, isValidTicketNumber := helper.ValidateUserInput(firstName, lastname, email, userTiket, remainingTickets)
if isValidName && isValidEmail && isValidTicketNumber {
// call books ticket
bookTicket(userTiket, firstName, lastname, email)
wg.Add(1)
go sendTicket(userTiket, firstName, lastname, email)
// call function print first name
firstNames := getFirstName()
fmt.Printf("There first names of bookings are: %v\n", firstNames)
if remainingTickets == 0 {
// end program
fmt.Println("Our conference is booked out. Come back next year.")
// break
}
} else {
if !isValidName {
fmt.Println("first name or last name you entered is too short")
}
if !isValidEmail {
fmt.Println("email addre you entered doesn't containt @ sign")
}
if !isValidTicketNumber {
fmt.Printf("number of tickets you entered is invalid")
}
}
wg.Wait()
}
func greetUsers() {
fmt.Printf("Hello World %v\n", name)
fmt.Printf("Tiket untuk nonton bola: %v dan tiket %v\n", confTicket, remainingTickets)
fmt.Printf("Get your tickets here to attend")
}
func getFirstName() []string {
firstNames := []string{}
for _, booking := range bookings {
firstNames = append(firstNames, booking.firstName)
}
// fmt.Printf("There first names of bookings are: %v\n", firstNames)
return firstNames
}
// validate
func getUserInput() (string, string, string, uint) {
var firstName string
var lastname string
var email string
var userTiket uint
fmt.Printf("Enter your first name: ")
fmt.Scan(&firstName)
fmt.Printf("Enter your last name: ")
fmt.Scan(&lastname)
fmt.Printf("Enter your email: ")
fmt.Scan(&email)
fmt.Printf("Enter number of ticket: ")
fmt.Scan(&userTiket)
return firstName, lastname, email, userTiket
}
func bookTicket(userTiket uint, firstName string, lastname string, email string) {
remainingTickets = remainingTickets - uint(userTiket)
// create a map for a user
var userData = UserData{
firstName: firstName,
lastname: lastname,
email: email,
numberOfTickets: userTiket,
}
// userData["firstname"] = firstName
// userData["lastname"] = lastname
// userData["email"] = email
// userData["numberOfTickets"] = strconv.FormatUint(uint64(userTiket), 10)
bookings = append(bookings, userData)
fmt.Printf("List of Bookings is %v\n", bookings)
fmt.Printf("Thank you %v %v for booking %v tickets. You will receive a confirmation eamil at %v\n",
firstName, lastname, userTiket, email)
fmt.Printf("%v tickets remaining for %v\n", remainingTickets, name)
}
func sendTicket(userTiket uint, firstName string, lastname string, email string) {
time.Sleep(10 * time.Second)
var ticket = fmt.Sprintf("%v tickets for %v %v\n", userTiket, firstName, lastname)
fmt.Println("###############")
fmt.Printf("Sending tiket:\n %v \nto email address %v\n", ticket, email)
fmt.Println("###############")
wg.Done()
}