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
157 changes: 151 additions & 6 deletions AirportDepartures.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,103 @@ 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 {
case EnRoute
case Schedule
case Canceled
case Delayed
case Landed
case Boarding
}

struct Airport {
var destination: String
}

struct Flight {
let airport: Airport
var airline: String
var flightNumber: String
var departureTime: Date?
var terminal: String?
var fightStatus: FlightStatus
}

class DepartureBoard {
var departureFlights: [Flight]
var currentAirport: String

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


func passengerAlerts() {

Choose a reason for hiding this comment

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

Not a big deal, but this function has an extra level of indentation that isn't needed. The further you can keep stuff to the left, the less chance you have of line wrapping.


let dateFormat = DateFormatter()
dateFormat.timeStyle = .medium
dateFormat.dateStyle = .none


for flight in departureFlights {

switch flight.fightStatus {
case .Schedule:
var alert = "Your flight to \(flight.airport.destination) is schedule to"

if let departure = flight.departureTime {
alert += "depart at \(dateFormat.string(from: departure))"
} else {
alert += "TBD"
}

if let terminalNum = flight.terminal {
alert += "terminal \(terminalNum)"
} else {
alert += "TBD"
}
print(alert)

case .Canceled:
print("We're sorry your flight to \(flight.airport.destination) was canceled, here is a $500 voucher")

case .Boarding:
var alert = "Your flight is boarding, please head to"

if let terminalNum = flight.terminal {
alert += "terminal: \(terminalNum) immediately. The doors are closing soon."
} else {
alert += "TBD"
}
print(alert)

case .Delayed:
var alert = "Your flight is delayed. Please be patient and grab a cup of coffee."

if let departure = flight.departureTime {
alert += "depart at \(dateFormat.string(from: departure))"
} else {
alert += "TBD"
}

if let terminalNum = flight.terminal {
alert += "terminal \(terminalNum)"
} else {
alert += "TBD"
}
print(alert)

default:
continue
}

}

}


} // END Class

Choose a reason for hiding this comment

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

Nice use of a comment to mark the end of your class.




Expand All @@ -29,6 +126,13 @@ 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 miamiFlight = Flight(airport: Airport(destination: "New York"), airline: "Delta", flightNumber: "DL2345", departureTime: Date(), terminal: "6", fightStatus: .Boarding)

let AtlantaFlight = Flight(airport: Airport(destination: "West Palm"), airline: "Spirit", flightNumber: "SP4789", departureTime: nil, terminal: "4", fightStatus: .Canceled)

let TampaFlight = Flight(airport: Airport(destination: "Houston"), airline: "American Airlines", flightNumber: "AA5614", departureTime: Date(), terminal: nil, fightStatus: .Delayed)

let myDepartureFlight = DepartureBoard(departureFlights: [miamiFlight, AtlantaFlight, TampaFlight], currentAirport: "Miami")



Expand All @@ -40,9 +144,31 @@ 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 printDeparture(departureBoard: DepartureBoard) {

let dateFormat = DateFormatter()
dateFormat.timeStyle = .short
dateFormat.dateStyle = .none

for departure in departureBoard.departureFlights {

var time = ""
var terminal = ""

if let departureTime = departure.departureTime {

time = dateFormat.string(from: departureTime)
}

if let terminalNum = departure.terminal {
terminal = "\(terminalNum)"
}
print("\nDestination \(departure.airport.destination) \nAirline: \(departure.airline) Flight: \(departure.flightNumber) Departure Time: \(time) Terminal: \(terminal) Flight Status: \(departure.fightStatus)")
}
Comment on lines +153 to +167

Choose a reason for hiding this comment

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

Nice use of variables that are providing default values in case you can't unwrap the optionals. Very good approach.


} // END

printDeparture(departureBoard: myDepartureFlight)

//: ## 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 @@ -59,7 +185,7 @@ import UIKit
//: 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


// Already did inside the above printDeparture function

//: ## 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 @@ -76,7 +202,7 @@ import UIKit
//:
//: f. Stretch: Display a custom message if the `terminal` is `nil`, tell the traveler to see the nearest information desk for more details.


myDepartureFlight.passengerAlerts()


//: ## 6. Create a free-standing function to calculate your total airfair for checked bags and destination
Expand All @@ -97,5 +223,24 @@ import UIKit
//:
//: 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 bagsCost = checkedBags * 25
let ticket = Double(distance) * 0.10
let totalCost = Double(bagsCost) + ticket * Double(travelers)

return totalCost

}


let tripOne = calculateAirfare(checkedBags: 2, distance: 1000, travelers: 3)

// Number Formatter stretch!
let usCurrencyFormatter = NumberFormatter()
usCurrencyFormatter.numberStyle = .currency
if let price = usCurrencyFormatter.string(from: NSNumber(floatLiteral: tripOne))
{
print(price)
}
Comment on lines +239 to +245

Choose a reason for hiding this comment

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

Great work figuring out the number formatter!