-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathContents.swift
53 lines (38 loc) · 1.64 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// Facade.swift
// ios-design-patterns
//
// Created by Astemir Eleev on 27/08/2018.
// Copyright © 2018 Astemir Eleev. All rights reserved.
//
import Foundation
public extension String {
/// Creates a Date instance from Self String based in the specified format
///
/// - Parameter format: is a String format that is used in parsing
/// - Returns: a Date instance
public func date(inFormat format: String) -> Date? {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = format
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
return dateFormatter.date(from: self)
}
}
//: Usage
// Travel Facade usage
let dateFormat = "yyyy-MM-DD"
guard let fromDate = "2018-10-01".date(inFormat: dateFormat), let toDate = "2018-10-15".date(inFormat: dateFormat) else {
fatalError("Could not format date for format: \(dateFormat)")
}
let travel = TravelFacade(from: fromDate, to: toDate)
// Use case #1
// get the first flight that is less than $500 & get the first hotel that has 4 or 5 stars rating and price per night less than $150
if let flight = travel.flights?.filter({ $0.price < 500 })[0], let hotel = travel.hotels?.filter({ $0.stars.rawValue > 3 && $0.pricePerNight < 150 })[0] {
travel.book(hotel: hotel, flight: flight)
}
// Use case #2
let bookingResult = travel.book(flightPrice: 500, hotelPrice: 150, minHotelStars: .four)
print("**Booking result**: ", bookingResult.status)
print("**Booked Flight**: ", bookingResult.flight ?? (Any).self)
print("**Bookied Hotel**: ", bookingResult.hotel ?? (Any).self)