-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
187 lines (131 loc) · 4.2 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"BOOKING_APP/helper"
"fmt"
"time"
"sync"
// "strconv"
// "strings"
)
var wait =sync.WaitGroup{}
//package level variable
var conferenceName = "Go Conference"
const conferenceTicket int = 50
var remainingTicket int = 50
// var booking = make([]map[string]string, 0)
var booking = make([]UserData, 0)
type UserData struct {
firstName string
lastName string
email string
userTicket int
}
func main() {
greetUsers()
//for remainingTicket > 0 || remainingTicket < 50
for {
firstName, lastName, email, userTicket := getUserInput()
isValidateName, isValidateEmail, isValidateTicketNumber := helper.ValidateUserInput(firstName, lastName, email, userTicket, remainingTicket)
if isValidateName && isValidateEmail && isValidateTicketNumber {
bookTicket(userTicket, firstName, lastName, email)
wait.Add(1)
go sendTicket(userTicket, firstName, lastName, email)
firstNames := getFirstName()
fmt.Printf("The booking user is %v\n", firstNames)
if remainingTicket == 0 {
fmt.Printf("Our conference tickets are sold. Thank you and try in next year")
break
} else {
fmt.Printf("Remaining ticket are %v \n", remainingTicket)
}
} else {
//fmt.Printf("we only have %v remaining ticket, but you can't book %v tickets\n", remainingTicket, userTicket)
if !isValidateName {
fmt.Println("The first name or last name you entered is too short")
}
if !isValidateEmail {
fmt.Println("The email address you entered doesn't contain @ sign")
}
if !isValidateTicketNumber {
fmt.Println("number of ticket you entered is invalid")
}
//fmt.Println("Your input are wrong, please try again later")
continue
}
wait.Wait()
}
}
func greetUsers() {
fmt.Printf("Welcome to %v booking application\n", conferenceName)
fmt.Printf("We have total of %v ticket and %v are still available\n", conferenceTicket, remainingTicket)
fmt.Printf("Get your Ticket here\n")
}
func getFirstName() []string {
firstNames := []string{}
// for i := 0; i < len(booking); i++ {
// //fmt.Println(strings.Split(booking[i], " "))
// var name = strings.Split(booking[i], " ")
// //fmt.Println("name : ", name)
// firstNames = append(firstNames, name[0])
// }
for _, bookings := range booking {
firstNames = append(firstNames, bookings.firstName)
// fmt.Printf("The booking user is %v\n", firstNames)
}
return firstNames
}
func getUserInput() (string, string, string, int) {
var firstName string
var lastName string
var email string
var userTicket int
fmt.Printf("Enter your first name: ")
fmt.Scan(&firstName)
fmt.Printf("Enter your last name: ")
fmt.Scan(&lastName)
fmt.Printf("Enter your email address: ")
fmt.Scan(&email)
fmt.Printf("Enter your ticket: ")
fmt.Scan(&userTicket)
return firstName, lastName, email, userTicket
}
func bookTicket(userTicket int, firstName string, lastName string, email string) {
remainingTicket = remainingTicket - userTicket
// var userData = make(map[string]string)
// userData["firstName"] = firstName
// userData["lastName"] = lastName
// userData["email"] = email
// userData["numberOfTicket"] = strconv.Itoa(userTicket)
var userData = UserData{
firstName: firstName,
lastName: lastName,
email: email,
userTicket: userTicket,
}
booking = append(booking, userData)
fmt.Printf("UserData is: %v\n", userData)
fmt.Printf("List of user is: %v\n", booking)
fmt.Printf("user %v %v is get %v ticket. \n", firstName, lastName, userTicket)
fmt.Printf("thank you and an invitation is send to your email address at %v.\n", email)
//fmt.Printf("Remaining ticket are %v \n", remainingTicket)
}
func sendTicket(userTicket int, firstName string, lastName string, email string) {
time.Sleep(50 * time.Second)
var user = fmt.Sprintf("%v tickets are for %v %v\n", userTicket, firstName, lastName)
fmt.Println("###########")
fmt.Printf("%v tickets are send to %v \n", userTicket, email)
fmt.Println("###########")
fmt.Print(user)
wait.Done()
}
// func switchCaseGo() {
// city := "Dhaka"
// switch city {
// case "London":
// fmt.Println("City is London")
// case "Bangladesh", "Dhaka":
// fmt.Println("City in Bangladesh")
// default:
// fmt.Println("City is not found")
// }
// }