package airport import ( "fmt" "math/rand" "strings" "time" ) type FlightNumber string type Flight struct { Id FlightNumber Destination string GateNumber GateNumber BoardingTime time.Time DepartureTime time.Time } type FlightProvider interface { GetFlight(FlightNumber) (Flight, error) } type FlightSchedule struct { flights map[FlightNumber]Flight } var possibleDestinations = []string{ "AMS", "ATH", "BCN", "BRU", "BUD", "CPH", "DUB", "DUS", "EDI", "FCO", "FRA", "GVA", "HEL", "IST", "LIS", "LYS", "MAD", "MUC", "OSL", "PRG", "RIX", "SOF", "STO", "TLL", "VIE", "WAW", "ZAG", "ZRH", "LUX", "MLA", } func GenerateRandomFlightNumbers(count int) []FlightNumber { airlines := []string{"LH", "AF", "KL", "BA", "SK", "AZ", "IB", "TK", "AY", "OS"} flightNumbers := make([]FlightNumber, count) for i := 0; i < count; i++ { airline := airlines[rand.Intn(len(airlines))] number := rand.Intn(9000) + 1000 // Random 4-digit number flightNumbers[i] = FlightNumber(fmt.Sprintf("%s%04d", airline, number)) } return flightNumbers } func GenerateRandomFlightSchedule(gates map[GateNumber]*Gate, count int) FlightSchedule { flightNumbers := GenerateRandomFlightNumbers(count) flights := make(map[FlightNumber]Flight) gateNumbers := make([]GateNumber, 0, len(gates)) for gn, _ := range gates { gateNumbers = append(gateNumbers, gn) } for _, flightNumber := range flightNumbers { destination := possibleDestinations[rand.Intn(len(possibleDestinations))] gateNumber := gateNumbers[rand.Intn(len(gateNumbers))] departureTime := time.Now().Add(time.Duration(rand.Intn(5*60)) * time.Second) boardingTime := departureTime.Add(-30 * time.Second) flights[flightNumber] = Flight{ Id: flightNumber, Destination: destination, GateNumber: gateNumber, BoardingTime: boardingTime, DepartureTime: departureTime, } } return FlightSchedule{flights: flights} } func (fs *FlightSchedule) AllFlights() []Flight { flights := make([]Flight, 0, len(fs.flights)) for _, flight := range fs.flights { flights = append(flights, flight) } return flights } func (fs *FlightSchedule) GetFlight(flightNumber FlightNumber) (Flight, error) { if flight, ok := fs.flights[flightNumber]; ok { return flight, nil } else { return Flight{}, fmt.Errorf("invalid flight number %v", flightNumber) } } func (fs *FlightSchedule) Print() { fmt.Printf("%-15s %-15s %-10s %-20s %-20s\n", "Flight Number", "Destination", "Gate", "Boarding Time", "Departure Time") fmt.Println(strings.Repeat("-", 80)) for _, flight := range fs.AllFlights() { fmt.Printf("%-15s %-15s %-10d %-20s %-20s\n", flight.Id, flight.Destination, flight.GateNumber, flight.BoardingTime.Format("15:04:05"), flight.DepartureTime.Format("15:04:05"), ) } }