|
| 1 | +import Foundation |
| 2 | +import UIKit |
| 3 | + |
| 4 | +protocol CricketAccessory{ |
| 5 | + func accept(counter : CheckoutCounter) -> Int |
| 6 | +} |
| 7 | + |
| 8 | +class CricketBat : CricketAccessory{ |
| 9 | + private var price : Double |
| 10 | + private var brand : String |
| 11 | + |
| 12 | + init(_ price : Double, _ brand:String) { |
| 13 | + self.price = price |
| 14 | + self.brand = brand |
| 15 | + } |
| 16 | + |
| 17 | + public func getPrice() -> Double{ |
| 18 | + return price |
| 19 | + } |
| 20 | + |
| 21 | + public func getBrand() -> String{ |
| 22 | + return brand |
| 23 | + } |
| 24 | + func accept(counter : CheckoutCounter) -> Int { |
| 25 | + return counter.moveToCounter(bat: self) |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +class CricketBall : CricketAccessory{ |
| 30 | + |
| 31 | + private var type : String |
| 32 | + private var price : Double |
| 33 | + |
| 34 | + init(_ type : String, _ price : Double){ |
| 35 | + self.type = type |
| 36 | + self.price = price |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + public func getType() -> String{ |
| 41 | + return type |
| 42 | + } |
| 43 | + |
| 44 | + public func getPrice() -> Double{ |
| 45 | + return price |
| 46 | + } |
| 47 | + |
| 48 | + func accept(counter : CheckoutCounter) -> Int { |
| 49 | + return counter.moveToCounter(ball: self) |
| 50 | + } |
| 51 | + |
| 52 | +} |
| 53 | + |
| 54 | +protocol CheckoutCounter { |
| 55 | + func moveToCounter(bat : CricketBat) -> Int |
| 56 | + func moveToCounter(ball : CricketBall) -> Int |
| 57 | +} |
| 58 | + |
| 59 | +class CashCounter :CheckoutCounter{ |
| 60 | + func moveToCounter(bat: CricketBat) -> Int { |
| 61 | + var cost : Int = 0 |
| 62 | + if bat.getBrand() == "Brittania"{ |
| 63 | + cost = Int(0.8 * bat.getPrice()) |
| 64 | + } else{ |
| 65 | + cost = Int(bat.getPrice()) |
| 66 | + } |
| 67 | + print("Bat brand : \(bat.getBrand()) and price is : \(cost) ") |
| 68 | + return cost |
| 69 | + } |
| 70 | + |
| 71 | + func moveToCounter(ball: CricketBall) -> Int { |
| 72 | + |
| 73 | + print("Ball Type : \(ball.getType()) and price is : \(ball.getPrice()) ") |
| 74 | + return Int(ball.getPrice()) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +func main(){ |
| 80 | + print("Main") |
| 81 | + func finalPriceCalculation(accessories : [CricketAccessory]) -> Int{ |
| 82 | + var checkout = CashCounter() |
| 83 | + var cost = 0 |
| 84 | + for item in accessories{ |
| 85 | + cost += item.accept(counter: checkout) |
| 86 | + } |
| 87 | + print("Toal cart value : \(cost)") |
| 88 | + return cost |
| 89 | + } |
| 90 | + |
| 91 | + var cartItems = [CricketAccessory]() |
| 92 | + let mrfBat = CricketBat(2000, "MRF") |
| 93 | + let brittaniaBat = CricketBat(1500, "Brittania") |
| 94 | + let tennisBall = CricketBall("Tennis", 120) |
| 95 | + let leatherBall = CricketBall("Leather", 200) |
| 96 | + cartItems.append(mrfBat) |
| 97 | + cartItems.append(brittaniaBat) |
| 98 | + cartItems.append(tennisBall) |
| 99 | + cartItems.append(leatherBall) |
| 100 | + |
| 101 | + var cost = finalPriceCalculation(accessories: cartItems) |
| 102 | + print("Checked Out with Bill Amount : \(cost)") |
| 103 | +} |
| 104 | + |
| 105 | +main() |
| 106 | + |
0 commit comments