From 1a64069228b08b097c333a855bf3e0184d8b351b Mon Sep 17 00:00:00 2001 From: Waseem Idelbi Date: Sun, 1 May 2022 14:49:39 -0400 Subject: [PATCH 1/8] =?UTF-8?q?created=20models=20for=20flights=20and=20de?= =?UTF-8?q?parture=20board,=20also=20the=20flight=20status=20enum,=20step?= =?UTF-8?q?=201=20done=20=F0=9F=91=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AirportDepartures.playground/Contents.swift | 26 ++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/AirportDepartures.playground/Contents.swift b/AirportDepartures.playground/Contents.swift index 98e2131..f072299 100644 --- a/AirportDepartures.playground/Contents.swift +++ b/AirportDepartures.playground/Contents.swift @@ -17,7 +17,31 @@ import UIKit //: //: 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" +} + +struct Flight { + var status: FlightStatus + var departureTime: Date? + let destination: String + let flightNumber: String + let airline: String + let terminal: String? +} + +class DepatureBoard { + var flights: [Flight] + var airport: String + + init(flights: [Flight], _ airportName: String) { + self.flights = flights + self.airport = airportName + } +} //: ## 2. Create 3 flights and add them to a departure board //: a. For the departure time, use `Date()` for the current time From 243fe978be75ad2f2a16a143c21c6b2bb99dfea9 Mon Sep 17 00:00:00 2001 From: Waseem Idelbi Date: Thu, 5 May 2022 17:10:41 -0400 Subject: [PATCH 2/8] Created 3 flights and a departure board. Added the flights to the departure board --- AirportDepartures.playground/Contents.swift | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/AirportDepartures.playground/Contents.swift b/AirportDepartures.playground/Contents.swift index f072299..c9c4217 100644 --- a/AirportDepartures.playground/Contents.swift +++ b/AirportDepartures.playground/Contents.swift @@ -53,8 +53,14 @@ class DepatureBoard { //: 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 flight1 = Flight(status: .scheduled, departureTime: Date(), destination: "Chicago", flightNumber: "1", airline: "United", terminal: "1") +let flight2 = Flight(status: .canceled, departureTime: nil, destination: "Dulles", flightNumber: "2", airline: "Delta", terminal: "2") +let flight3 = Flight(status: .enRoute, departureTime: Date(), destination: "Austin", flightNumber: "3", airline: "Turkish Airlines", terminal: nil) + +let depBoard = DepatureBoard(flights: [], "Waseem Airport") +depBoard.flights.append(flight1) +depBoard.flights.append(flight2) +depBoard.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:)` From f42e64afe4b2963a65479f1caa9fe834fad510c8 Mon Sep 17 00:00:00 2001 From: Waseem Idelbi Date: Mon, 9 May 2022 23:03:03 -0400 Subject: [PATCH 3/8] completed the stretch goal --- AirportDepartures.playground/Contents.swift | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/AirportDepartures.playground/Contents.swift b/AirportDepartures.playground/Contents.swift index c9c4217..9e87628 100644 --- a/AirportDepartures.playground/Contents.swift +++ b/AirportDepartures.playground/Contents.swift @@ -53,15 +53,22 @@ class DepatureBoard { //: 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 flight1 = Flight(status: .scheduled, departureTime: Date(), destination: "Chicago", flightNumber: "1", airline: "United", terminal: "1") -let flight2 = Flight(status: .canceled, departureTime: nil, destination: "Dulles", flightNumber: "2", airline: "Delta", terminal: "2") -let flight3 = Flight(status: .enRoute, departureTime: Date(), destination: "Austin", flightNumber: "3", airline: "Turkish Airlines", terminal: nil) +var specificTime = DateComponents(calendar: Calendar.current, year: 2020, month: 6, day: 18, hour: 7, minute: 0, second: 0, nanosecond: 0) + +var flight1 = Flight(status: .scheduled, departureTime: specificTime.date, destination: "Chicago", flightNumber: "1", airline: "United", terminal: "1") +var flight2 = Flight(status: .canceled, departureTime: nil, destination: "Dulles", flightNumber: "2", airline: "Delta", terminal: "2") +var flight3 = Flight(status: .enRoute, departureTime: Date(), destination: "Austin", flightNumber: "3", airline: "Turkish Airlines", terminal: nil) let depBoard = DepatureBoard(flights: [], "Waseem Airport") depBoard.flights.append(flight1) depBoard.flights.append(flight2) depBoard.flights.append(flight3) +if let departureTime = flight1.departureTime { + print(departureTime) +} + + //: ## 3. Create a free-standing function that can print the flight information from the `DepartureBoard` //: a. Use the function signature: `printDepartures(departureBoard:)` //: @@ -73,7 +80,6 @@ depBoard.flights.append(flight3) - //: ## 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 83ab24595bdd936e1af6407a879047e4d1e91f4a Mon Sep 17 00:00:00 2001 From: Waseem Idelbi Date: Tue, 10 May 2022 21:48:13 -0400 Subject: [PATCH 4/8] completed step 3 and now there's a function that takes in a Departure Board and prints out all the contained flights. Handles all optionals properly. --- AirportDepartures.playground/Contents.swift | 25 ++++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/AirportDepartures.playground/Contents.swift b/AirportDepartures.playground/Contents.swift index 9e87628..9f8bd96 100644 --- a/AirportDepartures.playground/Contents.swift +++ b/AirportDepartures.playground/Contents.swift @@ -43,6 +43,13 @@ class DepatureBoard { } } +var dateFormatter: DateFormatter { + let dateFormatter = DateFormatter() + dateFormatter.dateStyle = .medium + dateFormatter.doesRelativeDateFormatting = true + return dateFormatter +} + //: ## 2. Create 3 flights and add them to a departure board //: a. For the departure time, use `Date()` for the current time //: @@ -64,11 +71,6 @@ depBoard.flights.append(flight1) depBoard.flights.append(flight2) depBoard.flights.append(flight3) -if let departureTime = flight1.departureTime { - print(departureTime) -} - - //: ## 3. Create a free-standing function that can print the flight information from the `DepartureBoard` //: a. Use the function signature: `printDepartures(departureBoard:)` //: @@ -78,8 +80,19 @@ if let departureTime = flight1.departureTime { //: //: d. Print out the current DepartureBoard you created using the function +func printDepartures(departureBoard: DepatureBoard) { + for flight in departureBoard.flights { + var dateString = "" + if let unwrappedDepTime = flight.departureTime { + dateString = dateFormatter.string(from: unwrappedDepTime) + } else { + dateString = "TBD" + } + print("Status: \(flight.status.rawValue) - Departure Date: \(dateString) - Destination: \(flight.destination) - Flight Number: \(flight.flightNumber) - Airline: \(flight.airline) - Terminal: \(flight.terminal ?? "TBD")") + } +} - +printDepartures(departureBoard: depBoard) //: ## 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 8ff4c01a4b95508b9f19e272347e3344089b386a Mon Sep 17 00:00:00 2001 From: Waseem Idelbi Date: Tue, 10 May 2022 21:59:17 -0400 Subject: [PATCH 5/8] I actually already handled optionals and everything as mentioned in step 4 but back in step 3, it just wants an empty string if the departureTime is nil, so I just created another slightly modified copy of the function I made back in step 3. --- AirportDepartures.playground/Contents.swift | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/AirportDepartures.playground/Contents.swift b/AirportDepartures.playground/Contents.swift index 9f8bd96..f90bdc2 100644 --- a/AirportDepartures.playground/Contents.swift +++ b/AirportDepartures.playground/Contents.swift @@ -108,8 +108,17 @@ printDepartures(departureBoard: depBoard) //: 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: DepatureBoard) { + for flight in departureBoard.flights { + var dateString = "" + if let unwrappedDepTime = flight.departureTime { + dateString = dateFormatter.string(from: unwrappedDepTime) + } + print("Status: \(flight.status.rawValue) - Departure Date: \(dateString) - Destination: \(flight.destination) - Flight Number: \(flight.flightNumber) - Airline: \(flight.airline) - Terminal: \(flight.terminal ?? "TBD")") + } +} - +printDepartures2(departureBoard: depBoard) //: ## 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 74235f60bd77881e058a8b43dcf89710ff1c4f00 Mon Sep 17 00:00:00 2001 From: Waseem Idelbi Date: Tue, 10 May 2022 22:15:44 -0400 Subject: [PATCH 6/8] did step 5 with different flight alert announcements depending on the flight's status, added extra instructions for the passengers if the terminal is nil ( stretch goal), and used the same logic from earlier to print "TBD" if the departure time is nil. --- AirportDepartures.playground/Contents.swift | 31 ++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/AirportDepartures.playground/Contents.swift b/AirportDepartures.playground/Contents.swift index f90bdc2..f95b8d3 100644 --- a/AirportDepartures.playground/Contents.swift +++ b/AirportDepartures.playground/Contents.swift @@ -22,6 +22,7 @@ enum FlightStatus: String { case scheduled = "Scheduled" case canceled = "Canceled" case delayed = "Delayed" + case boarding = "Boarding" } struct Flight { @@ -134,7 +135,35 @@ printDepartures2(departureBoard: depBoard) //: //: f. Stretch: Display a custom message if the `terminal` is `nil`, tell the traveler to see the nearest information desk for more details. - +extension DepatureBoard { + func alertPassengers() { + + for flight in flights { + switch flight.status { + + case .enRoute: + print("Your flight is on the way, please remain in your seats and ensure you have your seat belt fastened") + + case .scheduled: + var dateString = "" + if let unwrappedDepTime = flight.departureTime { + dateString = dateFormatter.string(from: unwrappedDepTime) + } else { + dateString = "TBD" + } + print("Your flight to \(flight.destination) is scheduled to depart at \(dateString) from terminal: \(flight.terminal ?? "(Please see the nearest information desk for more details about your terminal)")") + + case .canceled: + print("We're sorry your flight to \(flight.destination) was canceled, here is a $500 voucher") + case .delayed: + print("We're sorry to inform you that flight \(flight.flightNumber) has been delayed, we apolagize for any inconvenience this may have caused and thank you for your patience and understanding.") + case .boarding: + print("Your flight is boarding, please head to terminal: \(flight.terminal ?? "(Please see the nearest information desk for more details about your terminal)") immediately. The doors are closing soon.") + } + } + + } +} //: ## 6. Create a free-standing function to calculate your total airfair for checked bags and destination From be2843461c6cc7574b237f0671a2a8bab5581039 Mon Sep 17 00:00:00 2001 From: Waseem Idelbi Date: Tue, 10 May 2022 22:30:56 -0400 Subject: [PATCH 7/8] Step 6 completed, created function that calculates that cost of a plane ticket and gives the total cost depending on how many people are traveling. Will work on the stretch goal that makes the result returned be formatted like actual USD currency, (example: $750.00) --- AirportDepartures.playground/Contents.swift | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/AirportDepartures.playground/Contents.swift b/AirportDepartures.playground/Contents.swift index f95b8d3..5bdd35e 100644 --- a/AirportDepartures.playground/Contents.swift +++ b/AirportDepartures.playground/Contents.swift @@ -184,5 +184,15 @@ extension DepatureBoard { //: //: 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 { + var grandTotal = 0.0 + + let bagsCost = Double(25 * checkedBags) + let milesCost = 0.10 * Double(distance) + let ticketCost = bagsCost + milesCost + grandTotal = ticketCost * Double(travelers) + print(grandTotal) + return grandTotal +} - +calculateAirfare(checkedBags: 2, distance: 2000, travelers: 3) From ece6a2153da02e2a7ed4bc04037f2a3e42f217d3 Mon Sep 17 00:00:00 2001 From: Waseem Idelbi Date: Tue, 10 May 2022 23:21:39 -0400 Subject: [PATCH 8/8] Completed the stretch goal, it took me way too long to figure out than I can cover a Double to an NSNumber but just casting the double as an NSNumber, so that's how the end of my night's been going... But it's working perfectly now so even tho I feel stupid I feel pretty good :') --- AirportDepartures.playground/Contents.swift | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/AirportDepartures.playground/Contents.swift b/AirportDepartures.playground/Contents.swift index 5bdd35e..c9f2b5c 100644 --- a/AirportDepartures.playground/Contents.swift +++ b/AirportDepartures.playground/Contents.swift @@ -1,5 +1,5 @@ import UIKit - +import Foundation //: ## 1. Create custom types to represent an Airport Departures display //: ![Airport Departures](matthew-smith-5934-unsplash.jpg) @@ -183,6 +183,13 @@ extension DepatureBoard { //: 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 numberFormatter: NumberFormatter { + let numberFormatter = NumberFormatter() + numberFormatter.locale = Locale.current + numberFormatter.usesGroupingSeparator = true + numberFormatter.numberStyle = .currency + return numberFormatter +} func calculateAirfare(checkedBags: Int, distance: Int, travelers: Int) -> Double { var grandTotal = 0.0 @@ -191,7 +198,7 @@ func calculateAirfare(checkedBags: Int, distance: Int, travelers: Int) -> Double let milesCost = 0.10 * Double(distance) let ticketCost = bagsCost + milesCost grandTotal = ticketCost * Double(travelers) - print(grandTotal) + print(numberFormatter.string(from: grandTotal as NSNumber)!) return grandTotal }