Last active
April 17, 2018 10:37
-
-
Save rajubd49/3d61ca484df506922860897ef7a6b683 to your computer and use it in GitHub Desktop.
Swift 4 - Object Oriented Programming Playground
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
//: Playground - noun: a place where people can play | |
import UIKit | |
print("Object Oriented Programming") | |
// Protocal | |
protocol PurchaseItem { | |
func cost() -> Float | |
func description() -> String | |
} | |
// Product | |
struct Apple: PurchaseItem { | |
let name: String | |
let price: Float | |
func cost() -> Float { | |
return price | |
} | |
func description() -> String { | |
return "\(name) : \(price)" | |
} | |
} | |
struct Beef: PurchaseItem { | |
let name: String | |
let weight: Float | |
let pricePerWeight: Float | |
func cost() -> Float { | |
return weight * pricePerWeight | |
} | |
func description() -> String { | |
return "\(name) : Weight \(weight), Peice per weight \(pricePerWeight)" | |
} | |
} | |
struct Fish: PurchaseItem { | |
let name: String | |
let price: Float | |
func cost() -> Float { | |
return price | |
} | |
func description() -> String { | |
return "\(name) : \(price)" | |
} | |
} | |
// Customer | |
struct Customer { | |
let name: String | |
let purchaseItems: [PurchaseItem] | |
} | |
// Supershop | |
struct SuperShop { | |
func printReceipt(customer: Customer) { | |
print("Print Receipt for customer \(customer.name)") | |
print("Description : \(customer.purchaseItems.reduce("", {$0 + "\n" + $1.description()}))") | |
print("Total : \(customer.purchaseItems.reduce(0, {$0 + $1.cost()}))") | |
/* Able to remove this code by using hig order "reduce" method by removing forEach codebase | |
var total: Float = 0 | |
customer.purchaseItems.forEach { (item) in | |
total += item.cost() | |
print(item.description()) | |
/* Able to remove this code by defining let purchaseItems: [PurchaseItem] from let purchaseItems: [Any] | |
if let purchaseItem = item as? PurchaseItem { | |
total += purchaseItem.cost() | |
print(purchaseItem.description()) | |
} | |
*/ | |
/* Able to remove this code by implimentin PurchaseItem protocol and cost, description func of that protocol | |
if let apple = item as? Apple { | |
print("\(apple.name) : \(apple.price)") | |
total += apple.price | |
} else if let beef = item as? Beef { | |
print("\(beef.name) : Weight\(beef.weight), Price per Weight \(beef.pricePerWeight)") | |
total += beef.weight * beef.pricePerWeight | |
} else if let fish = item as? Fish { | |
print("\(fish.name) : \(fish.price)") | |
total += fish.price | |
} | |
*/ | |
} | |
print("Total : \(total)") | |
*/ | |
} | |
} | |
let greenApple = Apple(name: "Green Apple", price: 50.0) | |
let redApple = Apple(name: "Red Apple", price: 40) | |
let localBeef = Beef(name: "Local Beef", weight: 2.5, pricePerWeight: 500) | |
let ilishFish = Fish(name: "Padma Ilish", price: 1200) | |
let customer = Customer(name: "Raju", purchaseItems: [greenApple, redApple, localBeef, ilishFish]) | |
let shwapno = SuperShop() | |
shwapno.printReceipt(customer: customer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment