From b42c17d45d321df11079ea208398dd05ba6035a4 Mon Sep 17 00:00:00 2001 From: Donna Mayfield Date: Tue, 12 May 2020 19:11:05 -0700 Subject: [PATCH 1/3] Add parts 1, 2 and 3 --- AirportDepartures.playground/Contents.swift | 82 +++++++++++++++++++-- 1 file changed, 77 insertions(+), 5 deletions(-) diff --git a/AirportDepartures.playground/Contents.swift b/AirportDepartures.playground/Contents.swift index 98e2131..ff22256 100644 --- a/AirportDepartures.playground/Contents.swift +++ b/AirportDepartures.playground/Contents.swift @@ -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 = [] + } + + 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 @@ -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) + +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:)` @@ -40,8 +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 From 0846729745f4e380a3c7ea41d664fe0f9495fc0f Mon Sep 17 00:00:00 2001 From: Donna Mayfield Date: Tue, 12 May 2020 19:43:13 -0700 Subject: [PATCH 2/3] Add parts 4, 5, 6, revised 3 then changed it back --- AirportDepartures.playground/Contents.swift | 43 +++++++++++++++++++-- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/AirportDepartures.playground/Contents.swift b/AirportDepartures.playground/Contents.swift index ff22256..a687d5f 100644 --- a/AirportDepartures.playground/Contents.swift +++ b/AirportDepartures.playground/Contents.swift @@ -130,9 +130,17 @@ printDepartures(departureBoard: departureBoard) //: 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" //: @@ -147,8 +155,23 @@ printDepartures(departureBoard: departureBoard) //: 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 { + 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.") + } + } +} +departureBoard.alertPassengers() //: ## 6. Create a free-standing function to calculate your total airfair for checked bags and destination @@ -168,6 +191,18 @@ printDepartures(departureBoard: departureBoard) //: 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) From f61629f0db9037784363ca15796085bcc8b78714 Mon Sep 17 00:00:00 2001 From: Donna Mayfield Date: Tue, 12 May 2020 19:55:41 -0700 Subject: [PATCH 3/3] Review file with corrections --- AirportDepartures.playground/Contents.swift | 2 -- 1 file changed, 2 deletions(-) diff --git a/AirportDepartures.playground/Contents.swift b/AirportDepartures.playground/Contents.swift index a687d5f..86a64c5 100644 --- a/AirportDepartures.playground/Contents.swift +++ b/AirportDepartures.playground/Contents.swift @@ -115,7 +115,6 @@ func printDepartures(departureBoard: DepartureBoard) { 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 //: @@ -173,7 +172,6 @@ for flight in departureBoard.flights { } 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` //: