Skip to content

Commit

Permalink
Added example of a map for handling userData
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWhittington committed Sep 14, 2024
1 parent cba2d86 commit 2a6d093
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
5 changes: 2 additions & 3 deletions helper/helper.go → helper.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package helper
package main

import "strings"

// captialise functions to export them
func ValidateUserInput(firstName string, lastName string, email string, userTickets uint, remainingTickets uint) (bool, bool, bool) { // multiple returns! declare inside ()
func validateUserInput(firstName string, lastName string, email string, userTickets uint, remainingTickets uint) (bool, bool, bool) { // multiple returns! declare inside ()

isValidName := len(firstName) >= 2 && len(lastName) >= 2
isValidEmail := strings.Contains(email, "@")
Expand Down
21 changes: 13 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package main

import (
"booking-app/helper"
"fmt"
"strings"
"strconv"
)

const conferenceTickets uint = 50

var conferenceName = "Go conference"
var remainingTickets uint = conferenceTickets
var bookings []string
var bookings = make([]map[string]string, 0)

func main() {

Expand All @@ -19,7 +18,7 @@ func main() {
//for { // infinite loop
for remainingTickets > 0 && len(bookings) < 50 { //equivalant of a while loop. Set a condition after 'for' keyword
firstName, lastName, email, userTickets := getUserInput()
isValidName, isValidEmail, isValidTicketNumber := helper.ValidateUserInput(firstName, lastName, email, userTickets, remainingTickets)
isValidName, isValidEmail, isValidTicketNumber := validateUserInput(firstName, lastName, email, userTickets, remainingTickets)

if isValidName && isValidEmail && isValidTicketNumber {
bookTicket(firstName, lastName, email, userTickets)
Expand Down Expand Up @@ -52,12 +51,11 @@ func greetUsers() {
fmt.Println("Get your tickets here to attend")
}

func getFirstNames(bookings []string) []string { // return type is put after the parameters
func getFirstNames(bookings []map[string]string) []string { // return type is put after the parameters
firstNames := []string{}

for _, booking := range bookings { //typical foreach loop (for index, value:= range list). Use _ to mark as not used
var names = strings.Fields(booking)
firstNames = append(firstNames, names[0])
firstNames = append(firstNames, booking["firstName"])
}
return firstNames
}
Expand All @@ -82,7 +80,14 @@ func getUserInput() (string, string, string, uint) {

func bookTicket(firstName string, lastName string, email string, userTickets uint) {
remainingTickets -= userTickets
bookings = append(bookings, firstName+" "+lastName)

var userData = make(map[string]string)
userData["firstName"] = firstName
userData["lastName"] = lastName
userData["email"] = email
userData["numberOfTickets"] = strconv.FormatUint(uint64(userTickets), 10)

bookings = append(bookings, userData)

fmt.Printf("Thankyou %v %v for booking %v tickets. You will recieve a confirmation email at %v\n", firstName, lastName, userTickets, email)
fmt.Printf("%v tickets remaining for %v\n", remainingTickets, conferenceName)
Expand Down

0 comments on commit 2a6d093

Please sign in to comment.