From daae6f010ac9c6e183d672b2b5c007367bdbb278 Mon Sep 17 00:00:00 2001 From: hansol Date: Fri, 6 Sep 2024 14:15:01 +0900 Subject: [PATCH 1/2] =?UTF-8?q?person,=20coffeeShop=EC=9D=98=20=ED=83=80?= =?UTF-8?q?=EC=9E=85=20=EB=B0=8F=20=ED=95=A8=EC=88=98=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CodeStarterCamp_Week3/CoffeeShop.swift | 48 ++++++++++++++++++++++++++ CodeStarterCamp_Week3/main.swift | 23 +++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 CodeStarterCamp_Week3/CoffeeShop.swift diff --git a/CodeStarterCamp_Week3/CoffeeShop.swift b/CodeStarterCamp_Week3/CoffeeShop.swift new file mode 100644 index 00000000..1e19b49c --- /dev/null +++ b/CodeStarterCamp_Week3/CoffeeShop.swift @@ -0,0 +1,48 @@ +import Foundation + +enum Coffee: String { + case americano = "아메리카노" + case cafeLatte = "카페라떼" + case cappuccino = "카푸치노" + + var orderMenu: String { + return self.rawValue + } +} + +class Person { + var name: String = "" + var money: Int = 0 + + func buyCoffee(coffeeShop: CoffeeShop, coffee: Coffee) { + if let price = coffeeShop.menu[coffee] { + if money >= price { + money = money - price + coffeeShop.makeCoffee(coffee: coffee) + } + } + } +} + +class CoffeeShop { + var shopName: String = "" + var menu: [Coffee: Int] = [:] + var revenue: Int = 0 + var pickUpTable: [Coffee] = [] + var barista: Person? + + func assignBarista(barista: Person) { + self.barista = barista + print("\(shopName)을 담당하는 바리스타는 \(barista.name)입니다.") + } + + func makeCoffee(coffee: Coffee) { + if let price = menu[coffee] { + revenue += price + pickUpTable.append(coffee) + print("바리스타 \(barista?.name ?? "바리스타")가 \(coffee.orderMenu)를 만들어 픽업 테이블에 올려놨습니다.") + } else { + print("\(coffee.orderMenu)는 메뉴에 없습니다.") + } + } +} diff --git a/CodeStarterCamp_Week3/main.swift b/CodeStarterCamp_Week3/main.swift index feb6d999..71b3c3a3 100644 --- a/CodeStarterCamp_Week3/main.swift +++ b/CodeStarterCamp_Week3/main.swift @@ -8,5 +8,26 @@ import Foundation -print("Hello, World!") +let misterLee = Person() +misterLee.name = "misterLee" +misterLee.money = 3000 +let missKim = Person() +missKim.name = "missKim" +missKim.money = 20000 + +let menu: [Coffee: Int] = [ + .americano: 5000, + .cafeLatte: 6000, + .cappuccino: 7000 +] + +let yagombucks = CoffeeShop() +yagombucks.shopName = "yagombucks" +yagombucks.menu = menu + +yagombucks.assignBarista(barista: misterLee) +missKim.buyCoffee(coffeeShop: yagombucks, coffee: .cafeLatte) +misterLee.buyCoffee(coffeeShop: yagombucks, coffee: .americano) + +print("\(yagombucks.shopName)의 매출은 \(yagombucks.revenue)원입니다.") From 031911dff655f0f94e862d28bfb353b8c0636d54 Mon Sep 17 00:00:00 2001 From: hansol Date: Fri, 6 Sep 2024 14:51:58 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=ED=98=B8?= =?UTF-8?q?=EC=B6=9C=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20=ED=94=84=EB=A6=B0?= =?UTF-8?q?=ED=8A=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CodeStarterCamp_Week3/CoffeeShop.swift | 15 +++++++++++---- CodeStarterCamp_Week3/main.swift | 7 ++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CodeStarterCamp_Week3/CoffeeShop.swift b/CodeStarterCamp_Week3/CoffeeShop.swift index 1e19b49c..bfe6cdd4 100644 --- a/CodeStarterCamp_Week3/CoffeeShop.swift +++ b/CodeStarterCamp_Week3/CoffeeShop.swift @@ -14,12 +14,18 @@ class Person { var name: String = "" var money: Int = 0 - func buyCoffee(coffeeShop: CoffeeShop, coffee: Coffee) { + func order(_ coffee: Coffee, of coffeeShop: CoffeeShop, by name: String) { if let price = coffeeShop.menu[coffee] { if money >= price { - money = money - price - coffeeShop.makeCoffee(coffee: coffee) + money -= price + print("\(name)님이 \(price)원에 \(coffee.orderMenu)를 결제했습니다.") + coffeeShop.make(coffee, from: name) + } else { + print("\(name)님의 잔액이 부족하여 \(coffee.orderMenu)를 구매하실 수 없습니다.") + print("잔액이 \(price - money)원만큼 부족합니다.") } + } else { + print("\(coffee.orderMenu)는 카페에서 판매하지 않습니다.") } } } @@ -36,11 +42,12 @@ class CoffeeShop { print("\(shopName)을 담당하는 바리스타는 \(barista.name)입니다.") } - func makeCoffee(coffee: Coffee) { + func make(_ coffee: Coffee, from name: String) { if let price = menu[coffee] { revenue += price pickUpTable.append(coffee) print("바리스타 \(barista?.name ?? "바리스타")가 \(coffee.orderMenu)를 만들어 픽업 테이블에 올려놨습니다.") + print("\(name)님이 주문하신 \(coffee.orderMenu)(이/가) 준비되었습니다. 픽업대에서 가져가주세요.") } else { print("\(coffee.orderMenu)는 메뉴에 없습니다.") } diff --git a/CodeStarterCamp_Week3/main.swift b/CodeStarterCamp_Week3/main.swift index 71b3c3a3..1ab78e61 100644 --- a/CodeStarterCamp_Week3/main.swift +++ b/CodeStarterCamp_Week3/main.swift @@ -27,7 +27,8 @@ yagombucks.shopName = "yagombucks" yagombucks.menu = menu yagombucks.assignBarista(barista: misterLee) -missKim.buyCoffee(coffeeShop: yagombucks, coffee: .cafeLatte) -misterLee.buyCoffee(coffeeShop: yagombucks, coffee: .americano) +missKim.order(.cafeLatte, of: yagombucks, by: missKim.name) +misterLee.order(.americano, of: yagombucks, by: misterLee.name) -print("\(yagombucks.shopName)의 매출은 \(yagombucks.revenue)원입니다.") +print("\(yagombucks.shopName)의 매출액: \(yagombucks.revenue)원") +print("픽업 테이블에 있는 커피: \(yagombucks.pickUpTable.map { $0.orderMenu }.joined(separator: ", "))")