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
129 changes: 117 additions & 12 deletions AirportDepartures.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,46 @@ 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
case scheduled
case canceled
case delayed
case landed
case diverted
case boarding
}

struct Airport {
let city: String
var destination: String
}

struct Flight {
var flightNo: String
var airline: String
var departTime: Date?
var terminal: String?
var flightStatus: FlightStatus
}

class DepartureBoard {
var flights: [Flight]
var airport: Airport

init(city: String, destination: Airport) {
self.airport = destination
self.flights = []
}
Comment on lines +46 to +49

Choose a reason for hiding this comment

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

You pass in a String to the parameter city but you don't use that anywhere in your initializer. Consider instead passing in an array of flights in addition to the airport, in case you wanted to initialize the departure board with some airports from the start.

init(flights: [Flight], airport: Airport) {
    self.airport = airport
    self.flights = flights
}


func alertPassengers() {
print("Passenger alert")
}
}

let airport1 = Airport(city: "Atlanta", destination: "JFK")
let airport2 = Airport(city: "New York", destination: "LAX")
let airport3 = Airport(city: "Los Angeles", destination: "BOS")

//: ## 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,8 +67,37 @@ 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 today = Date()
let format = DateFormatter()
format.dateStyle = .medium
print(format.string(from: today))

let flight1 = Flight(
flightNo: "DL1104",
airline: "Delta",
departTime: Date(),
terminal: nil,
flightStatus: .landed)

let flight2 = Flight(
flightNo: "AA302",
airline: "American Airline",
departTime: Date(),
terminal: "4",
flightStatus: .enRoute)

let flight3 = Flight(
flightNo: "DL5675",
airline: "Delta",
departTime: nil,
terminal: nil,
flightStatus: .canceled)
Comment on lines +75 to +94

Choose a reason for hiding this comment

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

Nice use of returns on the parameters to make things read very clearly.


let departureBoard = DepartureBoard(city: "Atlanta", destination: airport1)

departureBoard.flights.append(flight1)
departureBoard.flights.append(flight2)
departureBoard.flights.append(flight3)

//: ## 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 +107,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.flights {
print("Airline \(flight.airline), Departs: \(String(describing: flight.departTime)), FlightNo: \(flight.flightNo), FlightStatus: \(flight.flightStatus.rawValue)")
}
}



printDepartures(departureBoard: 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 +129,17 @@ 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.flights {
if flight.departTime != nil, flight.terminal != nil {
print("Airline: \(flight.airline), Departs: \(String(describing: flight.departTime)), Flight No: \(flight.flightNo), Status: \(flight.flightStatus)")
} else {
print("Airline \(flight.airline), Departs: \(" "), FlightNO: \(flight.flightNo), Status: \(flight.flightStatus)")
}
}
}

printDepartures2(departureBoard: 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,9 +154,23 @@ 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 departureBoard.flights {

Choose a reason for hiding this comment

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

This loop should ideally go inside of the alertPasengers() function in your departure board class. The instructions are confusing on this though, as the step is listed way down here, yet your class is up at the top. I definitely won't hold it against you, but just thought you should know.

if flight.terminal == nil {
print("Please go to the nearest information desk for flight status.")
} else {
switch flight.flightStatus {
case .canceled:
print("We're sorry your flight to \(String(describing: flight.terminal)) was canceled, here is a $500 voucher")
case .scheduled:
print("Your flight to \(flight.airline) is scheduled to depart at \(String(describing: flight.departTime)) from terminal: \(String(describing: flight.terminal))")
case .boarding:
print("Your flight is boarding soon, please head to terminal: \(String(describing: flight.terminal)) immediately. The doors are closing soon.")
default:
print("Please check the departure board for your flight status.")
}
Comment on lines +161 to +170

Choose a reason for hiding this comment

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

Nice use of a switch statement. This looks great!

}
}
departureBoard.alertPassengers()

//: ## 6. Create a free-standing function to calculate your total airfair for checked bags and destination
//: Use the method signature, and return the airfare as a `Double`
Expand All @@ -96,6 +189,18 @@ 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.
var costPerBag = 25.00
var costPerMile = 0.10

func calcFare(checkedBags: Double, distance: Double, travelers: Int) -> Double {
let ticketCost = (checkedBags * costPerBag) + (distance * costPerMile)
let totalFare = ticketCost * Double(travelers)
return totalFare
}

let formatter = NumberFormatter()
formatter.numberStyle = .currency

print(formatter.string(from: NSNumber(value: calcFare(checkedBags: 10, distance: 1000, travelers: 4))) ?? 0)
print(formatter.string(from: NSNumber(value: calcFare(checkedBags: 5, distance: 2000, travelers: 1))) ?? 0)
print(formatter.string(from: NSNumber(value: calcFare(checkedBags: 1, distance: 1500, travelers: 1))) ?? 0)
Comment on lines +204 to +206

Choose a reason for hiding this comment

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

Love that you used the number formatter, as well as the nil coalescing operator.