Skip to content

Instantly share code, notes, and snippets.

@takaheraw
Created October 17, 2015 06:51
Show Gist options
  • Save takaheraw/bcce4f9c629ef0518915 to your computer and use it in GitHub Desktop.
Save takaheraw/bcce4f9c629ef0518915 to your computer and use it in GitHub Desktop.
class Product {
var name:String
var price:Double
private var stockBackingValue:Int = 0
var stock:Int {
get {
return stockBackingValue
}
set {
stockBackingValue = max(0, newValue)
}
}
init(name:String, price:Double, stock:Int) {
self.name = name
self.price = price
self.stock = stock
}
func calculateTax(rate: Double) -> Double {
return min(10, self.price * rate)
}
var stockValue: Double {
get {
return self.price * Double(self.stock)
}
}
}
let product = Product(name: "hoge", price: 1000.0, stock: 200)
product.stockValue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment