-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
287 lines (135 loc) · 4.93 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package main
import (
"fmt"
//"strings"
"booking_app/helper"
)
var conference = "Go Conference"
const tickets = 50
var bookings = []string{}
// var bookings [50]string // array of fixed size
func main() {
var remaining_tickets uint = 50
fmt.Println("--------------------------------------------------------------------------")
fmt.Println(" welcome to", conference, "! ")
fmt.Println("--------------------------------------------------------------------------")
fmt.Println("Get your tickets here!")
//fmt.Println("Number of tickets available:", tickets)
fmt.Printf("Number of tickets available: %v\n", tickets)
fmt.Println("Number of tickets remaining:", remaining_tickets)
// Note:-
/*
we will be using %v for printing the value of the variable
Now we will betaking data from user using fmt.Scan
now we will be using loop to amke our code more efficient
*/
// for loop
for remaining_tickets > 0 && len(bookings) < 50 {
// getuserinput function
Firstname, lastname, email, usertickets := getUserInput()
// validate function
isvalid, isvalidd := helper.Validate(Firstname, lastname, email, usertickets)
if isvalid && isvalidd {
remaining_tickets = remaining_tickets - usertickets
fmt.Println("Number of tickets remaining:", remaining_tickets)
// bookticket function
bookticket( bookings, Firstname, lastname, email, usertickets)
//fmt.Println("--------------------------------------------------------------------------")
} else if usertickets > remaining_tickets {
fmt.Println("Sorry! We only have", remaining_tickets, "tickets left.")
//break
continue
// continue will alow the user to enter the data again
} else if remaining_tickets == 0 {
//end the program
fmt.Println("Sorry! No more tickets available. Please try next year.")
break
} else {
fmt.Println("Please enter the valid data")
continue
}
}
}
func bookticket(bookings []string, Firstname string, lastname string, email string, usertickets uint) string {
bookings = append(bookings, Firstname+" "+lastname)
fmt.Println("--------------------------------------------------------------------------")
fmt.Printf("Bookings: %v\n", bookings)
fmt.Printf("Hello %v %v, you have booked %v tickets and you will get confirmation of tickets on your %v\n", Firstname, lastname, usertickets, email)
fmt.Println("--------------------------------------------------------------------------")
return Firstname
}
func getUserInput() (string, string, string, uint) {
var Firstname string
var usertickets uint
var lastname string
var email string
fmt.Println("Enter your first name:")
fmt.Scan(&Firstname)
fmt.Println("Enter your last name:")
fmt.Scan(&lastname)
fmt.Println("Enter your email:")
fmt.Scan(&email)
fmt.Println("Enter the number of tickets you want to book:")
fmt.Scan(&usertickets)
return Firstname, lastname, email, usertickets
}
/*
func getfirstname(bookings []string, firstnames []string) ([]string, []string) {
for _, booking := range bookings {
names := strings.Fields(booking)
firstnames = append(firstnames, names[0])
fmt.Printf("first value is %v\n", bookings[0])
}
fmt.Printf("they are all the bookings %v\n", firstnames)
return bookings, firstnames
}
*/
/*
func extractFirstNames(bookings []string) []string {
firstnames := []string{}
for _, booking := range bookings {
names := strings.Fields(booking)
if len(names) > 0 {
firstnames = append(firstnames, names[0])
}
}
return firstnames
}
*/
/*
func getfirstname(bookings []string, firstnames []string) ([]string, []string) {
for _, booking := range bookings {
names := strings.Fields(booking)
if len(names) > 0 {
firstnames = append(firstnames, names[0])
}
}
fmt.Printf("First value is %v\n", firstnames[0])
fmt.Printf("They are all the bookings: %v\n", firstnames)
return bookings, firstnames
}
*/
//Note :-
/*
we will be using slice for storing the data of the user
as our array is of fixed size we will be using slices for gettinga any amount of data
gonna use function of string package for getting the first name of the user
now we will be using if else statement for checking the condition
noticketsremaining := remaining_tickets == 0
*/
// getfirstname function
// var firstnames []string
// getfirstname(bookings, firstnames)
// bookings := []string{}
// firstnames := extractFirstNames(bookings)
// fmt.Printf("They are all the bookings: %v\n", firstnames)
//Extra code :-
/*
we will be using %T for checking the type of the variable
fmt.Printf("username is of type %T\n", username)
bookings[0] = Firstname + " " + lastname
checking the validity of the data entered by user
//fmt.Printf("type of slice %T\n", bookings)
//fmt.Printf("last name is %v\n", bookings[1])
//fmt.Printf("length of slice is %v\n", len(bookings))
*/