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
123 changes: 110 additions & 13 deletions AirportDepartures.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,62 @@ 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 Scheduled = "Scheduled"
case Canceled = "Canceled"
case Delayed = "Delayed"
case Boarding = "Boarding"
Comment on lines +20 to +24

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a huge deal, but enum cases should be lower cased, as they are not a type:

enum FlightStatus: String {
    case enRoute = "En Route"
    case scheduled = "Scheduled"
    case canceled = "Canceled"
    case delayed = "Delayed"
    case boarding = "Boarding"
}

}

struct Airport {
let name: String
}

struct Flight {
var status: FlightStatus
var terminal: String? //Terminal "waiting to arrive on time" or #
var time: Date?
var destination: String
var airline: String
var flightID: String

}

class DepartureBoard {
let currentAirport: Airport
var departureFlights: [Flight]

init(currentAirport: Airport) {
self.currentAirport = currentAirport
self.departureFlights = []
}

func upcomingFlightAlert(flight: Flight) {
switch flight.status {
case .Boarding:
if let flightTerminal = flight.terminal {
print("Your flight is boarding, please head to terminal: \(flightTerminal) immediately. The doors are closing soon.")
} else {
print("See the nearest information desk for more details.")
}
case .Canceled:
print("We're sorry your flight to (city) was canceled, here is a $500 voucher")
case .Delayed:
print("We're sorry, your flight is delayed.")
case .EnRoute:
print("Your flight is en route.")
case .Scheduled:
if let flightTime = flight.time {
if let flightTerminal = flight.terminal {
print("Your flight to \(flight.destination) is scheduled to depart at \(flightTime) from terminal: \(flightTerminal)")
} else {
print("See the nearest information desk for more details.")
}
}
}
Comment on lines +51 to +72

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice switch statement! I will say that convention in Swift is for the cases to be at the same indentation as the switch keyword. ctrl-i in Xcode produces this:

func upcomingFlightAlert(flight: Flight) {
    switch flight.status {
    case .Boarding:
        if let flightTerminal = flight.terminal {
            print("Your flight is boarding, please head to terminal: \(flightTerminal) immediately. The doors are closing soon.")
        } else {
            print("See the nearest information desk for more details.")
        }
    case .Canceled:
        print("We're sorry your flight to (city) was canceled, here is a $500 voucher")
    case .Delayed:
        print("We're sorry, your flight is delayed.")
    case .EnRoute:
        print("Your flight is en route.")
    case .Scheduled:
        if let flightTime = flight.time {
            if let flightTerminal = flight.terminal {
                print("Your flight to \(flight.destination) is scheduled to depart at \(flightTime) from terminal: \(flightTerminal)")
            } else {
                print("See the nearest information desk for more details.")
            }
        }
    }
}

}
}
//: ## 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,9 +82,22 @@ 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



//create the airport and the departure board
let NYC_airport = Airport(name: "JFK Airport in NYC")
var NYC_departureBoard = DepartureBoard(currentAirport: NYC_airport)

//create the flights
let orlandoFlight = Flight(status: .Scheduled, terminal: "7",
time: Date(), destination: "Orlando", airline: "Delta Air Lines", flightID: "KL 6966")
Comment on lines +90 to +91

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the return to keep it from line wrapping. Thoughtful.

let atlantaFlight = Flight(status: .Canceled, terminal: nil, time: nil, destination: "Atlanta",
airline: "Jet Blue Airways", flightID: "B6 586")
let dallasFlight = Flight(status: .EnRoute, terminal: "4",
time: Date(), destination: "Dallas", airline: "KLM", flightID: "KL 6966")

//add the flights to the departure board
NYC_departureBoard.departureFlights.append(atlantaFlight)
NYC_departureBoard.departureFlights.append(orlandoFlight)
NYC_departureBoard.departureFlights.append(dallasFlight)
//: ## 3. Create a free-standing function that can print the flight information from the `DepartureBoard`
//: a. Use the function signature: `printDepartures(departureBoard:)`
//:
Expand All @@ -40,9 +106,13 @@ import UIKit
//: c. Make your `FlightStatus` enum conform to `String` so you can print the `rawValue` String values from the `enum`. See the [enum documentation](https://docs.swift.org/swift-book/LanguageGuide/Enumerations.html).
//:
//: d. Print out the current DepartureBoard you created using the function
func printDepartures(departureBoard: DepartureBoard) {
for flight in departureBoard.departureFlights {
print("The Flight is \(flight.status.rawValue)")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of .rawValue

}
}



printDepartures(departureBoard: NYC_departureBoard)

//: ## 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 @@ -58,9 +128,18 @@ import UIKit
//: Destination: Los Angeles Airline: Delta Air Lines Flight: KL 6966 Departure Time: Terminal: 4 Status: Canceled
//: Destination: Rochester Airline: Jet Blue Airways Flight: B6 586 Departure Time: 1:26 PM Terminal: Status: Scheduled
//: Destination: Boston Airline: KLM Flight: KL 6966 Departure Time: 1:26 PM Terminal: 4 Status: Scheduled



func printDepartures2(departureBoard: DepartureBoard) {
for flight in departureBoard.departureFlights {
let flightTerminal = flight.terminal ?? ""
if let departureTime = flight.time {
print("Destination: \(flight.destination) Airline: \(flight.airline) Flight: \(flight.flightID) Departure Time: \(departureTime) Terminal: \(flightTerminal) Status: \(flight.status.rawValue)")
} else {
print("Destination: \(flight.destination) Airline: \(flight.airline) Flight: \(flight.flightID) Departure Time: Terminal: \(flightTerminal) Status: \(flight.status.rawValue)")
}
}
}
Comment on lines +131 to +140

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love what you came up with here. Very clean with only two print statements and using the nil coalescing operator.


printDepartures2(departureBoard: NYC_departureBoard)
//: ## 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 +154,9 @@ 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.

for flight in NYC_departureBoard.departureFlights {
NYC_departureBoard.upcomingFlightAlert(flight: flight)
}



Expand All @@ -96,6 +177,22 @@ 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 bagCost: Double = 25.00
let costPerMile: Double = 0.10
let totalBagCost = Double(checkedBags) * bagCost
let totalMileCost = Double(distance) * costPerMile
let airfareTotal: Double = (totalBagCost + totalMileCost) * Double(travelers)
return airfareTotal
}
Comment on lines +180 to +187

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very clear and easy to understand your logic here.


func convertDoubleToCurrency(amount: Double) -> String {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .currency
numberFormatter.locale = Locale.current
return numberFormatter.string(from: NSNumber(value: amount)) ?? ""
}
Comment on lines +189 to +194

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hell yeah! Great helper function! Way to think like a developer.


print(convertDoubleToCurrency(amount: calculateAirfare(checkedBags: 2, distance: 2000, travelers: 3)))