Last active
May 3, 2019 18:01
-
-
Save SURYAKANTSHARMA/10c5157a2800d9ba078871dfc5771e5b to your computer and use it in GitHub Desktop.
DIProductExample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typealias Price = Double | |
struct Product { | |
let name: String | |
let price: Price | |
} | |
struct Coupon { | |
let code: String | |
let percentage: Double | |
} | |
struct Cart { | |
var products: [Product] = [] { | |
didSet { | |
totalPrice = products.map{$0.price}.reduce(0.0){ $0 + $1 } | |
} | |
} | |
private(set) var totalPrice: Price = 0.0 | |
mutating func apply(_ coupon: Coupon) { | |
totalPrice -= totalPrice * (coupon.percentage / 100) | |
} | |
mutating func add(_ product: Product) { | |
products.append(product) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment