Skip to content

Instantly share code, notes, and snippets.

@SURYAKANTSHARMA
Last active May 3, 2019 18:01
Show Gist options
  • Save SURYAKANTSHARMA/10c5157a2800d9ba078871dfc5771e5b to your computer and use it in GitHub Desktop.
Save SURYAKANTSHARMA/10c5157a2800d9ba078871dfc5771e5b to your computer and use it in GitHub Desktop.
DIProductExample
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