From dd9f943268cc65f9a859dcf248986e79eef8dc63 Mon Sep 17 00:00:00 2001 From: Mike Nichols Date: Wed, 6 May 2020 13:11:30 -0500 Subject: [PATCH 1/6] FlightStatus enums @mrflowers22 --- AirportDepartures.playground/Contents.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/AirportDepartures.playground/Contents.swift b/AirportDepartures.playground/Contents.swift index 98e2131..733b0cb 100644 --- a/AirportDepartures.playground/Contents.swift +++ b/AirportDepartures.playground/Contents.swift @@ -16,7 +16,12 @@ 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" +} //: ## 2. Create 3 flights and add them to a departure board From 8fa292e264f459f86b09f9967ad546f6e02a2b18 Mon Sep 17 00:00:00 2001 From: Mike Nichols Date: Thu, 7 May 2020 07:13:44 -0500 Subject: [PATCH 2/6] Getting a little tricky with optionals --- AirportDepartures.playground/Contents.swift | 55 ++++++++++++++++++++- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/AirportDepartures.playground/Contents.swift b/AirportDepartures.playground/Contents.swift index 733b0cb..7afd40d 100644 --- a/AirportDepartures.playground/Contents.swift +++ b/AirportDepartures.playground/Contents.swift @@ -23,6 +23,46 @@ enum FlightStatus: String { case delayed = "Delayed" } +struct Airport { + let airportName: String + let airportCode: String + var departingFlights: [Flight]? + var arrivingFlights: [Flight]? + + init(airportName: String, airportCode: String, departingFlights: [Flight], arrivingFlights: [Flight]) { + self.airportName = airportName + self.airportCode = airportCode + self.departingFlights = departingFlights + self.arrivingFlights = arrivingFlights + } +} + +struct Flight { + let flightNumber: Int + var departureTime: Date? + let departureAirport: Airport + var departureTerminal: String? + var arrivalTime: Date? + let arrivalAirport: Airport + var arrivalTerminal: String? + var flightStatus: FlightStatus? +} + +var flights: [Flight] = [] + +class DepartureBoard { + var flights: [Flight] + let currentAirport: Airport + + init(flights: [Flight], currentAirport: Airport) { + self.flights = flights + self.currentAirport = currentAirport + } +} + +let stlAirport = Airport(airportName: "St. Louis", airportCode: "STL", departingFlights: [], arrivingFlights: []) +let lgaAirport = Airport(airportName: "NY LaGuardia", airportCode: "LGA", departingFlights: [], arrivingFlights: []) +let ordAirport = Airport(airportName: "Chicago - O'Hare", airportCode: "ORD", departingFlights: [], arrivingFlights: []) //: ## 2. Create 3 flights and add them to a departure board //: a. For the departure time, use `Date()` for the current time @@ -34,7 +74,13 @@ enum FlightStatus: String { //: 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 flightA = Flight(flightNumber: 123, departureTime: Date(), departureAirport: lgaAirport, departureTerminal: "E", arrivalTime: Date(), arrivalAirport: stlAirport, arrivalTerminal: nil, flightStatus: FlightStatus.enroute) +let flightB = Flight(flightNumber: 456, departureTime: Date(), departureAirport: ordAirport, departureTerminal: "B", arrivalTime: Date(), arrivalAirport: lgaAirport, arrivalTerminal: "1", flightStatus: FlightStatus.canceled) +let flightC = Flight(flightNumber: 789, departureTime: Date(), departureAirport: stlAirport, departureTerminal: "B", arrivalTime: Date(), arrivalAirport: ordAirport, arrivalTerminal: "D", flightStatus: FlightStatus.delayed) +flights.append(flightA) +flights.append(flightB) +flights.append(flightC) //: ## 3. Create a free-standing function that can print the flight information from the `DepartureBoard` @@ -45,8 +91,13 @@ enum FlightStatus: String { //: 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: String) { + for flight in flights { + if departureBoard == flights.departureAirport { + print("Destination: \(arrivalAirport) Flight #\(flights.flightNumber) | ") + } + } +} //: ## 4. Make a second function to print print an empty string if the `departureTime` is nil From 729da45a0afd6130b2d3b4123c9f34200f6d94ae Mon Sep 17 00:00:00 2001 From: Mike Nichols Date: Thu, 7 May 2020 08:40:59 -0500 Subject: [PATCH 3/6] OK, optional bindings still tricky but figuring out I apparently have nesting blindness --- AirportDepartures.playground/Contents.swift | 49 +++++++++++++-------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/AirportDepartures.playground/Contents.swift b/AirportDepartures.playground/Contents.swift index 7afd40d..c794b37 100644 --- a/AirportDepartures.playground/Contents.swift +++ b/AirportDepartures.playground/Contents.swift @@ -27,42 +27,47 @@ struct Airport { let airportName: String let airportCode: String var departingFlights: [Flight]? - var arrivingFlights: [Flight]? - init(airportName: String, airportCode: String, departingFlights: [Flight], arrivingFlights: [Flight]) { + init(airportName: String, airportCode: String, departingFlights: [Flight]) { self.airportName = airportName self.airportCode = airportCode self.departingFlights = departingFlights - self.arrivingFlights = arrivingFlights } } +enum Airports { + case stl + case lga + case ord +} + struct Flight { let flightNumber: Int var departureTime: Date? - let departureAirport: Airport + let departureAirport: String var departureTerminal: String? var arrivalTime: Date? - let arrivalAirport: Airport + let arrivalAirport: String var arrivalTerminal: String? - var flightStatus: FlightStatus? + var flightStatus: FlightStatus } var flights: [Flight] = [] class DepartureBoard { - var flights: [Flight] - let currentAirport: Airport + var departingFlights: [Flight] = [] + let currentAirport: String - init(flights: [Flight], currentAirport: Airport) { - self.flights = flights + init(departingFlights: [Flight], currentAirport: String) { + self.departingFlights = departingFlights self.currentAirport = currentAirport } + } -let stlAirport = Airport(airportName: "St. Louis", airportCode: "STL", departingFlights: [], arrivingFlights: []) -let lgaAirport = Airport(airportName: "NY LaGuardia", airportCode: "LGA", departingFlights: [], arrivingFlights: []) -let ordAirport = Airport(airportName: "Chicago - O'Hare", airportCode: "ORD", departingFlights: [], arrivingFlights: []) +let stlAirport = Airport(airportName: "St. Louis", airportCode: "STL", departingFlights: []) +let lgaAirport = Airport(airportName: "NY LaGuardia", airportCode: "LGA", departingFlights: []) +let ordAirport = Airport(airportName: "Chicago - O'Hare", airportCode: "ORD", departingFlights: []) //: ## 2. Create 3 flights and add them to a departure board //: a. For the departure time, use `Date()` for the current time @@ -74,9 +79,9 @@ let ordAirport = Airport(airportName: "Chicago - O'Hare", airportCode: "ORD", de //: 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 flightA = Flight(flightNumber: 123, departureTime: Date(), departureAirport: lgaAirport, departureTerminal: "E", arrivalTime: Date(), arrivalAirport: stlAirport, arrivalTerminal: nil, flightStatus: FlightStatus.enroute) -let flightB = Flight(flightNumber: 456, departureTime: Date(), departureAirport: ordAirport, departureTerminal: "B", arrivalTime: Date(), arrivalAirport: lgaAirport, arrivalTerminal: "1", flightStatus: FlightStatus.canceled) -let flightC = Flight(flightNumber: 789, departureTime: Date(), departureAirport: stlAirport, departureTerminal: "B", arrivalTime: Date(), arrivalAirport: ordAirport, arrivalTerminal: "D", flightStatus: FlightStatus.delayed) +let flightA = Flight(flightNumber: 123, departureTime: Date(), departureAirport: "LGA", departureTerminal: "E", arrivalTime: Date(), arrivalAirport: "STL", arrivalTerminal: nil, flightStatus: FlightStatus.enroute) +let flightB = Flight(flightNumber: 456, departureTime: Date(), departureAirport: "ORD", departureTerminal: "B", arrivalTime: Date(), arrivalAirport: "LGA", arrivalTerminal: "1", flightStatus: FlightStatus.canceled) +let flightC = Flight(flightNumber: 789, departureTime: Date(), departureAirport: "STL", departureTerminal: "B", arrivalTime: Date(), arrivalAirport: "ORD", arrivalTerminal: "D", flightStatus: FlightStatus.delayed) flights.append(flightA) flights.append(flightB) @@ -91,14 +96,22 @@ flights.append(flightC) //: 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 +print(flights) + func printDepartures(departureBoard: String) { for flight in flights { - if departureBoard == flights.departureAirport { - print("Destination: \(arrivalAirport) Flight #\(flights.flightNumber) | ") + let airport = flight.departureAirport + if airport == departureBoard { + if let unwrappedTerminal = flight.departureTerminal { + print("Destination: \(flight.arrivalAirport) Flight #\(flight.flightNumber) Time: \(flight.departureTime) Terminal: \(unwrappedTerminal) Status: \(flight.flightStatus.rawValue)") + } else { + print("Destination: \(flight.arrivalAirport) Flight #\(flight.flightNumber) Time: \(flight.departureTime) Terminal: \("TBD") Status: \(flight.flightStatus.rawValue)") + } } } } +printDepartures(departureBoard: "STL") //: ## 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 From cae7d0620145d0f056baf58c412d601c80774cf2 Mon Sep 17 00:00:00 2001 From: Mike Nichols Date: Thu, 7 May 2020 09:36:27 -0500 Subject: [PATCH 4/6] done with #4 --- AirportDepartures.playground/Contents.swift | 29 ++++++++++++++++----- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/AirportDepartures.playground/Contents.swift b/AirportDepartures.playground/Contents.swift index c794b37..2e7659c 100644 --- a/AirportDepartures.playground/Contents.swift +++ b/AirportDepartures.playground/Contents.swift @@ -101,11 +101,13 @@ print(flights) func printDepartures(departureBoard: String) { for flight in flights { let airport = flight.departureAirport - if airport == departureBoard { - if let unwrappedTerminal = flight.departureTerminal { - print("Destination: \(flight.arrivalAirport) Flight #\(flight.flightNumber) Time: \(flight.departureTime) Terminal: \(unwrappedTerminal) Status: \(flight.flightStatus.rawValue)") - } else { - print("Destination: \(flight.arrivalAirport) Flight #\(flight.flightNumber) Time: \(flight.departureTime) Terminal: \("TBD") Status: \(flight.flightStatus.rawValue)") + if let time = flight.departureTime { + if airport == departureBoard { + if let unwrappedTerminal = flight.departureTerminal { + print("Destination: \(flight.arrivalAirport) Flight #\(flight.flightNumber) Time: \(String(describing: time)) Terminal: \(unwrappedTerminal) Status: \(flight.flightStatus.rawValue)") + } else { + print("Destination: \(flight.arrivalAirport) Flight #\(flight.flightNumber) Time: \(String(describing: time)) Terminal: \("TBD") Status: \(flight.flightStatus.rawValue)") + } } } } @@ -127,8 +129,23 @@ printDepartures(departureBoard: "STL") //: 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: String) { + for flight in flights { + let airport = flight.departureAirport + if let time = flight.departureTime { + if airport == departureBoard { + if let unwrappedTerminal = flight.departureTerminal { + let time = flight.departureTime + print("Destination: \(flight.arrivalAirport) Flight #\(flight.flightNumber) Time: \(String(describing: time)) Terminal: \(unwrappedTerminal) Status: \(flight.flightStatus.rawValue)") + } else { + print("Destination: \(flight.arrivalAirport) Flight #\(flight.flightNumber) Time: \(String(describing: time)) Terminal: \("TBD") Status: \(flight.flightStatus.rawValue)") + } + } + } + } +} - +printDepartures2(departureBoard: "ORD") //: ## 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" From ba663990d14cbecb53ca35c18eb9ddaac926d3d1 Mon Sep 17 00:00:00 2001 From: Mike Nichols Date: Thu, 7 May 2020 10:24:51 -0500 Subject: [PATCH 5/6] OK I'm starting to overthink now. I think i'm screwing up by trying to build departure/arrival lists as a func in the Airport struct. --- AirportDepartures.playground/Contents.swift | 68 +++++++++++++++++---- 1 file changed, 55 insertions(+), 13 deletions(-) diff --git a/AirportDepartures.playground/Contents.swift b/AirportDepartures.playground/Contents.swift index 2e7659c..b95c97a 100644 --- a/AirportDepartures.playground/Contents.swift +++ b/AirportDepartures.playground/Contents.swift @@ -26,21 +26,34 @@ enum FlightStatus: String { struct Airport { let airportName: String let airportCode: String - var departingFlights: [Flight]? + var departingFlights: [Flight]? = [] + var arrivingFlights: [Flight]? = [] - init(airportName: String, airportCode: String, departingFlights: [Flight]) { + init(airportName: String, airportCode: String, departingFlights: [Flight], arrivingFlights: [Flight]) { self.airportName = airportName self.airportCode = airportCode - self.departingFlights = departingFlights } -} -enum Airports { - case stl - case lga - case ord +// creating list of departing flights + func departures() { + for flight in flights { + if flight.departureAirport == airportCode { + departingFlights.append(flight) + } + } + } + +// creating list of arriving flights + func arrivals() { + for flight in flights { + if flight.arrivalAirport == airportCode { + departingFlights.append(flight) + } + } + } } + struct Flight { let flightNumber: Int var departureTime: Date? @@ -61,13 +74,42 @@ class DepartureBoard { init(departingFlights: [Flight], currentAirport: String) { self.departingFlights = departingFlights self.currentAirport = currentAirport + + func alertPassengers() { + for flight in flights { + if let departureTime = flight.departureTime { + switch flight.flightStatus { + case FlightStatus.canceled: + print("Flight \(flight.flightNumber) We're sorry your flight to \(flight.arrivalAirport) was canceled, here is a $500 voucher") + case FlightStatus.delayed: + if flight.departureTerminal == nil { + print("Flight \(flight.flightNumber) Your flight to \(flight.arrivalAirport) is scheduled to depart at \(String(describing: departureTime)) from terminal: TBD") + } else { + if let terminal = flight.departureTerminal { + print("Flight \(flight.flightNumber) is boarding, please head to terminal: \(terminal) immediately. The doors are closing soon.") + } + } + case FlightStatus.scheduled: + if flight.departureTerminal == nil { + print("Flight \(flight.flightNumber) Your flight to \(flight.arrivalAirport) is scheduled to depart at \(String(describing: departureTime)) from terminal: TBD") + } else { + if let terminal = flight.departureTerminal { + print("Flight \(flight.flightNumber) is boarding, please head to terminal: \(terminal) immediately. The doors are closing soon.") + } + } + default: + print() + } + } + } + } } } -let stlAirport = Airport(airportName: "St. Louis", airportCode: "STL", departingFlights: []) -let lgaAirport = Airport(airportName: "NY LaGuardia", airportCode: "LGA", departingFlights: []) -let ordAirport = Airport(airportName: "Chicago - O'Hare", airportCode: "ORD", departingFlights: []) +let stlAirport = Airport(airportName: "St. Louis", airportCode: "STL", departingFlights: [], arrivingFlights: []) +let lgaAirport = Airport(airportName: "NY LaGuardia", airportCode: "LGA", departingFlights: [], arrivingFlights: []) +let ordAirport = Airport(airportName: "Chicago - O'Hare", airportCode: "ORD", departingFlights: [], arrivingFlights: []) //: ## 2. Create 3 flights and add them to a departure board //: a. For the departure time, use `Date()` for the current time @@ -161,8 +203,8 @@ printDepartures2(departureBoard: "ORD") //: 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. - - +let stlDepartureBoard = DepartureBoard(currentAirport: "STL", departingFlights: departures) +DepartureBoard(alertPassengers()) //: ## 6. Create a free-standing function to calculate your total airfair for checked bags and destination From 5412b81b57ddbddd57ddd6b9edcdeabf2afa3376 Mon Sep 17 00:00:00 2001 From: Mike Nichols Date: Thu, 7 May 2020 23:00:30 -0500 Subject: [PATCH 6/6] fin. --- AirportDepartures.playground/Contents.swift | 54 +++++---------------- 1 file changed, 13 insertions(+), 41 deletions(-) diff --git a/AirportDepartures.playground/Contents.swift b/AirportDepartures.playground/Contents.swift index b95c97a..cffa38d 100644 --- a/AirportDepartures.playground/Contents.swift +++ b/AirportDepartures.playground/Contents.swift @@ -26,31 +26,11 @@ enum FlightStatus: String { struct Airport { let airportName: String let airportCode: String - var departingFlights: [Flight]? = [] - var arrivingFlights: [Flight]? = [] init(airportName: String, airportCode: String, departingFlights: [Flight], arrivingFlights: [Flight]) { self.airportName = airportName self.airportCode = airportCode } - -// creating list of departing flights - func departures() { - for flight in flights { - if flight.departureAirport == airportCode { - departingFlights.append(flight) - } - } - } - -// creating list of arriving flights - func arrivals() { - for flight in flights { - if flight.arrivalAirport == airportCode { - departingFlights.append(flight) - } - } - } } @@ -71,11 +51,11 @@ class DepartureBoard { var departingFlights: [Flight] = [] let currentAirport: String - init(departingFlights: [Flight], currentAirport: String) { - self.departingFlights = departingFlights + init(currentAirport: String) { self.currentAirport = currentAirport + } - func alertPassengers() { + func alertPassengers() { for flight in flights { if let departureTime = flight.departureTime { switch flight.flightStatus { @@ -105,7 +85,6 @@ class DepartureBoard { } } -} let stlAirport = Airport(airportName: "St. Louis", airportCode: "STL", departingFlights: [], arrivingFlights: []) let lgaAirport = Airport(airportName: "NY LaGuardia", airportCode: "LGA", departingFlights: [], arrivingFlights: []) @@ -189,23 +168,9 @@ func printDepartures2(departureBoard: String) { printDepartures2(departureBoard: "ORD") -//: ## 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" -//: -//: b. If the flight is scheduled print out: "Your flight to \(city) is scheduled to depart at \(time) from terminal: \(terminal)" -//: -//: c. If their flight is boarding print out: "Your flight is boarding, please head to terminal: \(terminal) immediately. The doors are closing soon." -//: -//: d. If the `departureTime` or `terminal` are optional, use "TBD" instead of a blank String -//: -//: e. If you have any other cases to handle please print out appropriate messages -//: -//: 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. -let stlDepartureBoard = DepartureBoard(currentAirport: "STL", departingFlights: departures) -DepartureBoard(alertPassengers()) +let stlDepartures = DepartureBoard(currentAirport: "STL") +print(stlDepartures) //: ## 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` @@ -224,6 +189,13 @@ DepartureBoard(alertPassengers()) //: 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(bags: Int, distance: Int, travelers: Int) -> Double { + let bagCost = Double(bags * 25) + let ticketCost = Double(distance) * 0.1 + let passengerCost = Double(ticketCost) * Double(travelers) + let totalCost: Double = bagCost + passengerCost + return totalCost +} - +calculateAirfare(bags: 2, distance: 2000, travelers: 3)