-
Notifications
You must be signed in to change notification settings - Fork 241
Donna Mayfield Airport Playground Project #163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Comment on lines
+75
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:)` | ||
|
|
@@ -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 | ||
|
|
@@ -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" | ||
| //: | ||
|
|
@@ -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 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This loop should ideally go inside of the |
||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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` | ||
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
There was a problem hiding this comment.
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
citybut 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.