Skip to content
Open
Show file tree
Hide file tree
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
Binary file added .DS_Store
Binary file not shown.
115 changes: 110 additions & 5 deletions AirportDepartures.playground/Contents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,55 @@ 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 = "On Schedule"
case canceled = "Canceled"
case delayed = "Delayed"
case boarding = "Boarding"
}

struct Airport {
let name: String
let code: String
let city: String
let state: String
let country: String
}

struct Flight {
let destination: String
let airlineName: String
let flightCode: String
let departureTime: Date?
let terminal: String?
let status: FlightStatus
}

class DepartureBoard {
var flights: [Flight]

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

func alertPassengers() {
for eachFlight in departureBoard.flights {
switch eachFlight.status {
case .canceled:
print("We're sorry your flight to \(eachFlight.destination) was canceled, here is a $500 voucher");
case .scheduled:
print("Your flight to \(eachFlight.destination) is scheduled to depart at \(eachFlight.departureTime?.description ?? "TBD")) from terminal: \(String(describing: eachFlight.terminal ?? "TBD"))");
case .boarding:
print("Your flight is boarding, please head to terminal: \(String(describing: eachFlight.departureTime)) immediately. The doors are closing soon.");
case .delayed:
print("Your flight has been delayed.");
default:
print("Your flight will arrive soon.")
}
}
}
}

//: ## 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 +76,23 @@ 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 jfkAirport = Airport(name: "John F. Kenney Airport",
code: "JFK",
city: "New York City",
state: "New York",
country: "USA")
var flight1 = Flight(destination: jfkAirport.code, airlineName: "Southwest", flightCode: "SW344", departureTime: Date(), terminal: "4", status: .scheduled)
var flight2 = Flight(destination: jfkAirport.code, airlineName: "Alaska", flightCode: "AK9739", departureTime: nil, terminal: "8", status: .canceled)
var flight3 = Flight(destination: jfkAirport.code, airlineName: "Delta", flightCode: "DL889", departureTime: Date(), terminal: nil, status: .delayed)



var threeNewFlights = [Flight]()
threeNewFlights.append(flight1)
threeNewFlights.append(flight2)
threeNewFlights.append(flight3)

let departureBoard = DepartureBoard(flights: threeNewFlights)

//: ## 3. Create a free-standing function that can print the flight information from the `DepartureBoard`
//: a. Use the function signature: `printDepartures(departureBoard:)`
Expand All @@ -43,6 +105,23 @@ import UIKit



func printDepartures(departureBoard: DepartureBoard) {
for eachFlight in departureBoard.flights {
if let unwrappedDepartureTime = eachFlight.departureTime, let unwrappedTerminal = eachFlight.terminal {
print("Destination: \(eachFlight.destination) Airline: \(eachFlight.airlineName) Flight Number: \(eachFlight.flightCode) Departure Time: \(unwrappedDepartureTime) Terminal: \(unwrappedTerminal) Status: \(eachFlight.status.rawValue)")
} else {
if let unwrappedDepartureTime = eachFlight.departureTime {
print("Destination: \(eachFlight.destination) Airline: \(eachFlight.airlineName) Flight Number: \(eachFlight.flightCode) Departure Time: \(unwrappedDepartureTime) Terminal: \(eachFlight.terminal ?? "") Status: \(eachFlight.status.rawValue)")
} else {
if let unwrappedTerminal = eachFlight.terminal {
print("Destination: \(eachFlight.destination) Airline: \(eachFlight.airlineName) Flight Number: \(eachFlight.flightCode) Departure Time: \("") \(unwrappedTerminal) Status: \(eachFlight.status.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,6 +137,23 @@ 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 eachFlight in departureBoard.flights {
if let unwrappedDepartureTime = eachFlight.departureTime, let unwrappedTerminal = eachFlight.terminal {
print("Destination: \(eachFlight.destination) Airline: \(eachFlight.airlineName) Flight Number: \(eachFlight.flightCode) Departure Time: \(unwrappedDepartureTime) Terminal: \(unwrappedTerminal) Status: \(eachFlight.status.rawValue)")
} else {
if let unwrappedDepartureTime = eachFlight.departureTime {
print("Destination: \(eachFlight.destination) Airline: \(eachFlight.airlineName) Flight Number: \(eachFlight.flightCode) Departure Time: \(unwrappedDepartureTime) Terminal: \(eachFlight.terminal ?? "") Status: \(eachFlight.status.rawValue)")
} else {
if let unwrappedTerminal = eachFlight.terminal {
print("Destination: \(eachFlight.destination) Airline: \(eachFlight.airlineName) Flight Number: \(eachFlight.flightCode) Departure Time: \("") \(unwrappedTerminal) Status: \(eachFlight.status.rawValue)")
}
}
}
}
}

printDepartures2(departureBoard: departureBoard)



Expand All @@ -75,7 +171,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.

departureBoard.alertPassengers()



Expand All @@ -96,6 +192,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 checkedBagTotal = checkedBags*25
let distanceTotal = distance/10
let travelersTotal = travelers*500
let total: Double = Double(checkedBagTotal + distanceTotal + travelersTotal)
return(total)
}

print(calculateAirfare(checkedBags: 4, distance: 1000, travelers: 2))
print(calculateAirfare(checkedBags: 8, distance: 100, travelers: 8))
print(calculateAirfare(checkedBags: 3, distance: 5320, travelers: 3))