-
Notifications
You must be signed in to change notification settings - Fork 241
Completed Project #173
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?
Completed Project #173
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,9 +16,62 @@ 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" | ||
| case Boarding = "Boarding" | ||
| } | ||
|
|
||
| struct Airport { | ||
| let name: String | ||
| } | ||
|
|
||
| struct Flight { | ||
| var status: FlightStatus | ||
| var terminal: String? //Terminal "waiting to arrive on time" or # | ||
| var time: Date? | ||
| var destination: String | ||
| var airline: String | ||
| var flightID: String | ||
|
|
||
| } | ||
|
|
||
| class DepartureBoard { | ||
| let currentAirport: Airport | ||
| var departureFlights: [Flight] | ||
|
|
||
| init(currentAirport: Airport) { | ||
| self.currentAirport = currentAirport | ||
| self.departureFlights = [] | ||
| } | ||
|
|
||
| func upcomingFlightAlert(flight: Flight) { | ||
| switch flight.status { | ||
| case .Boarding: | ||
| if let flightTerminal = flight.terminal { | ||
| print("Your flight is boarding, please head to terminal: \(flightTerminal) immediately. The doors are closing soon.") | ||
| } else { | ||
| print("See the nearest information desk for more details.") | ||
| } | ||
| case .Canceled: | ||
| print("We're sorry your flight to (city) was canceled, here is a $500 voucher") | ||
| case .Delayed: | ||
| print("We're sorry, your flight is delayed.") | ||
| case .EnRoute: | ||
| print("Your flight is en route.") | ||
| case .Scheduled: | ||
| if let flightTime = flight.time { | ||
| if let flightTerminal = flight.terminal { | ||
| print("Your flight to \(flight.destination) is scheduled to depart at \(flightTime) from terminal: \(flightTerminal)") | ||
| } else { | ||
| print("See the nearest information desk for more details.") | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+51
to
+72
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 switch statement! I will say that convention in Swift is for the cases to be at the same indentation as the switch keyword. ctrl-i in Xcode produces this: func upcomingFlightAlert(flight: Flight) {
switch flight.status {
case .Boarding:
if let flightTerminal = flight.terminal {
print("Your flight is boarding, please head to terminal: \(flightTerminal) immediately. The doors are closing soon.")
} else {
print("See the nearest information desk for more details.")
}
case .Canceled:
print("We're sorry your flight to (city) was canceled, here is a $500 voucher")
case .Delayed:
print("We're sorry, your flight is delayed.")
case .EnRoute:
print("Your flight is en route.")
case .Scheduled:
if let flightTime = flight.time {
if let flightTerminal = flight.terminal {
print("Your flight to \(flight.destination) is scheduled to depart at \(flightTime) from terminal: \(flightTerminal)")
} else {
print("See the nearest information desk for more details.")
}
}
}
} |
||
| } | ||
| } | ||
| //: ## 2. Create 3 flights and add them to a departure board | ||
| //: a. For the departure time, use `Date()` for the current time | ||
| //: | ||
|
|
@@ -29,9 +82,22 @@ 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 | ||
|
|
||
|
|
||
|
|
||
| //create the airport and the departure board | ||
| let NYC_airport = Airport(name: "JFK Airport in NYC") | ||
| var NYC_departureBoard = DepartureBoard(currentAirport: NYC_airport) | ||
|
|
||
| //create the flights | ||
| let orlandoFlight = Flight(status: .Scheduled, terminal: "7", | ||
| time: Date(), destination: "Orlando", airline: "Delta Air Lines", flightID: "KL 6966") | ||
|
Comment on lines
+90
to
+91
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. I appreciate the return to keep it from line wrapping. Thoughtful. |
||
| let atlantaFlight = Flight(status: .Canceled, terminal: nil, time: nil, destination: "Atlanta", | ||
| airline: "Jet Blue Airways", flightID: "B6 586") | ||
| let dallasFlight = Flight(status: .EnRoute, terminal: "4", | ||
| time: Date(), destination: "Dallas", airline: "KLM", flightID: "KL 6966") | ||
|
|
||
| //add the flights to the departure board | ||
| NYC_departureBoard.departureFlights.append(atlantaFlight) | ||
| NYC_departureBoard.departureFlights.append(orlandoFlight) | ||
| NYC_departureBoard.departureFlights.append(dallasFlight) | ||
| //: ## 3. Create a free-standing function that can print the flight information from the `DepartureBoard` | ||
| //: a. Use the function signature: `printDepartures(departureBoard:)` | ||
| //: | ||
|
|
@@ -40,9 +106,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.departureFlights { | ||
| print("The Flight is \(flight.status.rawValue)") | ||
|
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 |
||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| printDepartures(departureBoard: NYC_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 +128,18 @@ 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.departureFlights { | ||
| let flightTerminal = flight.terminal ?? "" | ||
| if let departureTime = flight.time { | ||
| print("Destination: \(flight.destination) Airline: \(flight.airline) Flight: \(flight.flightID) Departure Time: \(departureTime) Terminal: \(flightTerminal) Status: \(flight.status.rawValue)") | ||
| } else { | ||
| print("Destination: \(flight.destination) Airline: \(flight.airline) Flight: \(flight.flightID) Departure Time: Terminal: \(flightTerminal) Status: \(flight.status.rawValue)") | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+131
to
+140
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 what you came up with here. Very clean with only two print statements and using the nil coalescing operator. |
||
|
|
||
| printDepartures2(departureBoard: NYC_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,7 +154,9 @@ 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 NYC_departureBoard.departureFlights { | ||
| NYC_departureBoard.upcomingFlightAlert(flight: flight) | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
@@ -96,6 +177,22 @@ 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 bagCost: Double = 25.00 | ||
| let costPerMile: Double = 0.10 | ||
| let totalBagCost = Double(checkedBags) * bagCost | ||
| let totalMileCost = Double(distance) * costPerMile | ||
| let airfareTotal: Double = (totalBagCost + totalMileCost) * Double(travelers) | ||
| return airfareTotal | ||
| } | ||
|
Comment on lines
+180
to
+187
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. Very clear and easy to understand your logic here. |
||
|
|
||
| func convertDoubleToCurrency(amount: Double) -> String { | ||
| let numberFormatter = NumberFormatter() | ||
| numberFormatter.numberStyle = .currency | ||
| numberFormatter.locale = Locale.current | ||
| return numberFormatter.string(from: NSNumber(value: amount)) ?? "" | ||
| } | ||
|
Comment on lines
+189
to
+194
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. Hell yeah! Great helper function! Way to think like a developer. |
||
|
|
||
| print(convertDoubleToCurrency(amount: calculateAirfare(checkedBags: 2, distance: 2000, travelers: 3))) | ||
|
|
||
|
|
||
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.
Not a huge deal, but enum cases should be lower cased, as they are not a type: