Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 109 additions & 6 deletions AirportDepartures.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,70 @@ import UIKit
//: e. Use a `String?` for the Terminal, since it may not be set yet (i.e.: waiting to arrive on time)
//:
//: f. Use a class to represent a `DepartureBoard` with a list of departure flights, and the current airport


enum FlightStatus: String {
case enRoute = "En-Route"
case reRoute = "Re-Route"
case scheduled = "Scheduled"
case canceled = "Canceled"
case delayed = "Delayed"
case onTime = "On-Time"
case boarding = "Boarding"
}

enum Airline: String {
case jetBlueAirWays = "JetBlue Airways"
case emirates = "Emirates"
case koreaAir = "Korea Air"
case deltaAirLines = "Delta Air Lines"
case americanAirlines = "American Airlines"
}

struct Airport {
var destination: String
var arrival: String
}

struct Flight {
var airport: Airport
var departure: Date?
var flight: String?
var airline: Airline
var terminal: String?
var flightStatus: FlightStatus
}

class DepartureBoard {
var departureFlights: [Flight] = []
var currentAirport: String
var flightStatus: FlightStatus

init(flightStatus: FlightStatus, currentAirport: String) {
self.departureFlights = []
self.currentAirport = currentAirport
self.flightStatus = flightStatus
}

func passengerAlert() {
for departureFlight in departureFlights {
if (departureFlight.terminal != nil),
departureFlight.departure == nil {
print("TBD")
} else {

switch departureFlight.flightStatus {
case .canceled:
print("We're sorry your flight to \(departureFlight.airport) was canceled, here is a $500 voucher")
case .scheduled:
print("Your flight to \(departureFlight.airport) is scheduled to depart at (time) from terminal: \(String(describing: departureFlight.terminal))")
case .boarding:
print("Your flight is boarding, please head to terminal: \(String(describing: departureFlight.terminal)) immediately. The doors are closing soon.")
default:
print("if you still need help finding your flight status please contact customer support")
}
}
}
}
}

//: ## 2. Create 3 flights and add them to a departure board
//: a. For the departure time, use `Date()` for the current time
Expand All @@ -29,7 +91,15 @@ import UIKit
//: d. Make one of the flights have a `nil` terminal because it has not been decided yet.
//:
//: e. Stretch: Look at the API for [`DateComponents`](https://developer.apple.com/documentation/foundation/datecomponents?language=objc) for creating a specific time
let aa3LAX = Flight(airport: .init(destination: "Los Angeles (LAX)", arrival: "3:00 PM"), departure: Date(), flight: "AA3", airline: .americanAirlines, terminal: "8", flightStatus: .enRoute)
let b62201FLL = Flight(airport: .init(destination: "Fort Lauderdele (FLL)", arrival: "Un-Avilable"), departure: nil, flight: "B62201", airline: .jetBlueAirWays, terminal: "5", flightStatus: .canceled)
let ke82 = Flight(airport: .init(destination: "Seol (ICN)", arrival: "3:41 AM"), departure: Date(), flight: "KE82", airline: .koreaAir, terminal: nil, flightStatus: .enRoute)

let myFlight = DepartureBoard(flightStatus: .onTime, currentAirport: "Los Angeles (LAX)")

myFlight.departureFlights.append(aa3LAX)
myFlight.departureFlights.append(b62201FLL)
myFlight.departureFlights.append(ke82)


//: ## 3. Create a free-standing function that can print the flight information from the `DepartureBoard`
Expand All @@ -42,7 +112,13 @@ import UIKit
//: d. Print out the current DepartureBoard you created using the function


func printDepartures(departureBoard: DepartureBoard) {
for flightDepartures in departureBoard.departureFlights {
print("Current Departures, Departure: \(String(describing: flightDepartures.departure)), Flight: \(String(describing: flightDepartures.flight)), Airline: \(flightDepartures.airline.rawValue), Terminal: \(String(describing: flightDepartures.terminal)), Flight Status: \(flightDepartures.flightStatus.rawValue)")
}
}

printDepartures(departureBoard: myFlight)

//: ## 4. Make a second function to print print an empty string if the `departureTime` is nil
//: a. Createa new `printDepartures2(departureBoard:)` or modify the previous function
Expand All @@ -60,7 +136,25 @@ import UIKit
//: Destination: Boston Airline: KLM Flight: KL 6966 Departure Time: 1:26 PM Terminal: 4 Status: Scheduled



func printDepartures2(departureBoard: DepartureBoard) {
var departure2: String

for departureFlight in departureBoard.departureFlights {

if let unwrapDeparture = departureFlight.departure {
let dateForatter = DateFormatter()
dateForatter.dateStyle = .none
dateForatter.timeStyle = .short
dateForatter.locale = .current

departure2 = dateForatter.string(from: unwrapDeparture)

print("Current Unwrapped Departures, Departure: \(String(describing: departure2)), Flight: \(String(describing: departureFlight.flight)), Airline: \(departureFlight.airline.rawValue), Terminal: \(String(describing: departureFlight.terminal)), Flight Status: \(departureFlight.flightStatus.rawValue)")
}
}
}

printDepartures2(departureBoard: myFlight)
//: ## 5. Add an instance method to your `DepatureBoard` class (above) that can send an alert message to all passengers about their upcoming flight. Loop through the flights and use a `switch` on the flight status variable.
//: a. If the flight is canceled print out: "We're sorry your flight to \(city) was canceled, here is a $500 voucher"
//:
Expand All @@ -75,7 +169,7 @@ import UIKit
//: d. Call the `alertPassengers()` function on your `DepartureBoard` object below
//:
//: f. Stretch: Display a custom message if the `terminal` is `nil`, tell the traveler to see the nearest information desk for more details.

myFlight.passengerAlert()



Expand All @@ -96,6 +190,15 @@ import UIKit
//: e. Make sure to cast the numbers to the appropriate types so you calculate the correct airfare
//:
//: f. Stretch: Use a [`NumberFormatter`](https://developer.apple.com/documentation/foundation/numberformatter) with the `currencyStyle` to format the amount in US dollars.


func calculateAirfare(checkedBags: Int, distance: Int, travelers: Int) -> Double {
let bag = (25 * checkedBags)
let mileCosts = 0.10 * Double(distance)
let ticketCost = 300 * travelers
let totalCost = Double(bag) + Double(mileCosts) + Double(ticketCost)

print("\(checkedBags) bags, \(distance) miles, travlers = \(travelers)")
return(totalCost)
}

calculateAirfare(checkedBags: 4, distance: 50000, travelers: 3)