diff --git a/README.md b/README.md index 78dd187..4eabbc3 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,16 @@ # go-cli-course -Full golang course with a CLI project development \ No newline at end of file +Full golang course with a CLI project development + +## Project + +- Booking App +- Stopped at 1:56:05 - https://youtu.be/yyUHQIec83I?si=62O0tlgcHmQBku9f&t=6965 + +## Setup + +### Install Go + +### VS Code Extensions + +- Go - Go Team at Google diff --git a/booking-app/go.mod b/booking-app/go.mod new file mode 100644 index 0000000..0e59127 --- /dev/null +++ b/booking-app/go.mod @@ -0,0 +1,3 @@ +module booking-app + +go 1.26.5 diff --git a/booking-app/main b/booking-app/main new file mode 100755 index 0000000..e71a116 Binary files /dev/null and b/booking-app/main differ diff --git a/booking-app/main.go b/booking-app/main.go new file mode 100644 index 0000000..ddd9146 --- /dev/null +++ b/booking-app/main.go @@ -0,0 +1,80 @@ +package main + +import ( + "fmt" + "strings" +) + +func main() { + conferenceName := "Go Conference" + const conferenceTickets int = 50 + var remainingTickets uint = 50 + + fmt.Printf("Welcome to %v booking application\n", conferenceName) + fmt.Printf("We have total of %v tickets and %v are still available.\n", conferenceTickets, remainingTickets) + fmt.Println("Get your tickets here to attend") + + bookings := []string{} + + for remainingTickets > 0 && len(bookings) < 50 { + var firstName string + var lastName string + var email string + var userTickets uint + + // ask user for their name + fmt.Printf("- Enter your first name: ") + fmt.Scan(&firstName) + + // ask user for their last name + fmt.Printf("- Enter your last name: ") + fmt.Scan(&lastName) + + // ask user for their email + fmt.Printf("- Enter your email address: ") + fmt.Scan(&email) + + // ask user for the number of tickets + fmt.Printf("- How many tickets to you want to book: ") + fmt.Scan(&userTickets) + + // validations + isValidName := len(firstName) >= 2 && len(lastName) >= 2 + isValidEmail := strings.Contains(email, "@") + isValidNumber := userTickets > 0 && userTickets <= remainingTickets + + if isValidName && isValidEmail && isValidNumber { + // array-fashion: bookings[0] = firstName + " " + lastName + bookings = append(bookings, firstName + " " + lastName) + remainingTickets = remainingTickets - userTickets + + // Thank you message + fmt.Printf("Thank you %v %v for booking %v tickets. You will receive a confirmation email at %v\n", firstName, lastName, userTickets, email) + fmt.Printf("%v ticket remaining for %v\n", remainingTickets, conferenceName) + + firstNames := []string{} + for _, firstAndLastName := range bookings { + var names = strings.Fields(firstAndLastName) + firstNames = append(firstNames, names[0]) + } + fmt.Printf("These are all the first names of our bookings: %v\n", firstNames) + + if remainingTickets == 0 { + // end program + fmt.Println("Out conference is booked out. Come back next year.") + break + } + } else { + if !isValidName { + fmt.Printf("!! Oops: first name and last name must be at least size 2\n") + } + if !isValidEmail { + fmt.Printf("!! Oops: email must be a valid email\n") + } + if !isValidNumber { + fmt.Printf("!! Oops: number of tickets should be valid and less or equals %v\n", remainingTickets) + } + } + } +} +