2
1
Fork 0
pr3-ws202526/go/06-airport/airport/passenger.go

129 lines
4.0 KiB
Go

package airport
import (
"fmt"
"math/rand"
"time"
"github.com/docker/docker/pkg/namesgenerator"
)
type Ticket struct {
Name string
Destination string
FlightNumber FlightNumber
}
type BoardingPass struct {
Ticket
Gate GateNumber
}
type Baggage struct {
Items []string
}
type Passenger struct {
Name string
Ticket Ticket
BoardingPass *BoardingPass
Bags []Baggage
CarryOnItems []string
}
var PossibleItems = []string{
"Laptop", "Tablet", "Smartphone", "Headphones", "Charger", "Book", "Magazine", "Notebook", "Pen", "Pencil", "Carabiner",
"Water Bottle", "Snacks", "Sandwich", "Camera", "Tripod", "Gun", "Clothes", "Shoes", "Toothbrush", "Toothpaste",
"Shampoo", "Conditioner", "Soap", "Deodorant", "Perfume", "Makeup Kit", "Hairbrush", "Comb", "Razor", "Shaving Cream",
"First Aid Kit", "Medicine", "Umbrella", "Hat", "Sunglasses", "Scarf", "Gloves", "Wallet", "Passport", "Camping Gear",
"Keys", "Watch", "Jewelry", "Flashlight", "Power Bank", "Knife", "USB Drive", "External Hard Drive", "Gaming Console", "Controller",
"Board Game", "Puzzle", "Toy", "Explosives", "Stuffed Animal", "Blanket", "Pillow", "Sleeping Bag", "Towel", "Swimsuit",
"Goggles", "Flip Flops", "Backpack", "Handbag", "Suitcase", "Briefcase", "Binoculars", "Map", "Guidebook", "Notebook",
"Sketchpad", "Paints", "Brushes", "Musical Instrument", "Sheet Music", "Sports Equipment", "Yoga Mat", "Jump Rope", "Resistance Bands",
"Tent", "Cooking Utensils", "Portable Stove", "Food Containers", "Water Filter", "Fishing Rod", "Knife Sharpener", "Multi-tool", "Rope",
"Compass", "GPS Device", "Safety Vest", "Helmet", "Protective Gear", "Fire Extinguisher", "Matches", "Lighter", "Candles", "Notebook",
"Clipboard", "Calculator", "Documents", "Folders", "Envelopes",
}
func NewRandomPassenger(flights []Flight) Passenger {
// Generate random ticket details
name := namesgenerator.GetRandomName(0)
flight := flights[rand.Intn(len(flights))]
ticket := Ticket{
Name: name,
Destination: flight.Destination,
FlightNumber: flight.Id,
}
// Generate random baggage
numBags := rand.Intn(3) // Random number of bags (0 to 2)
bags := make([]Baggage, numBags)
for i := 0; i < numBags; i++ {
numItems := rand.Intn(5) + 1 // Random number of items (1 to 5)
items := make([]string, numItems)
for j := 0; j < numItems; j++ {
items[j] = PossibleItems[rand.Intn(len(PossibleItems))]
}
bags[i] = Baggage{
Items: items,
}
}
// Generate random carry-on items
numCarryOnItems := rand.Intn(3) // Random number of carry-on items (0 to 2)
carryOnItems := make([]string, numCarryOnItems)
for i := 0; i < numCarryOnItems; i++ {
carryOnItems[i] = PossibleItems[rand.Intn(len(PossibleItems))]
}
return Passenger{
Name: name,
Ticket: ticket,
BoardingPass: nil,
Bags: bags,
CarryOnItems: carryOnItems,
}
}
func (p *Passenger) Start(ap *Airport) {
fmt.Printf("%s has arrived at the airport for the flight number %s to %s in %.02f minutes with %d bags and carry-on items: %v\n",
p.Name,
p.Ticket.FlightNumber,
p.Ticket.Destination,
time.Until(ap.FlightSchedule.flights[p.Ticket.FlightNumber].DepartureTime).Minutes(),
len(p.Bags),
p.CarryOnItems)
err := ap.Checkin.Process(p)
if err != nil {
fmt.Printf("\033[31m%s: %v\033[0m\n", p.Name, err)
return
}
fmt.Printf("%s completed the checkin procedure and is now heading to security\n", p.Name)
err = ap.Security.Process(*p)
if err != nil {
fmt.Printf("\033[31m%s: %v\033[0m\n", p.Name, err)
return
}
fmt.Printf("%s has successfully passed the security check and is now walking to the gate\n", p.Name)
gate, ok := ap.Gates[p.BoardingPass.Gate]
if !ok {
fmt.Printf("\033[31m%s: invalid gate number %v\033[0m\n", p.Name, p.BoardingPass.Gate)
return
}
err = gate.Process(*p)
if err != nil {
fmt.Printf("\033[31mError: %s: %v\033[0m\n", p.Name, err)
return
}
fmt.Printf("\033[32m%s has successfully boarded flight %s to %s\033[0m\n", p.Name, p.Ticket.FlightNumber, p.Ticket.Destination)
}