From 905c3f0c0d3743e1d857b25f236ce7166ecfbb55 Mon Sep 17 00:00:00 2001 From: YongwooKim Date: Tue, 8 Jun 2021 14:16:03 +0900 Subject: [PATCH 01/26] =?UTF-8?q?docs=20:=20Readme.md=20=ED=8C=8C=EC=9D=BC?= =?UTF-8?q?=20=EC=83=9D=EC=84=B1=20=EB=B0=8F=20step1=EB=B8=8C=EB=9F=B0?= =?UTF-8?q?=EC=B9=98=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Readme.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 Readme.md diff --git a/Readme.md b/Readme.md new file mode 100644 index 000000000..d31a20c7c --- /dev/null +++ b/Readme.md @@ -0,0 +1 @@ +#쥬스 메이커 From 246a02a7984e042e78b104c52b50e1a87f4f2386 Mon Sep 17 00:00:00 2001 From: taeyoung-Kim-KR Date: Tue, 8 Jun 2021 17:19:55 +0900 Subject: [PATCH 02/26] =?UTF-8?q?feat=20:=20enum=EC=A5=AC=EC=8A=A4?= =?UTF-8?q?=ED=83=80=EC=9E=85=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JuiceMaker/JuiceMaker/Controller/ViewController.swift | 10 ++++++++++ JuiceMaker/JuiceMaker/Model/FruitStore.swift | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/JuiceMaker/JuiceMaker/Controller/ViewController.swift b/JuiceMaker/JuiceMaker/Controller/ViewController.swift index 19f26dc83..29565e43f 100644 --- a/JuiceMaker/JuiceMaker/Controller/ViewController.swift +++ b/JuiceMaker/JuiceMaker/Controller/ViewController.swift @@ -7,6 +7,16 @@ import UIKit class ViewController: UIViewController { + + enum JuiceType: Int { + case strawberryJuice = 1 + case bananaJuice + case pineappleJuice + case kiwiJuice + case mangoJuice + case strawberryBananaJuice + case mangoKiwiJuice + } override func viewDidLoad() { super.viewDidLoad() diff --git a/JuiceMaker/JuiceMaker/Model/FruitStore.swift b/JuiceMaker/JuiceMaker/Model/FruitStore.swift index a1e925afc..c8fb34508 100644 --- a/JuiceMaker/JuiceMaker/Model/FruitStore.swift +++ b/JuiceMaker/JuiceMaker/Model/FruitStore.swift @@ -8,5 +8,11 @@ import Foundation // 과일 타입 class FruitStore { + var strawberry: Int = 10 + var banana: Int = 10 + var pineapple: Int = 10 + var kiwi: Int = 10 + var mango: Int = 10 + } From e68220f78c96e0133fab8591ee66a0ffd57fa81c Mon Sep 17 00:00:00 2001 From: taeyoung-Kim-KR Date: Wed, 9 Jun 2021 11:08:23 +0900 Subject: [PATCH 03/26] =?UTF-8?q?feat=20:=20=EC=A5=AC=EC=8A=A4=EB=A9=94?= =?UTF-8?q?=EC=9D=B4=EC=BB=A4=20=ED=83=80=EC=9E=85=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fruit,Juice를 열거형으로 생성, 쥬스 종류와 소모될 재료의 양을 가질 변수 생성 --- .../Controller/ViewController.swift | 10 ---- JuiceMaker/JuiceMaker/Model/JuiceMaker.swift | 58 ++++++++++++++++++- 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/JuiceMaker/JuiceMaker/Controller/ViewController.swift b/JuiceMaker/JuiceMaker/Controller/ViewController.swift index 29565e43f..19f26dc83 100644 --- a/JuiceMaker/JuiceMaker/Controller/ViewController.swift +++ b/JuiceMaker/JuiceMaker/Controller/ViewController.swift @@ -7,16 +7,6 @@ import UIKit class ViewController: UIViewController { - - enum JuiceType: Int { - case strawberryJuice = 1 - case bananaJuice - case pineappleJuice - case kiwiJuice - case mangoJuice - case strawberryBananaJuice - case mangoKiwiJuice - } override func viewDidLoad() { super.viewDidLoad() diff --git a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift index 0c8629e1d..0a8bbc3fb 100644 --- a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift +++ b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift @@ -6,7 +6,61 @@ import Foundation -// 쥬스 메이커 타입 +// 쥬스 메이커 타입 +enum Fruit { + case strawberry + case banana + case pineapple + case kiwi + case mango +} + struct JuiceMaker { - + enum Juice: Int, CustomStringConvertible { + case strawberryBananaJuice = 1 + case mangoKiwiJuice + case strawberryJuice + case bananaJuice + case mangoJuice + case kiwiJuice + case pineappleJuice + + var description: String { + switch self { + case .strawberryJuice: + return "딸기" + case .bananaJuice: + return "바나나" + case .pineappleJuice: + return "파인애플" + case .kiwiJuice: + return "키위" + case .mangoJuice: + return "망고" + case .strawberryBananaJuice: + return "딸바" + case .mangoKiwiJuice: + return "망키" + } + } + + var ingredients: [(Fruit, Int)] { + switch self { + case .strawberryJuice: + return [(.strawberry, 16)] + case .bananaJuice: + return [(.banana, 2)] + case .pineappleJuice: + return [(.pineapple, 2)] + case .kiwiJuice: + return [(.kiwi, 3)] + case .mangoJuice: + return [(.mango, 3)] + case .strawberryBananaJuice: + return [(.strawberry, 10), (.banana, 1)] + case .mangoKiwiJuice: + return [(.mango, 2), (.kiwi, 1)] + } + } + } } From c5e76a3d880e7c42ae0c3709e0d4bc980f0acdec Mon Sep 17 00:00:00 2001 From: YongwooKim Date: Wed, 9 Jun 2021 11:24:08 +0900 Subject: [PATCH 04/26] =?UTF-8?q?feat=20:=20FruitStore=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20currentStock=EB=A9=94=EC=86=8C=EB=93=9C=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit FruitStore의 각 과일(저장프로퍼티)의 현재 재고를 알려주는 currentStock메소드 생성 --- JuiceMaker/JuiceMaker/Model/FruitStore.swift | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/JuiceMaker/JuiceMaker/Model/FruitStore.swift b/JuiceMaker/JuiceMaker/Model/FruitStore.swift index c8fb34508..e9a0206cd 100644 --- a/JuiceMaker/JuiceMaker/Model/FruitStore.swift +++ b/JuiceMaker/JuiceMaker/Model/FruitStore.swift @@ -14,5 +14,18 @@ class FruitStore { var kiwi: Int = 10 var mango: Int = 10 - + func currentStock(_ fruit: Fruit) -> Int { + switch fruit { + case .strawberry: + return strawberry + case .banana: + return banana + case .pineapple: + return pineapple + case .kiwi: + return kiwi + case .mango: + return mango + } + } } From a6d8205762d58838a5f66281d4700415bef07335 Mon Sep 17 00:00:00 2001 From: taeyoung-Kim-KR Date: Wed, 9 Jun 2021 11:26:25 +0900 Subject: [PATCH 05/26] =?UTF-8?q?feat=20:=20FruitStore=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20=EC=95=88=EC=97=90=20changeStock=20?= =?UTF-8?q?=EB=A9=94=EC=86=8C=EB=93=9C=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 과일마다의 갯수를 수정하는 메소드 생성 --- JuiceMaker/JuiceMaker/Model/FruitStore.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/JuiceMaker/JuiceMaker/Model/FruitStore.swift b/JuiceMaker/JuiceMaker/Model/FruitStore.swift index e9a0206cd..5bfc4376a 100644 --- a/JuiceMaker/JuiceMaker/Model/FruitStore.swift +++ b/JuiceMaker/JuiceMaker/Model/FruitStore.swift @@ -28,4 +28,19 @@ class FruitStore { return mango } } + + func changeStock(_ fruit: Fruit, _ changingQuantity: Int) { + switch fruit { + case .strawberry: + strawberry -= changingQuantity + case .banana: + banana -= changingQuantity + case .pineapple: + pineapple -= changingQuantity + case .kiwi: + kiwi -= changingQuantity + case .mango: + mango -= changingQuantity + } + } } From 2eb3ab8039154f0314e0ba97aadb8eb6fd246e52 Mon Sep 17 00:00:00 2001 From: YongwooKim Date: Wed, 9 Jun 2021 11:31:53 +0900 Subject: [PATCH 06/26] =?UTF-8?q?feat=20:=20JuiceMaker=20=EA=B5=AC?= =?UTF-8?q?=EC=A1=B0=EC=B2=B4=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit JuiceMaker 구조체 내부에 FruitStore() 인스턴스를 하나 생성 Fruit 와 Juice enum을 이용해서 쥬스를 만드는 makeJuice() 메소드와 재고 확인을 하는 checkStock() 메소드를 구현 --- JuiceMaker/JuiceMaker/Model/FruitStore.swift | 48 ++++++++++---------- JuiceMaker/JuiceMaker/Model/JuiceMaker.swift | 22 +++++++++ 2 files changed, 46 insertions(+), 24 deletions(-) diff --git a/JuiceMaker/JuiceMaker/Model/FruitStore.swift b/JuiceMaker/JuiceMaker/Model/FruitStore.swift index 5bfc4376a..ffec75f3f 100644 --- a/JuiceMaker/JuiceMaker/Model/FruitStore.swift +++ b/JuiceMaker/JuiceMaker/Model/FruitStore.swift @@ -15,32 +15,32 @@ class FruitStore { var mango: Int = 10 func currentStock(_ fruit: Fruit) -> Int { - switch fruit { - case .strawberry: - return strawberry - case .banana: - return banana - case .pineapple: - return pineapple - case .kiwi: - return kiwi - case .mango: - return mango - } + switch fruit { + case .strawberry: + return strawberry + case .banana: + return banana + case .pineapple: + return pineapple + case .kiwi: + return kiwi + case .mango: + return mango } + } func changeStock(_ fruit: Fruit, _ changingQuantity: Int) { - switch fruit { - case .strawberry: - strawberry -= changingQuantity - case .banana: - banana -= changingQuantity - case .pineapple: - pineapple -= changingQuantity - case .kiwi: - kiwi -= changingQuantity - case .mango: - mango -= changingQuantity - } + switch fruit { + case .strawberry: + strawberry -= changingQuantity + case .banana: + banana -= changingQuantity + case .pineapple: + pineapple -= changingQuantity + case .kiwi: + kiwi -= changingQuantity + case .mango: + mango -= changingQuantity } + } } diff --git a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift index 0a8bbc3fb..37d56f6ea 100644 --- a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift +++ b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift @@ -63,4 +63,26 @@ struct JuiceMaker { } } } + + let store = FruitStore() + + func makeJuice(_ type: Juice) -> Juice? { + if checkStock(type.ingredients) { + for ingredient in type.ingredients as [(fruit: Fruit, removingQuantity:Int)] { + store.changeStock(ingredient.fruit, ingredient.removingQuantity) + } + return type + } else { + return nil + } + } + + func checkStock(_ ingredients: [(fruit: Fruit, removingQuantity:Int)]) -> Bool { + for ingredient in ingredients { + if store.currentStock(ingredient.fruit) < ingredient.removingQuantity { + return false + } + } + return true + } } From 12158b412ab533c8b9b1f8d2d9319f1a2eeb97ed Mon Sep 17 00:00:00 2001 From: YongwooKim Date: Thu, 10 Jun 2021 23:06:53 +0900 Subject: [PATCH 07/26] =?UTF-8?q?fix=20:=20Step=201=20PR=20=EB=A6=AC?= =?UTF-8?q?=EB=B7=B0=20=EA=B4=80=EB=A0=A8=20=EB=B3=80=EA=B2=BD=EC=82=AC?= =?UTF-8?q?=ED=95=AD=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JuiceMaker/JuiceMaker/Model/JuiceMaker.swift | 42 ++++++++++---------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift index 37d56f6ea..3529d5df9 100644 --- a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift +++ b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift @@ -44,45 +44,47 @@ struct JuiceMaker { } } - var ingredients: [(Fruit, Int)] { + var ingredients: Dictionary { switch self { case .strawberryJuice: - return [(.strawberry, 16)] + return [.strawberry:16] case .bananaJuice: - return [(.banana, 2)] + return [.banana:2] case .pineappleJuice: - return [(.pineapple, 2)] + return [.pineapple:2] case .kiwiJuice: - return [(.kiwi, 3)] + return [.kiwi:3] case .mangoJuice: - return [(.mango, 3)] + return [.mango:3] case .strawberryBananaJuice: - return [(.strawberry, 10), (.banana, 1)] + return [.strawberry:10, .banana:1] case .mangoKiwiJuice: - return [(.mango, 2), (.kiwi, 1)] + return [.mango:2, .kiwi:1] } } } let store = FruitStore() - func makeJuice(_ type: Juice) -> Juice? { - if checkStock(type.ingredients) { - for ingredient in type.ingredients as [(fruit: Fruit, removingQuantity:Int)] { - store.changeStock(ingredient.fruit, ingredient.removingQuantity) + func makeJuice(_ juice: Juice) -> Juice? { + if checkStock(juice.ingredients) { + for ingredient in juice.ingredients { + store.changeStock(ingredient.key, ingredient.value) } - return type + return juice } else { return nil } } - func checkStock(_ ingredients: [(fruit: Fruit, removingQuantity:Int)]) -> Bool { - for ingredient in ingredients { - if store.currentStock(ingredient.fruit) < ingredient.removingQuantity { - return false - } - } - return true + func checkStock(_ ingredients: Dictionary) -> Bool { +// for ingredient in ingredients { +// if store.currentStock(ingredient.key) < ingredient.value { +// return false +// } +// } +// return true + + return ingredients.filter({ store.currentStock($0.key) < $0.value }).isEmpty // filter 후 배열 인자 존재 == 재고보다 소모될 양이 많았던 경우가 존재 -> false } } From 56adcec4eeca3bcd5bbf5443ce953011ceae0fe9 Mon Sep 17 00:00:00 2001 From: taeyoung-Kim-KR Date: Mon, 14 Jun 2021 17:47:09 +0900 Subject: [PATCH 08/26] =?UTF-8?q?refactor=20:=20step=5F1=20FruitStore=20?= =?UTF-8?q?=ED=94=84=EB=A1=9C=ED=8D=BC=ED=8B=B0=20=ED=83=80=EC=9E=85=20?= =?UTF-8?q?=EB=B0=8F=20=ED=95=A8=EC=88=98=20=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit FruitStore의 기존 프로퍼티는 switch구문이 길어져서 딕셔너리로 변경하였습니다. 그에따라 함수도 변경하였습니다.: --- JuiceMaker/JuiceMaker/Model/FruitStore.swift | 42 +++++++------------- 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/JuiceMaker/JuiceMaker/Model/FruitStore.swift b/JuiceMaker/JuiceMaker/Model/FruitStore.swift index ffec75f3f..e3eec7929 100644 --- a/JuiceMaker/JuiceMaker/Model/FruitStore.swift +++ b/JuiceMaker/JuiceMaker/Model/FruitStore.swift @@ -8,39 +8,27 @@ import Foundation // 과일 타입 class FruitStore { - var strawberry: Int = 10 - var banana: Int = 10 - var pineapple: Int = 10 - var kiwi: Int = 10 - var mango: Int = 10 + var fruitStocks: Dictionary + + init() { + fruitStocks = [.strawberry:10, .banana:10, .pineapple:10, .kiwi:10, .mango:10] + } func currentStock(_ fruit: Fruit) -> Int { - switch fruit { - case .strawberry: - return strawberry - case .banana: - return banana - case .pineapple: - return pineapple - case .kiwi: - return kiwi - case .mango: - return mango + guard let stock = fruitStocks[fruit] else { + print("재고에 존재하지 않는 과일") + return } + + return stock } func changeStock(_ fruit: Fruit, _ changingQuantity: Int) { - switch fruit { - case .strawberry: - strawberry -= changingQuantity - case .banana: - banana -= changingQuantity - case .pineapple: - pineapple -= changingQuantity - case .kiwi: - kiwi -= changingQuantity - case .mango: - mango -= changingQuantity + guard let stock = fruitStocks[fruit] else { + print("재고에 존재하지 않는 과일") + return } + + fruitStocks.updateValue(stock - changingQuantity, forKey: fruit) } } From 2a1a6b06ca72a381ef853607c4be0bc44b749f49 Mon Sep 17 00:00:00 2001 From: YongwooKim Date: Mon, 14 Jun 2021 17:58:59 +0900 Subject: [PATCH 09/26] =?UTF-8?q?refactor=20:=20JuiceMaker.checkStock=20?= =?UTF-8?q?=EC=9E=AC=EB=A3=8C=20=EC=B2=98=EB=A6=AC=20=EB=B0=A9=EC=8B=9D=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기존에 튜플 또는 딕셔너리 처리하는 과정에서 호출명에 대한 고민을 for-loop의 인자명으로 해결해봤습니다. --- JuiceMaker/JuiceMaker/Model/JuiceMaker.swift | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift index 3529d5df9..d159dc607 100644 --- a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift +++ b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift @@ -78,13 +78,12 @@ struct JuiceMaker { } func checkStock(_ ingredients: Dictionary) -> Bool { -// for ingredient in ingredients { -// if store.currentStock(ingredient.key) < ingredient.value { -// return false -// } -// } -// return true + for (fruit, removingQuantity) in ingredients { + if store.currentStock(fruit) < removingQuantity { + return false + } + } - return ingredients.filter({ store.currentStock($0.key) < $0.value }).isEmpty // filter 후 배열 인자 존재 == 재고보다 소모될 양이 많았던 경우가 존재 -> false + return true } } From dbb467944d0eb3910c207fbf38d50170a4d7077a Mon Sep 17 00:00:00 2001 From: taeyoung-Kim-KR Date: Mon, 14 Jun 2021 18:06:57 +0900 Subject: [PATCH 10/26] =?UTF-8?q?feat=20:=20=EC=A5=AC=EC=8A=A4=EB=A9=94?= =?UTF-8?q?=EC=9D=B4=EC=BB=A4=EC=97=90=EB=9F=AC=20=ED=83=80=EC=9E=85=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JuiceMaker/JuiceMaker/Model/JuiceMaker.swift | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift index d159dc607..82c22a7af 100644 --- a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift +++ b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift @@ -77,13 +77,15 @@ struct JuiceMaker { } } - func checkStock(_ ingredients: Dictionary) -> Bool { + func checkStock(_ ingredients: Dictionary) throws { for (fruit, removingQuantity) in ingredients { if store.currentStock(fruit) < removingQuantity { - return false + throw JuiceMakerError.outOfStock } } - - return true } } + +enum JuiceMakerError: Error { + case outOfStock +} From ca4ff2ab1bf21463dbf43d9453c18c4e35707698 Mon Sep 17 00:00:00 2001 From: YongwooKim Date: Mon, 14 Jun 2021 18:10:46 +0900 Subject: [PATCH 11/26] =?UTF-8?q?feat=20:=20JuiceMaker.makeJuice=20?= =?UTF-8?q?=ED=95=A8=EC=88=98=20=EC=97=90=EB=9F=AC=EC=B2=98=EB=A6=AC=20?= =?UTF-8?q?=ED=98=95=ED=83=9C=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit makeJuice 함수가 주스만들기 기능만 초점이 잡히고 재고가 부족한 경우는 에러 상황으로 생각하면서 함수를 구현해봤습니다. --- JuiceMaker/JuiceMaker/Model/JuiceMaker.swift | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift index 82c22a7af..ab5e6ab88 100644 --- a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift +++ b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift @@ -63,17 +63,18 @@ struct JuiceMaker { } } } - let store = FruitStore() - func makeJuice(_ juice: Juice) -> Juice? { - if checkStock(juice.ingredients) { - for ingredient in juice.ingredients { - store.changeStock(ingredient.key, ingredient.value) + func makeJuice(_ juice: Juice) { + do { + try checkStock(juice.ingredients) + + for (fruit, removingQuantity) in juice.ingredients { + store.changeStock(fruit, removingQuantity) } - return juice - } else { - return nil + // success notification (juice.description) + } catch { + // fail notification (juice.description) } } @@ -87,5 +88,5 @@ struct JuiceMaker { } enum JuiceMakerError: Error { - case outOfStock + case outOfStock } From 6b7480c6228922a39b8fcc565e4863bf9402e8cc Mon Sep 17 00:00:00 2001 From: taeyoung-Kim-KR Date: Mon, 14 Jun 2021 18:20:58 +0900 Subject: [PATCH 12/26] =?UTF-8?q?feat=20:=20currentStock=20=EC=97=90?= =?UTF-8?q?=EB=9F=AC=20=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 반환값을 옵셔널로 주거나 throw로 에러처리를 주는 방식 보다 재고 부족과 같은 상황으로 처리했습니다. --- JuiceMaker/JuiceMaker/Model/FruitStore.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/JuiceMaker/JuiceMaker/Model/FruitStore.swift b/JuiceMaker/JuiceMaker/Model/FruitStore.swift index e3eec7929..bad82b574 100644 --- a/JuiceMaker/JuiceMaker/Model/FruitStore.swift +++ b/JuiceMaker/JuiceMaker/Model/FruitStore.swift @@ -9,6 +9,7 @@ import Foundation // 과일 타입 class FruitStore { var fruitStocks: Dictionary + let emptyQuantity = 0 init() { fruitStocks = [.strawberry:10, .banana:10, .pineapple:10, .kiwi:10, .mango:10] @@ -17,7 +18,7 @@ class FruitStore { func currentStock(_ fruit: Fruit) -> Int { guard let stock = fruitStocks[fruit] else { print("재고에 존재하지 않는 과일") - return + return emptyQuantity } return stock From 202411ecac9e7cd2d3f5e1339c213fe88cda1cb3 Mon Sep 17 00:00:00 2001 From: YongwooKim Date: Mon, 14 Jun 2021 21:41:29 +0900 Subject: [PATCH 13/26] =?UTF-8?q?feat=20:=20UI=20=EC=98=A4=ED=83=80=20?= =?UTF-8?q?=EB=B0=8F=20=EB=82=B4=EB=B9=84=EA=B2=8C=EC=9D=B4=EC=85=98=20?= =?UTF-8?q?=EC=A0=9C=EB=AA=A9=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 딸바쥬스 -> 딸기쥬스, 딸바쥬스 -> 망키쥬스 등등 동작에 맞는 버튼Title 변경 및 재고추가 내비게이션 제목추가 --- .../View/Base.lproj/Main.storyboard | 24 ++++++++++--------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/JuiceMaker/JuiceMaker/View/Base.lproj/Main.storyboard b/JuiceMaker/JuiceMaker/View/Base.lproj/Main.storyboard index 7abdcc72a..7335caca4 100644 --- a/JuiceMaker/JuiceMaker/View/Base.lproj/Main.storyboard +++ b/JuiceMaker/JuiceMaker/View/Base.lproj/Main.storyboard @@ -59,7 +59,7 @@ - @@ -217,6 +238,13 @@ + + + + + + + @@ -240,10 +268,10 @@ - + - + @@ -383,7 +411,7 @@ - + From 3c43ff97d71a2fa64587fc9535440536fa5285bc Mon Sep 17 00:00:00 2001 From: YongwooKim Date: Mon, 14 Jun 2021 23:20:44 +0900 Subject: [PATCH 15/26] =?UTF-8?q?feat=20:=20=ED=95=98=EB=82=98=EC=9D=98=20?= =?UTF-8?q?IBAction=EC=97=90=EC=84=9C=20UIButton=20=EC=9D=B4=EC=9A=A9?= =?UTF-8?q?=ED=95=B4=EC=84=9C=20Juice=20case=20=EC=96=BB=EA=B8=B0=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 7개의 버튼 IBAction을 하나로 구현하고 동작시 sender를 이용해서 Juice case를 얻는 형태로 구현했습니다. --- .../Controller/MainViewController.swift | 7 +- JuiceMaker/JuiceMaker/Model/JuiceMaker.swift | 91 ++++++++++--------- 2 files changed, 52 insertions(+), 46 deletions(-) diff --git a/JuiceMaker/JuiceMaker/Controller/MainViewController.swift b/JuiceMaker/JuiceMaker/Controller/MainViewController.swift index 799b6f0be..81c0ae27b 100644 --- a/JuiceMaker/JuiceMaker/Controller/MainViewController.swift +++ b/JuiceMaker/JuiceMaker/Controller/MainViewController.swift @@ -13,12 +13,17 @@ class MainViewController: UIViewController { @IBOutlet weak var kiwiLabel: UILabel! @IBOutlet weak var pineappleLabel: UILabel! + let maker = JuiceMaker() + override func viewDidLoad() { super.viewDidLoad() - // Do any additional setup after loading the view. } @IBAction func juiceOrder(_ sender: UIButton) { + guard let titleLabel = sender.titleLabel else { print("버튼 타이틀 에러"); return } + guard let text = titleLabel.text, let juice = Juice(rawValue: text) else { print("쥬스 구분실패"); return } +// print(juice) + maker.makeJuice(juice) } } diff --git a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift index ab5e6ab88..dc50010c4 100644 --- a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift +++ b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift @@ -15,54 +15,55 @@ enum Fruit { case mango } -struct JuiceMaker { - enum Juice: Int, CustomStringConvertible { - case strawberryBananaJuice = 1 - case mangoKiwiJuice - case strawberryJuice - case bananaJuice - case mangoJuice - case kiwiJuice - case pineappleJuice - - var description: String { - switch self { - case .strawberryJuice: - return "딸기" - case .bananaJuice: - return "바나나" - case .pineappleJuice: - return "파인애플" - case .kiwiJuice: - return "키위" - case .mangoJuice: - return "망고" - case .strawberryBananaJuice: - return "딸바" - case .mangoKiwiJuice: - return "망키" - } +enum Juice: String, CustomStringConvertible { + case strawberryBananaJuice = "딸바쥬스 주문" + case mangoKiwiJuice = "망키쥬스 주문" + case strawberryJuice = "딸기쥬스 주문" + case bananaJuice = "바나나쥬스 주문" + case mangoJuice = "망고쥬스 주문" + case kiwiJuice = "키위쥬스 주문" + case pineappleJuice = "파인애플쥬스 주문" + + var description: String { + switch self { + case .strawberryJuice: + return "딸기" + case .bananaJuice: + return "바나나" + case .pineappleJuice: + return "파인애플" + case .kiwiJuice: + return "키위" + case .mangoJuice: + return "망고" + case .strawberryBananaJuice: + return "딸바" + case .mangoKiwiJuice: + return "망키" } - - var ingredients: Dictionary { - switch self { - case .strawberryJuice: - return [.strawberry:16] - case .bananaJuice: - return [.banana:2] - case .pineappleJuice: - return [.pineapple:2] - case .kiwiJuice: - return [.kiwi:3] - case .mangoJuice: - return [.mango:3] - case .strawberryBananaJuice: - return [.strawberry:10, .banana:1] - case .mangoKiwiJuice: - return [.mango:2, .kiwi:1] - } + } + + var ingredients: Dictionary { + switch self { + case .strawberryJuice: + return [.strawberry:16] + case .bananaJuice: + return [.banana:2] + case .pineappleJuice: + return [.pineapple:2] + case .kiwiJuice: + return [.kiwi:3] + case .mangoJuice: + return [.mango:3] + case .strawberryBananaJuice: + return [.strawberry:10, .banana:1] + case .mangoKiwiJuice: + return [.mango:2, .kiwi:1] } } +} + +struct JuiceMaker { let store = FruitStore() func makeJuice(_ juice: Juice) { From a5e679d5d26cdb61be6e835c34b7b8ed5197c255 Mon Sep 17 00:00:00 2001 From: taeyoung-Kim-KR Date: Tue, 15 Jun 2021 00:08:40 +0900 Subject: [PATCH 16/26] =?UTF-8?q?feat=20:=20Alert=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EC=84=B1=EA=B3=B5=EC=8B=9C=20alert=20=EC=83=9D=EC=84=B1,=20?= =?UTF-8?q?=EC=8B=A4=ED=8C=A8=EC=8B=9C=20alert=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controller/MainViewController.swift | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/JuiceMaker/JuiceMaker/Controller/MainViewController.swift b/JuiceMaker/JuiceMaker/Controller/MainViewController.swift index 81c0ae27b..72add9f78 100644 --- a/JuiceMaker/JuiceMaker/Controller/MainViewController.swift +++ b/JuiceMaker/JuiceMaker/Controller/MainViewController.swift @@ -28,3 +28,21 @@ class MainViewController: UIViewController { } +extension MainViewController { + func successAlert() { + let alert = UIAlertController(title: "*** 쥬스 나왔습니다! 맛있게 드세요!", message: nil, preferredStyle: .alert) + let confirmAction = UIAlertAction(title: "감사합니다.", style: .default) + alert.addAction(confirmAction) + present(alert, animated: true, completion: nil) + } + + func failAlert() { + let alert = UIAlertController(title: "재료가 모자라요. 재고를 수정할까요?", message: nil, preferredStyle: .alert) + let confirmAction = UIAlertAction(title: "예", style: .default){ _ in print("예 선택")} + let cancelAction = UIAlertAction(title: "아니오", style: .default){ _ in print("아니오 선택")} + alert.addAction(confirmAction) + alert.addAction(cancelAction) + present(alert, animated: true, completion: nil) + } + +} From 02a17b7044a93aa1aee920f51c942a18153c0d46 Mon Sep 17 00:00:00 2001 From: YongwooKim Date: Tue, 15 Jun 2021 10:51:15 +0900 Subject: [PATCH 17/26] =?UTF-8?q?docs=20:=20=EC=97=B4=EA=B1=B0=EC=B2=B4=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=EB=A7=8C=20=EB=AA=A8=EC=95=84=EB=86=93?= =?UTF-8?q?=EC=9D=80=20=ED=8C=8C=EC=9D=BC=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fruit, Juice, JuiceMakerError 열거형 타입선언 코드를 따로 EnumTypes.swift 파일로 변환해놓음 --- .../JuiceMaker.xcodeproj/project.pbxproj | 4 ++ JuiceMaker/JuiceMaker/Model/EnumTypes.swift | 65 +++++++++++++++++++ JuiceMaker/JuiceMaker/Model/JuiceMaker.swift | 60 ----------------- 3 files changed, 69 insertions(+), 60 deletions(-) create mode 100644 JuiceMaker/JuiceMaker/Model/EnumTypes.swift diff --git a/JuiceMaker/JuiceMaker.xcodeproj/project.pbxproj b/JuiceMaker/JuiceMaker.xcodeproj/project.pbxproj index e90d2afc4..16daee456 100644 --- a/JuiceMaker/JuiceMaker.xcodeproj/project.pbxproj +++ b/JuiceMaker/JuiceMaker.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + A99B246226783EBC00F94FCC /* EnumTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = A99B246126783EBC00F94FCC /* EnumTypes.swift */; }; B463F9C3267796BA0073F37A /* ModifiyViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B463F9C2267796BA0073F37A /* ModifiyViewController.swift */; }; C71CD66B266C7ACB0038B9CB /* FruitStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = C71CD66A266C7ACB0038B9CB /* FruitStore.swift */; }; C73DAF37255D0CDD00020D38 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C73DAF36255D0CDD00020D38 /* AppDelegate.swift */; }; @@ -19,6 +20,7 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + A99B246126783EBC00F94FCC /* EnumTypes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnumTypes.swift; sourceTree = ""; }; B463F9C2267796BA0073F37A /* ModifiyViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModifiyViewController.swift; sourceTree = ""; }; C71CD66A266C7ACB0038B9CB /* FruitStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FruitStore.swift; sourceTree = ""; }; C73DAF33255D0CDD00020D38 /* JuiceMaker.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = JuiceMaker.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -59,6 +61,7 @@ children = ( C73DAF4B255D0D0400020D38 /* JuiceMaker.swift */, C71CD66A266C7ACB0038B9CB /* FruitStore.swift */, + A99B246126783EBC00F94FCC /* EnumTypes.swift */, ); path = Model; sourceTree = ""; @@ -175,6 +178,7 @@ C73DAF37255D0CDD00020D38 /* AppDelegate.swift in Sources */, C73DAF39255D0CDD00020D38 /* SceneDelegate.swift in Sources */, B463F9C3267796BA0073F37A /* ModifiyViewController.swift in Sources */, + A99B246226783EBC00F94FCC /* EnumTypes.swift in Sources */, C73DAF4C255D0D0400020D38 /* JuiceMaker.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/JuiceMaker/JuiceMaker/Model/EnumTypes.swift b/JuiceMaker/JuiceMaker/Model/EnumTypes.swift new file mode 100644 index 000000000..5eac71148 --- /dev/null +++ b/JuiceMaker/JuiceMaker/Model/EnumTypes.swift @@ -0,0 +1,65 @@ +// +// EnumTypes.swift +// JuiceMaker +// +// Created by Yongwoo Marco on 2021/06/15. +// + +enum JuiceMakerError: Error { + case outOfStock +} + +enum Fruit { + case strawberry + case banana + case pineapple + case kiwi + case mango +} + +enum Juice: String, CustomStringConvertible { + case strawberryBananaJuice = "딸바쥬스 주문" + case mangoKiwiJuice = "망키쥬스 주문" + case strawberryJuice = "딸기쥬스 주문" + case bananaJuice = "바나나쥬스 주문" + case mangoJuice = "망고쥬스 주문" + case kiwiJuice = "키위쥬스 주문" + case pineappleJuice = "파인애플쥬스 주문" + + var description: String { + switch self { + case .strawberryJuice: + return "딸기" + case .bananaJuice: + return "바나나" + case .pineappleJuice: + return "파인애플" + case .kiwiJuice: + return "키위" + case .mangoJuice: + return "망고" + case .strawberryBananaJuice: + return "딸바" + case .mangoKiwiJuice: + return "망키" + } + } + var ingredients: Dictionary { + switch self { + case .strawberryJuice: + return [.strawberry:16] + case .bananaJuice: + return [.banana:2] + case .pineappleJuice: + return [.pineapple:2] + case .kiwiJuice: + return [.kiwi:3] + case .mangoJuice: + return [.mango:3] + case .strawberryBananaJuice: + return [.strawberry:10, .banana:1] + case .mangoKiwiJuice: + return [.mango:2, .kiwi:1] + } + } +} diff --git a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift index dc50010c4..3f20bfa36 100644 --- a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift +++ b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift @@ -7,62 +7,6 @@ import Foundation // 쥬스 메이커 타입 -enum Fruit { - case strawberry - case banana - case pineapple - case kiwi - case mango -} - -enum Juice: String, CustomStringConvertible { - case strawberryBananaJuice = "딸바쥬스 주문" - case mangoKiwiJuice = "망키쥬스 주문" - case strawberryJuice = "딸기쥬스 주문" - case bananaJuice = "바나나쥬스 주문" - case mangoJuice = "망고쥬스 주문" - case kiwiJuice = "키위쥬스 주문" - case pineappleJuice = "파인애플쥬스 주문" - - var description: String { - switch self { - case .strawberryJuice: - return "딸기" - case .bananaJuice: - return "바나나" - case .pineappleJuice: - return "파인애플" - case .kiwiJuice: - return "키위" - case .mangoJuice: - return "망고" - case .strawberryBananaJuice: - return "딸바" - case .mangoKiwiJuice: - return "망키" - } - } - - var ingredients: Dictionary { - switch self { - case .strawberryJuice: - return [.strawberry:16] - case .bananaJuice: - return [.banana:2] - case .pineappleJuice: - return [.pineapple:2] - case .kiwiJuice: - return [.kiwi:3] - case .mangoJuice: - return [.mango:3] - case .strawberryBananaJuice: - return [.strawberry:10, .banana:1] - case .mangoKiwiJuice: - return [.mango:2, .kiwi:1] - } - } -} - struct JuiceMaker { let store = FruitStore() @@ -87,7 +31,3 @@ struct JuiceMaker { } } } - -enum JuiceMakerError: Error { - case outOfStock -} From dcf35131ed82325439f2a7a81d949daf6f0634c9 Mon Sep 17 00:00:00 2001 From: taeyoung-Kim-KR Date: Tue, 15 Jun 2021 12:16:48 +0900 Subject: [PATCH 18/26] =?UTF-8?q?feat=20:=20NotificationCenter=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 성공시 쥬스이름을 키값으로 보내는 Alert 생성, 실패시 값 없이 Post Alert 생성,메인 뷰컨트롤러가 NotificationCenter 옵저버 설정 --- .../JuiceMaker/Controller/MainViewController.swift | 14 +++++++++++--- JuiceMaker/JuiceMaker/Model/JuiceMaker.swift | 4 ++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/JuiceMaker/JuiceMaker/Controller/MainViewController.swift b/JuiceMaker/JuiceMaker/Controller/MainViewController.swift index 72add9f78..5587654a2 100644 --- a/JuiceMaker/JuiceMaker/Controller/MainViewController.swift +++ b/JuiceMaker/JuiceMaker/Controller/MainViewController.swift @@ -17,6 +17,9 @@ class MainViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() + + NotificationCenter.default.addObserver(self, selector: #selector(successAlert(_:)), name: Notification.Name(rawValue: "successAlert"), object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(failAlert), name: Notification.Name(rawValue: "failAlert"), object: nil) } @IBAction func juiceOrder(_ sender: UIButton) { @@ -29,14 +32,19 @@ class MainViewController: UIViewController { } extension MainViewController { - func successAlert() { - let alert = UIAlertController(title: "*** 쥬스 나왔습니다! 맛있게 드세요!", message: nil, preferredStyle: .alert) + @objc func successAlert(_ notification: Notification) { + guard let userInfo = notification.userInfo else { print("userInfo에러"); return + } + guard let anyType = userInfo["쥬스이름"], let juiceName = anyType as? String else { print("anyType에러"); return + } + + let alert = UIAlertController(title: "\(juiceName) 쥬스 나왔습니다! 맛있게 드세요!", message: nil, preferredStyle: .alert) let confirmAction = UIAlertAction(title: "감사합니다.", style: .default) alert.addAction(confirmAction) present(alert, animated: true, completion: nil) } - func failAlert() { + @objc func failAlert() { let alert = UIAlertController(title: "재료가 모자라요. 재고를 수정할까요?", message: nil, preferredStyle: .alert) let confirmAction = UIAlertAction(title: "예", style: .default){ _ in print("예 선택")} let cancelAction = UIAlertAction(title: "아니오", style: .default){ _ in print("아니오 선택")} diff --git a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift index 3f20bfa36..ef67d73ee 100644 --- a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift +++ b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift @@ -17,9 +17,9 @@ struct JuiceMaker { for (fruit, removingQuantity) in juice.ingredients { store.changeStock(fruit, removingQuantity) } - // success notification (juice.description) + NotificationCenter.default.post(name: Notification.Name(rawValue: "successAlert"), object: nil, userInfo: ["쥬스이름": juice.description]) } catch { - // fail notification (juice.description) + NotificationCenter.default.post(name: Notification.Name(rawValue: "failAlert"), object: nil) } } From 4aa4ae5a9bff1f14b74487f44a30bf56773826dd Mon Sep 17 00:00:00 2001 From: YongwooKim Date: Tue, 15 Jun 2021 15:10:00 +0900 Subject: [PATCH 19/26] =?UTF-8?q?feat=20:=20=ED=99=94=EB=A9=B4=20Label=20?= =?UTF-8?q?=EC=97=85=EB=8D=B0=EC=9D=B4=ED=8A=B8=20=ED=95=98=EB=8A=94=20upd?= =?UTF-8?q?ateUILabel=20=ED=95=A8=EC=88=98=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit FruitStore에 과일 갯수가 바뀌는 경우 해당 과일의 수량을 화면에 띄워주는 updateUILable 함수를 구현하고 NotificationCenter의 메세지를 받아 동작하게 만들었습니다. --- .../Controller/MainViewController.swift | 33 +++++++++++++++++-- JuiceMaker/JuiceMaker/Model/EnumTypes.swift | 2 +- JuiceMaker/JuiceMaker/Model/FruitStore.swift | 2 +- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/JuiceMaker/JuiceMaker/Controller/MainViewController.swift b/JuiceMaker/JuiceMaker/Controller/MainViewController.swift index 5587654a2..641b82fb4 100644 --- a/JuiceMaker/JuiceMaker/Controller/MainViewController.swift +++ b/JuiceMaker/JuiceMaker/Controller/MainViewController.swift @@ -20,6 +20,11 @@ class MainViewController: UIViewController { NotificationCenter.default.addObserver(self, selector: #selector(successAlert(_:)), name: Notification.Name(rawValue: "successAlert"), object: nil) NotificationCenter.default.addObserver(self, selector: #selector(failAlert), name: Notification.Name(rawValue: "failAlert"), object: nil) + + NotificationCenter.default.addObserver(self, selector: #selector(updateUILabel(_:)), name: Notification.Name(rawValue: "updateUILabel"), object: nil) + for fruit in Fruit.allCases { + NotificationCenter.default.post(name: Notification.Name("updateUILabel"), object: nil, userInfo: ["과일종류":fruit]) + } } @IBAction func juiceOrder(_ sender: UIButton) { @@ -28,14 +33,36 @@ class MainViewController: UIViewController { // print(juice) maker.makeJuice(juice) } - } extension MainViewController { + @objc func updateUILabel(_ notification: Notification) { + guard let userInfo = notification.userInfo else { + print("userInfo 에러"); return + } + guard let userInfoValue = userInfo["과일종류"], let fruit = userInfoValue as? Fruit else { + print("userInfoValue 에러"); return + } + let currentStock = String(maker.store.currentStock(fruit)) + switch fruit { + case .strawberry: + strawberryLabel.text = currentStock + case .banana: + bananaLabel.text = currentStock + case .pineapple: + pineappleLabel.text = currentStock + case .kiwi: + kiwiLabel.text = currentStock + case .mango: + mangoLabel.text = currentStock + } + } @objc func successAlert(_ notification: Notification) { - guard let userInfo = notification.userInfo else { print("userInfo에러"); return + guard let userInfo = notification.userInfo else { + print("userInfo 에러"); return } - guard let anyType = userInfo["쥬스이름"], let juiceName = anyType as? String else { print("anyType에러"); return + guard let userInfoValue = userInfo["쥬스이름"], let juiceName = userInfoValue as? String else { + print("userInfoValue 에러"); return } let alert = UIAlertController(title: "\(juiceName) 쥬스 나왔습니다! 맛있게 드세요!", message: nil, preferredStyle: .alert) diff --git a/JuiceMaker/JuiceMaker/Model/EnumTypes.swift b/JuiceMaker/JuiceMaker/Model/EnumTypes.swift index 5eac71148..850364e36 100644 --- a/JuiceMaker/JuiceMaker/Model/EnumTypes.swift +++ b/JuiceMaker/JuiceMaker/Model/EnumTypes.swift @@ -9,7 +9,7 @@ enum JuiceMakerError: Error { case outOfStock } -enum Fruit { +enum Fruit: CaseIterable { case strawberry case banana case pineapple diff --git a/JuiceMaker/JuiceMaker/Model/FruitStore.swift b/JuiceMaker/JuiceMaker/Model/FruitStore.swift index bad82b574..096faf1c4 100644 --- a/JuiceMaker/JuiceMaker/Model/FruitStore.swift +++ b/JuiceMaker/JuiceMaker/Model/FruitStore.swift @@ -29,7 +29,7 @@ class FruitStore { print("재고에 존재하지 않는 과일") return } - fruitStocks.updateValue(stock - changingQuantity, forKey: fruit) + NotificationCenter.default.post(name: Notification.Name("updateUILabel"), object: nil, userInfo: ["과일종류":fruit]) } } From f37d256f8566d5dd9e4d71e1d9284ad4a774c090 Mon Sep 17 00:00:00 2001 From: YongwooKim Date: Tue, 15 Jun 2021 15:27:25 +0900 Subject: [PATCH 20/26] =?UTF-8?q?refactor=20:=20=EB=B3=80=EC=88=98=20?= =?UTF-8?q?=EB=B0=8F=20=ED=95=A8=EC=88=98=EB=AA=85=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit successAlert -> alertMakingJuiceSuccess failAlert -> alertMakingJuiceFail maker -> juiceMaker store -> fruitStore 등 함수(동사형태), 변수명 명확하게 수정해봤습니다. --- .../Controller/MainViewController.swift | 20 +++++++++---------- JuiceMaker/JuiceMaker/Model/JuiceMaker.swift | 10 +++++----- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/JuiceMaker/JuiceMaker/Controller/MainViewController.swift b/JuiceMaker/JuiceMaker/Controller/MainViewController.swift index 641b82fb4..e1c8a1350 100644 --- a/JuiceMaker/JuiceMaker/Controller/MainViewController.swift +++ b/JuiceMaker/JuiceMaker/Controller/MainViewController.swift @@ -13,13 +13,13 @@ class MainViewController: UIViewController { @IBOutlet weak var kiwiLabel: UILabel! @IBOutlet weak var pineappleLabel: UILabel! - let maker = JuiceMaker() + let juiceMaker = JuiceMaker() override func viewDidLoad() { super.viewDidLoad() - NotificationCenter.default.addObserver(self, selector: #selector(successAlert(_:)), name: Notification.Name(rawValue: "successAlert"), object: nil) - NotificationCenter.default.addObserver(self, selector: #selector(failAlert), name: Notification.Name(rawValue: "failAlert"), object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(alertMakingJuiceSuccess(_:)), name: Notification.Name(rawValue: "makeJuiceSuccess"), object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(alertMakingJuiceFail), name: Notification.Name(rawValue: "makeJuiceFail"), object: nil) NotificationCenter.default.addObserver(self, selector: #selector(updateUILabel(_:)), name: Notification.Name(rawValue: "updateUILabel"), object: nil) for fruit in Fruit.allCases { @@ -31,7 +31,7 @@ class MainViewController: UIViewController { guard let titleLabel = sender.titleLabel else { print("버튼 타이틀 에러"); return } guard let text = titleLabel.text, let juice = Juice(rawValue: text) else { print("쥬스 구분실패"); return } // print(juice) - maker.makeJuice(juice) + juiceMaker.makeJuice(juice) } } @@ -43,7 +43,7 @@ extension MainViewController { guard let userInfoValue = userInfo["과일종류"], let fruit = userInfoValue as? Fruit else { print("userInfoValue 에러"); return } - let currentStock = String(maker.store.currentStock(fruit)) + let currentStock = String(juiceMaker.fruitStore.currentStock(fruit)) switch fruit { case .strawberry: strawberryLabel.text = currentStock @@ -57,7 +57,7 @@ extension MainViewController { mangoLabel.text = currentStock } } - @objc func successAlert(_ notification: Notification) { + @objc func alertMakingJuiceSuccess(_ notification: Notification) { guard let userInfo = notification.userInfo else { print("userInfo 에러"); return } @@ -66,15 +66,15 @@ extension MainViewController { } let alert = UIAlertController(title: "\(juiceName) 쥬스 나왔습니다! 맛있게 드세요!", message: nil, preferredStyle: .alert) - let confirmAction = UIAlertAction(title: "감사합니다.", style: .default) + let confirmAction = UIAlertAction(title: "감사합니다.", style: .default) { _ in print("주문성공")} alert.addAction(confirmAction) present(alert, animated: true, completion: nil) } - @objc func failAlert() { + @objc func alertMakingJuiceFail() { let alert = UIAlertController(title: "재료가 모자라요. 재고를 수정할까요?", message: nil, preferredStyle: .alert) - let confirmAction = UIAlertAction(title: "예", style: .default){ _ in print("예 선택")} - let cancelAction = UIAlertAction(title: "아니오", style: .default){ _ in print("아니오 선택")} + let confirmAction = UIAlertAction(title: "예", style: .default){ _ in print("주문실패 - 예 선택")} + let cancelAction = UIAlertAction(title: "아니오", style: .default){ _ in print("주문실패 - 아니오 선택")} alert.addAction(confirmAction) alert.addAction(cancelAction) present(alert, animated: true, completion: nil) diff --git a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift index ef67d73ee..8aae57c3f 100644 --- a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift +++ b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift @@ -8,24 +8,24 @@ import Foundation // 쥬스 메이커 타입 struct JuiceMaker { - let store = FruitStore() + let fruitStore = FruitStore() func makeJuice(_ juice: Juice) { do { try checkStock(juice.ingredients) for (fruit, removingQuantity) in juice.ingredients { - store.changeStock(fruit, removingQuantity) + fruitStore.changeStock(fruit, removingQuantity) } - NotificationCenter.default.post(name: Notification.Name(rawValue: "successAlert"), object: nil, userInfo: ["쥬스이름": juice.description]) + NotificationCenter.default.post(name: Notification.Name(rawValue: "makeJuiceSuccess"), object: nil, userInfo: ["쥬스이름": juice.description]) } catch { - NotificationCenter.default.post(name: Notification.Name(rawValue: "failAlert"), object: nil) + NotificationCenter.default.post(name: Notification.Name(rawValue: "makeJuiceFail"), object: nil) } } func checkStock(_ ingredients: Dictionary) throws { for (fruit, removingQuantity) in ingredients { - if store.currentStock(fruit) < removingQuantity { + if fruitStore.currentStock(fruit) < removingQuantity { throw JuiceMakerError.outOfStock } } From b978ab9798a11d03ac3f201302590f4390555ee3 Mon Sep 17 00:00:00 2001 From: YongwooKim Date: Tue, 15 Jun 2021 15:31:00 +0900 Subject: [PATCH 21/26] =?UTF-8?q?refactor=20:=20=EB=B6=88=ED=95=84?= =?UTF-8?q?=EC=9A=94=ED=95=9C=20=EC=A4=84=EB=B0=94=EA=BF=88=20=EB=B0=8F=20?= =?UTF-8?q?=EA=B3=B5=EB=B0=B1=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../JuiceMaker/Controller/MainViewController.swift | 14 ++++++-------- .../Controller/ModifiyViewController.swift | 1 - JuiceMaker/JuiceMaker/Model/FruitStore.swift | 2 -- JuiceMaker/JuiceMaker/Model/JuiceMaker.swift | 1 - 4 files changed, 6 insertions(+), 12 deletions(-) diff --git a/JuiceMaker/JuiceMaker/Controller/MainViewController.swift b/JuiceMaker/JuiceMaker/Controller/MainViewController.swift index e1c8a1350..f1e525e47 100644 --- a/JuiceMaker/JuiceMaker/Controller/MainViewController.swift +++ b/JuiceMaker/JuiceMaker/Controller/MainViewController.swift @@ -18,10 +18,12 @@ class MainViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - NotificationCenter.default.addObserver(self, selector: #selector(alertMakingJuiceSuccess(_:)), name: Notification.Name(rawValue: "makeJuiceSuccess"), object: nil) - NotificationCenter.default.addObserver(self, selector: #selector(alertMakingJuiceFail), name: Notification.Name(rawValue: "makeJuiceFail"), object: nil) - - NotificationCenter.default.addObserver(self, selector: #selector(updateUILabel(_:)), name: Notification.Name(rawValue: "updateUILabel"), object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(alertMakingJuiceSuccess(_:)), + name: Notification.Name(rawValue: "makeJuiceSuccess"), object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(alertMakingJuiceFail), + name: Notification.Name(rawValue: "makeJuiceFail"), object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(updateUILabel(_:)), + name: Notification.Name(rawValue: "updateUILabel"), object: nil) for fruit in Fruit.allCases { NotificationCenter.default.post(name: Notification.Name("updateUILabel"), object: nil, userInfo: ["과일종류":fruit]) } @@ -30,7 +32,6 @@ class MainViewController: UIViewController { @IBAction func juiceOrder(_ sender: UIButton) { guard let titleLabel = sender.titleLabel else { print("버튼 타이틀 에러"); return } guard let text = titleLabel.text, let juice = Juice(rawValue: text) else { print("쥬스 구분실패"); return } -// print(juice) juiceMaker.makeJuice(juice) } } @@ -64,13 +65,11 @@ extension MainViewController { guard let userInfoValue = userInfo["쥬스이름"], let juiceName = userInfoValue as? String else { print("userInfoValue 에러"); return } - let alert = UIAlertController(title: "\(juiceName) 쥬스 나왔습니다! 맛있게 드세요!", message: nil, preferredStyle: .alert) let confirmAction = UIAlertAction(title: "감사합니다.", style: .default) { _ in print("주문성공")} alert.addAction(confirmAction) present(alert, animated: true, completion: nil) } - @objc func alertMakingJuiceFail() { let alert = UIAlertController(title: "재료가 모자라요. 재고를 수정할까요?", message: nil, preferredStyle: .alert) let confirmAction = UIAlertAction(title: "예", style: .default){ _ in print("주문실패 - 예 선택")} @@ -79,5 +78,4 @@ extension MainViewController { alert.addAction(cancelAction) present(alert, animated: true, completion: nil) } - } diff --git a/JuiceMaker/JuiceMaker/Controller/ModifiyViewController.swift b/JuiceMaker/JuiceMaker/Controller/ModifiyViewController.swift index d4ba2ac43..93e104099 100644 --- a/JuiceMaker/JuiceMaker/Controller/ModifiyViewController.swift +++ b/JuiceMaker/JuiceMaker/Controller/ModifiyViewController.swift @@ -13,6 +13,5 @@ class ModifiyViewController: UIViewController { super.viewDidLoad() } - } diff --git a/JuiceMaker/JuiceMaker/Model/FruitStore.swift b/JuiceMaker/JuiceMaker/Model/FruitStore.swift index 096faf1c4..b16321796 100644 --- a/JuiceMaker/JuiceMaker/Model/FruitStore.swift +++ b/JuiceMaker/JuiceMaker/Model/FruitStore.swift @@ -20,10 +20,8 @@ class FruitStore { print("재고에 존재하지 않는 과일") return emptyQuantity } - return stock } - func changeStock(_ fruit: Fruit, _ changingQuantity: Int) { guard let stock = fruitStocks[fruit] else { print("재고에 존재하지 않는 과일") diff --git a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift index 8aae57c3f..656e99c99 100644 --- a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift +++ b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift @@ -22,7 +22,6 @@ struct JuiceMaker { NotificationCenter.default.post(name: Notification.Name(rawValue: "makeJuiceFail"), object: nil) } } - func checkStock(_ ingredients: Dictionary) throws { for (fruit, removingQuantity) in ingredients { if fruitStore.currentStock(fruit) < removingQuantity { From fe8d284b4d4373dd4e73c795b9cbac4c1eac1eaa Mon Sep 17 00:00:00 2001 From: taeyoung-Kim-KR Date: Fri, 18 Jun 2021 16:08:52 +0900 Subject: [PATCH 22/26] =?UTF-8?q?refactor=20:=20=ED=83=80=EC=9E=85=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기존 딕셔너리 타입에서 프로퍼티로 다시 생성하고 KVO를 사용하기 위해 코드를 수정 하였습니다. --- .../Controller/MainViewController.swift | 15 ++---- JuiceMaker/JuiceMaker/Model/FruitStore.swift | 52 +++++++++++++------ JuiceMaker/JuiceMaker/Model/JuiceMaker.swift | 1 + 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/JuiceMaker/JuiceMaker/Controller/MainViewController.swift b/JuiceMaker/JuiceMaker/Controller/MainViewController.swift index f1e525e47..64998ce5a 100644 --- a/JuiceMaker/JuiceMaker/Controller/MainViewController.swift +++ b/JuiceMaker/JuiceMaker/Controller/MainViewController.swift @@ -17,15 +17,14 @@ class MainViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - + NotificationCenter.default.addObserver(self, selector: #selector(alertMakingJuiceSuccess(_:)), name: Notification.Name(rawValue: "makeJuiceSuccess"), object: nil) NotificationCenter.default.addObserver(self, selector: #selector(alertMakingJuiceFail), name: Notification.Name(rawValue: "makeJuiceFail"), object: nil) - NotificationCenter.default.addObserver(self, selector: #selector(updateUILabel(_:)), - name: Notification.Name(rawValue: "updateUILabel"), object: nil) + for fruit in Fruit.allCases { - NotificationCenter.default.post(name: Notification.Name("updateUILabel"), object: nil, userInfo: ["과일종류":fruit]) + } } @@ -37,13 +36,7 @@ class MainViewController: UIViewController { } extension MainViewController { - @objc func updateUILabel(_ notification: Notification) { - guard let userInfo = notification.userInfo else { - print("userInfo 에러"); return - } - guard let userInfoValue = userInfo["과일종류"], let fruit = userInfoValue as? Fruit else { - print("userInfoValue 에러"); return - } + func updateUILabel(_ fruit: Fruit) { let currentStock = String(juiceMaker.fruitStore.currentStock(fruit)) switch fruit { case .strawberry: diff --git a/JuiceMaker/JuiceMaker/Model/FruitStore.swift b/JuiceMaker/JuiceMaker/Model/FruitStore.swift index b16321796..53a2fb9fd 100644 --- a/JuiceMaker/JuiceMaker/Model/FruitStore.swift +++ b/JuiceMaker/JuiceMaker/Model/FruitStore.swift @@ -8,26 +8,46 @@ import Foundation // 과일 타입 class FruitStore { - var fruitStocks: Dictionary - let emptyQuantity = 0 + @objc dynamic var strawberry = 10 + @objc dynamic var banana = 10 + @objc dynamic var pineapple = 10 + @objc dynamic var kiwi = 10 + @objc dynamic var mango = 10 - init() { - fruitStocks = [.strawberry:10, .banana:10, .pineapple:10, .kiwi:10, .mango:10] + subscript(_ fruit: Fruit) -> Int { + get { + switch fruit { + case .strawberry: + return strawberry + case .banana: + return banana + case .pineapple: + return pineapple + case .kiwi: + return kiwi + case .mango: + return mango + } + } + set { + switch fruit { + case .strawberry: + strawberry = newValue + case .banana: + banana = newValue + case .pineapple: + pineapple = newValue + case .kiwi: + kiwi = newValue + case .mango: + mango = newValue + } + } } - func currentStock(_ fruit: Fruit) -> Int { - guard let stock = fruitStocks[fruit] else { - print("재고에 존재하지 않는 과일") - return emptyQuantity - } - return stock + return self[fruit] } func changeStock(_ fruit: Fruit, _ changingQuantity: Int) { - guard let stock = fruitStocks[fruit] else { - print("재고에 존재하지 않는 과일") - return - } - fruitStocks.updateValue(stock - changingQuantity, forKey: fruit) - NotificationCenter.default.post(name: Notification.Name("updateUILabel"), object: nil, userInfo: ["과일종류":fruit]) + self[fruit] = currentStock(fruit) - changingQuantity } } diff --git a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift index 656e99c99..1a4b94a83 100644 --- a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift +++ b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift @@ -9,6 +9,7 @@ import Foundation // 쥬스 메이커 타입 struct JuiceMaker { let fruitStore = FruitStore() + func makeJuice(_ juice: Juice) { do { From f58c0a48c1858711f95f3810873546733b8565fd Mon Sep 17 00:00:00 2001 From: YongwooKim Date: Fri, 18 Jun 2021 19:09:46 +0900 Subject: [PATCH 23/26] =?UTF-8?q?feat=20:=20=EC=A5=AC=EC=8A=A4=20=EC=97=B4?= =?UTF-8?q?=EA=B1=B0=EC=B2=B4=20=EA=B0=9D=EC=B2=B4=ED=99=94=20=EC=8B=9C?= =?UTF-8?q?=EB=8F=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JuiceMaker/JuiceMaker/Model/EnumTypes.swift | 108 +++++++++++-------- JuiceMaker/JuiceMaker/Model/FruitStore.swift | 2 +- JuiceMaker/JuiceMaker/Model/JuiceMaker.swift | 8 +- 3 files changed, 68 insertions(+), 50 deletions(-) diff --git a/JuiceMaker/JuiceMaker/Model/EnumTypes.swift b/JuiceMaker/JuiceMaker/Model/EnumTypes.swift index 850364e36..81f7ea7c3 100644 --- a/JuiceMaker/JuiceMaker/Model/EnumTypes.swift +++ b/JuiceMaker/JuiceMaker/Model/EnumTypes.swift @@ -17,49 +17,67 @@ enum Fruit: CaseIterable { case mango } -enum Juice: String, CustomStringConvertible { - case strawberryBananaJuice = "딸바쥬스 주문" - case mangoKiwiJuice = "망키쥬스 주문" - case strawberryJuice = "딸기쥬스 주문" - case bananaJuice = "바나나쥬스 주문" - case mangoJuice = "망고쥬스 주문" - case kiwiJuice = "키위쥬스 주문" - case pineappleJuice = "파인애플쥬스 주문" - - var description: String { - switch self { - case .strawberryJuice: - return "딸기" - case .bananaJuice: - return "바나나" - case .pineappleJuice: - return "파인애플" - case .kiwiJuice: - return "키위" - case .mangoJuice: - return "망고" - case .strawberryBananaJuice: - return "딸바" - case .mangoKiwiJuice: - return "망키" - } - } - var ingredients: Dictionary { - switch self { - case .strawberryJuice: - return [.strawberry:16] - case .bananaJuice: - return [.banana:2] - case .pineappleJuice: - return [.pineapple:2] - case .kiwiJuice: - return [.kiwi:3] - case .mangoJuice: - return [.mango:3] - case .strawberryBananaJuice: - return [.strawberry:10, .banana:1] - case .mangoKiwiJuice: - return [.mango:2, .kiwi:1] - } - } +//enum Juice: String, CustomStringConvertible { +// case strawberryBananaJuice = "딸바쥬스 주문" +// case mangoKiwiJuice = "망키쥬스 주문" +// case strawberryJuice = "딸기쥬스 주문" +// case bananaJuice = "바나나쥬스 주문" +// case mangoJuice = "망고쥬스 주문" +// case kiwiJuice = "키위쥬스 주문" +// case pineappleJuice = "파인애플쥬스 주문" +// +// var description: String { +// switch self { +// case .strawberryJuice: +// return "딸기" +// case .bananaJuice: +// return "바나나" +// case .pineappleJuice: +// return "파인애플" +// case .kiwiJuice: +// return "키위" +// case .mangoJuice: +// return "망고" +// case .strawberryBananaJuice: +// return "딸바" +// case .mangoKiwiJuice: +// return "망키" +// } +// } +// var ingredients: Dictionary { +// switch self { +// case .strawberryJuice: +// return [.strawberry:16] +// case .bananaJuice: +// return [.banana:2] +// case .pineappleJuice: +// return [.pineapple:2] +// case .kiwiJuice: +// return [.kiwi:3] +// case .mangoJuice: +// return [.mango:3] +// case .strawberryBananaJuice: +// return [.strawberry:10, .banana:1] +// case .mangoKiwiJuice: +// return [.mango:2, .kiwi:1] +// } +// } +//} + +struct JuiceType { + var name: String + var ingredients: Array +} + +struct JuiceMenu { + var strawberryJuice = JuiceType(name: "딸기쥬스", ingredients: [16]) + var bananaJuice = JuiceType(name: "바나나쥬스", ingredients: [2]) + var pineappleJuice = JuiceType(name: "파인애플쥬스", ingredients: [2]) + var kiwiJuice = JuiceType(name: "키위쥬스", ingredients: [3]) + var mangoJuice = JuiceType(name: "망고쥬스", ingredients: [3]) + var strawberryBananaJuice = JuiceType(name: "딸바쥬스", ingredients: [10, 1]) + var mangoKiwiJuice = JuiceType(name: "망키쥬스", ingredients: [2, 1]) } + + +Int diff --git a/JuiceMaker/JuiceMaker/Model/FruitStore.swift b/JuiceMaker/JuiceMaker/Model/FruitStore.swift index 53a2fb9fd..1a9d87b4b 100644 --- a/JuiceMaker/JuiceMaker/Model/FruitStore.swift +++ b/JuiceMaker/JuiceMaker/Model/FruitStore.swift @@ -48,6 +48,6 @@ class FruitStore { return self[fruit] } func changeStock(_ fruit: Fruit, _ changingQuantity: Int) { - self[fruit] = currentStock(fruit) - changingQuantity + self[fruit] -= changingQuantity } } diff --git a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift index 1a4b94a83..523db4cf0 100644 --- a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift +++ b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift @@ -11,19 +11,19 @@ struct JuiceMaker { let fruitStore = FruitStore() - func makeJuice(_ juice: Juice) { + func makeJuice(_ juice: JuiceType) { do { try checkStock(juice.ingredients) - for (fruit, removingQuantity) in juice.ingredients { + for (fruit, removingQuantity) in juice. { fruitStore.changeStock(fruit, removingQuantity) } - NotificationCenter.default.post(name: Notification.Name(rawValue: "makeJuiceSuccess"), object: nil, userInfo: ["쥬스이름": juice.description]) +// NotificationCenter.default.post(name: Notification.Name(rawValue: "makeJuiceSuccess"), object: nil, userInfo: ["쥬스이름": juice.description]) } catch { NotificationCenter.default.post(name: Notification.Name(rawValue: "makeJuiceFail"), object: nil) } } - func checkStock(_ ingredients: Dictionary) throws { + func checkStock(_ ingredients: Array) throws { for (fruit, removingQuantity) in ingredients { if fruitStore.currentStock(fruit) < removingQuantity { throw JuiceMakerError.outOfStock From e7b1e5a9a9c28c6c35f019ca1cf11e1ed8954d55 Mon Sep 17 00:00:00 2001 From: YongwooKim Date: Fri, 18 Jun 2021 21:37:51 +0900 Subject: [PATCH 24/26] =?UTF-8?q?feat=20:=20enum=20=ED=83=80=EC=9E=85?= =?UTF-8?q?=EC=9D=98=20Juice=EB=A5=BC=20=EA=B5=AC=EC=A1=B0=EC=B2=B4?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 구조체로 변경된 Juice의 인스턴스 사용하는 형태로 변경 --- JuiceMaker/JuiceMaker/Model/EnumTypes.swift | 23 ++++++++------------ JuiceMaker/JuiceMaker/Model/FruitStore.swift | 2 +- JuiceMaker/JuiceMaker/Model/JuiceMaker.swift | 9 ++++---- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/JuiceMaker/JuiceMaker/Model/EnumTypes.swift b/JuiceMaker/JuiceMaker/Model/EnumTypes.swift index 81f7ea7c3..e560ff5e6 100644 --- a/JuiceMaker/JuiceMaker/Model/EnumTypes.swift +++ b/JuiceMaker/JuiceMaker/Model/EnumTypes.swift @@ -64,20 +64,15 @@ enum Fruit: CaseIterable { // } //} -struct JuiceType { +struct Juice { var name: String - var ingredients: Array + var ingredients: Dictionary } -struct JuiceMenu { - var strawberryJuice = JuiceType(name: "딸기쥬스", ingredients: [16]) - var bananaJuice = JuiceType(name: "바나나쥬스", ingredients: [2]) - var pineappleJuice = JuiceType(name: "파인애플쥬스", ingredients: [2]) - var kiwiJuice = JuiceType(name: "키위쥬스", ingredients: [3]) - var mangoJuice = JuiceType(name: "망고쥬스", ingredients: [3]) - var strawberryBananaJuice = JuiceType(name: "딸바쥬스", ingredients: [10, 1]) - var mangoKiwiJuice = JuiceType(name: "망키쥬스", ingredients: [2, 1]) -} - - -Int +var strawberryJuice = Juice(name: "딸기쥬스", ingredients: [.strawberry:16]) +var bananaJuice = Juice(name: "바나나쥬스", ingredients: [.banana:2]) +var pineappleJuice = Juice(name: "파인애플쥬스", ingredients: [.pineapple:2]) +var kiwiJuice = Juice(name: "키위쥬스", ingredients: [.kiwi:3]) +var mangoJuice = Juice(name: "망고쥬스", ingredients: [.mango:3]) +var strawberryBananaJuice = Juice(name: "딸바쥬스", ingredients: [.strawberry:10, .banana:1]) +var mangoKiwiJuice = Juice(name: "망키쥬스", ingredients: [.mango:2, .kiwi:1]) diff --git a/JuiceMaker/JuiceMaker/Model/FruitStore.swift b/JuiceMaker/JuiceMaker/Model/FruitStore.swift index 1a9d87b4b..53a2fb9fd 100644 --- a/JuiceMaker/JuiceMaker/Model/FruitStore.swift +++ b/JuiceMaker/JuiceMaker/Model/FruitStore.swift @@ -48,6 +48,6 @@ class FruitStore { return self[fruit] } func changeStock(_ fruit: Fruit, _ changingQuantity: Int) { - self[fruit] -= changingQuantity + self[fruit] = currentStock(fruit) - changingQuantity } } diff --git a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift index 523db4cf0..c67347a5e 100644 --- a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift +++ b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift @@ -10,20 +10,19 @@ import Foundation struct JuiceMaker { let fruitStore = FruitStore() - - func makeJuice(_ juice: JuiceType) { + func makeJuice(_ juice: Juice) { do { try checkStock(juice.ingredients) - for (fruit, removingQuantity) in juice. { + for (fruit, removingQuantity) in juice.ingredients { fruitStore.changeStock(fruit, removingQuantity) } // NotificationCenter.default.post(name: Notification.Name(rawValue: "makeJuiceSuccess"), object: nil, userInfo: ["쥬스이름": juice.description]) } catch { - NotificationCenter.default.post(name: Notification.Name(rawValue: "makeJuiceFail"), object: nil) +// NotificationCenter.default.post(name: Notification.Name(rawValue: "makeJuiceFail"), object: nil) } } - func checkStock(_ ingredients: Array) throws { + func checkStock(_ ingredients: Dictionary) throws { for (fruit, removingQuantity) in ingredients { if fruitStore.currentStock(fruit) < removingQuantity { throw JuiceMakerError.outOfStock From 5646fe1f6ba2931914ed57ce7fdce7d95523719b Mon Sep 17 00:00:00 2001 From: taeyoung-Kim-KR Date: Fri, 18 Jun 2021 22:02:35 +0900 Subject: [PATCH 25/26] =?UTF-8?q?feat=20:=20=EB=B2=84=ED=8A=BC=20=EA=B5=AC?= =?UTF-8?q?=EB=B3=84=20=EB=B0=A9=EC=8B=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 버튼이름을 직접 코드에 넣는 하드코딩 방식을 피하기 위해서 버튼 구별하는 방식을 변경하였습니다. --- .../Controller/MainViewController.swift | 32 ++++++++-- JuiceMaker/JuiceMaker/Model/EnumTypes.swift | 59 ------------------- JuiceMaker/JuiceMaker/Model/FruitStore.swift | 7 +++ JuiceMaker/JuiceMaker/Model/JuiceMaker.swift | 8 +++ .../View/Base.lproj/Main.storyboard | 14 ++--- 5 files changed, 50 insertions(+), 70 deletions(-) diff --git a/JuiceMaker/JuiceMaker/Controller/MainViewController.swift b/JuiceMaker/JuiceMaker/Controller/MainViewController.swift index 64998ce5a..b54518ab7 100644 --- a/JuiceMaker/JuiceMaker/Controller/MainViewController.swift +++ b/JuiceMaker/JuiceMaker/Controller/MainViewController.swift @@ -15,6 +15,14 @@ class MainViewController: UIViewController { let juiceMaker = JuiceMaker() + let strawberryJuice = Juice(name: "딸기쥬스", ingredients: [.strawberry:16]) + let bananaJuice = Juice(name: "바나나쥬스", ingredients: [.banana:2]) + let pineappleJuice = Juice(name: "파인애플쥬스", ingredients: [.pineapple:2]) + let kiwiJuice = Juice(name: "키위쥬스", ingredients: [.kiwi:3]) + let mangoJuice = Juice(name: "망고쥬스", ingredients: [.mango:3]) + let strawberryBananaJuice = Juice(name: "딸바쥬스", ingredients: [.strawberry:10, .banana:1]) + let mangoKiwiJuice = Juice(name: "망키쥬스", ingredients: [.mango:2, .kiwi:1]) + override func viewDidLoad() { super.viewDidLoad() @@ -28,10 +36,26 @@ class MainViewController: UIViewController { } } - @IBAction func juiceOrder(_ sender: UIButton) { - guard let titleLabel = sender.titleLabel else { print("버튼 타이틀 에러"); return } - guard let text = titleLabel.text, let juice = Juice(rawValue: text) else { print("쥬스 구분실패"); return } - juiceMaker.makeJuice(juice) + @IBAction func strawberryJuiceOrder(_ sender: Any) { + juiceMaker.makeJuice(strawberryJuice) + } + @IBAction func bananaJuiceOrder(_ sender: Any) { + juiceMaker.makeJuice(bananaJuice) + } + @IBAction func mangoJuiceOrder(_ sender: Any) { + juiceMaker.makeJuice(pineappleJuice) + } + @IBAction func kiwiJuiceOrder(_ sender: Any) { + juiceMaker.makeJuice(kiwiJuice) + } + @IBAction func pineappleJuiceOrder(_ sender: Any) { + juiceMaker.makeJuice(pineappleJuice) + } + @IBAction func strawberryBananaJuiceOrder(_ sender: Any) { + juiceMaker.makeJuice(strawberryBananaJuice) + } + @IBAction func mangoKiwiJuiceOrder(_ sender: Any) { + juiceMaker.makeJuice(mangoKiwiJuice) } } diff --git a/JuiceMaker/JuiceMaker/Model/EnumTypes.swift b/JuiceMaker/JuiceMaker/Model/EnumTypes.swift index e560ff5e6..c472993ba 100644 --- a/JuiceMaker/JuiceMaker/Model/EnumTypes.swift +++ b/JuiceMaker/JuiceMaker/Model/EnumTypes.swift @@ -5,65 +5,6 @@ // Created by Yongwoo Marco on 2021/06/15. // -enum JuiceMakerError: Error { - case outOfStock -} - -enum Fruit: CaseIterable { - case strawberry - case banana - case pineapple - case kiwi - case mango -} - -//enum Juice: String, CustomStringConvertible { -// case strawberryBananaJuice = "딸바쥬스 주문" -// case mangoKiwiJuice = "망키쥬스 주문" -// case strawberryJuice = "딸기쥬스 주문" -// case bananaJuice = "바나나쥬스 주문" -// case mangoJuice = "망고쥬스 주문" -// case kiwiJuice = "키위쥬스 주문" -// case pineappleJuice = "파인애플쥬스 주문" -// -// var description: String { -// switch self { -// case .strawberryJuice: -// return "딸기" -// case .bananaJuice: -// return "바나나" -// case .pineappleJuice: -// return "파인애플" -// case .kiwiJuice: -// return "키위" -// case .mangoJuice: -// return "망고" -// case .strawberryBananaJuice: -// return "딸바" -// case .mangoKiwiJuice: -// return "망키" -// } -// } -// var ingredients: Dictionary { -// switch self { -// case .strawberryJuice: -// return [.strawberry:16] -// case .bananaJuice: -// return [.banana:2] -// case .pineappleJuice: -// return [.pineapple:2] -// case .kiwiJuice: -// return [.kiwi:3] -// case .mangoJuice: -// return [.mango:3] -// case .strawberryBananaJuice: -// return [.strawberry:10, .banana:1] -// case .mangoKiwiJuice: -// return [.mango:2, .kiwi:1] -// } -// } -//} - struct Juice { var name: String var ingredients: Dictionary diff --git a/JuiceMaker/JuiceMaker/Model/FruitStore.swift b/JuiceMaker/JuiceMaker/Model/FruitStore.swift index 53a2fb9fd..244abc319 100644 --- a/JuiceMaker/JuiceMaker/Model/FruitStore.swift +++ b/JuiceMaker/JuiceMaker/Model/FruitStore.swift @@ -6,6 +6,13 @@ import Foundation +enum Fruit: CaseIterable { + case strawberry + case banana + case pineapple + case kiwi + case mango +} // 과일 타입 class FruitStore { @objc dynamic var strawberry = 10 diff --git a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift index c67347a5e..25bf8b48c 100644 --- a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift +++ b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift @@ -6,6 +6,14 @@ import Foundation +enum JuiceMakerError: Error { + case outOfStock +} + +struct Juice { + var name: String + var ingredients: Dictionary +} // 쥬스 메이커 타입 struct JuiceMaker { let fruitStore = FruitStore() diff --git a/JuiceMaker/JuiceMaker/View/Base.lproj/Main.storyboard b/JuiceMaker/JuiceMaker/View/Base.lproj/Main.storyboard index 3ea1a1d65..2dc4227b0 100644 --- a/JuiceMaker/JuiceMaker/View/Base.lproj/Main.storyboard +++ b/JuiceMaker/JuiceMaker/View/Base.lproj/Main.storyboard @@ -126,7 +126,7 @@ - + @@ -156,7 +156,7 @@ - + @@ -183,7 +183,7 @@ - + @@ -207,7 +207,7 @@ - + From 45f0d2fdd59cb8878541d479db258c8252238458 Mon Sep 17 00:00:00 2001 From: YongwooKim Date: Fri, 18 Jun 2021 23:41:03 +0900 Subject: [PATCH 26/26] =?UTF-8?q?feat=20:=20KVO=20=ED=98=95=ED=83=9C?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=ED=99=94=EA=B0=92=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../JuiceMaker.xcodeproj/project.pbxproj | 4 -- .../Controller/MainViewController.swift | 37 +++++++++++++++---- JuiceMaker/JuiceMaker/Model/EnumTypes.swift | 19 ---------- JuiceMaker/JuiceMaker/Model/FruitStore.swift | 2 +- 4 files changed, 30 insertions(+), 32 deletions(-) delete mode 100644 JuiceMaker/JuiceMaker/Model/EnumTypes.swift diff --git a/JuiceMaker/JuiceMaker.xcodeproj/project.pbxproj b/JuiceMaker/JuiceMaker.xcodeproj/project.pbxproj index 16daee456..e90d2afc4 100644 --- a/JuiceMaker/JuiceMaker.xcodeproj/project.pbxproj +++ b/JuiceMaker/JuiceMaker.xcodeproj/project.pbxproj @@ -7,7 +7,6 @@ objects = { /* Begin PBXBuildFile section */ - A99B246226783EBC00F94FCC /* EnumTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = A99B246126783EBC00F94FCC /* EnumTypes.swift */; }; B463F9C3267796BA0073F37A /* ModifiyViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B463F9C2267796BA0073F37A /* ModifiyViewController.swift */; }; C71CD66B266C7ACB0038B9CB /* FruitStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = C71CD66A266C7ACB0038B9CB /* FruitStore.swift */; }; C73DAF37255D0CDD00020D38 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C73DAF36255D0CDD00020D38 /* AppDelegate.swift */; }; @@ -20,7 +19,6 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ - A99B246126783EBC00F94FCC /* EnumTypes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnumTypes.swift; sourceTree = ""; }; B463F9C2267796BA0073F37A /* ModifiyViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModifiyViewController.swift; sourceTree = ""; }; C71CD66A266C7ACB0038B9CB /* FruitStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FruitStore.swift; sourceTree = ""; }; C73DAF33255D0CDD00020D38 /* JuiceMaker.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = JuiceMaker.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -61,7 +59,6 @@ children = ( C73DAF4B255D0D0400020D38 /* JuiceMaker.swift */, C71CD66A266C7ACB0038B9CB /* FruitStore.swift */, - A99B246126783EBC00F94FCC /* EnumTypes.swift */, ); path = Model; sourceTree = ""; @@ -178,7 +175,6 @@ C73DAF37255D0CDD00020D38 /* AppDelegate.swift in Sources */, C73DAF39255D0CDD00020D38 /* SceneDelegate.swift in Sources */, B463F9C3267796BA0073F37A /* ModifiyViewController.swift in Sources */, - A99B246226783EBC00F94FCC /* EnumTypes.swift in Sources */, C73DAF4C255D0D0400020D38 /* JuiceMaker.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/JuiceMaker/JuiceMaker/Controller/MainViewController.swift b/JuiceMaker/JuiceMaker/Controller/MainViewController.swift index b54518ab7..bea5ab61a 100644 --- a/JuiceMaker/JuiceMaker/Controller/MainViewController.swift +++ b/JuiceMaker/JuiceMaker/Controller/MainViewController.swift @@ -14,6 +14,7 @@ class MainViewController: UIViewController { @IBOutlet weak var pineappleLabel: UILabel! let juiceMaker = JuiceMaker() + var observations = [NSKeyValueObservation]() let strawberryJuice = Juice(name: "딸기쥬스", ingredients: [.strawberry:16]) let bananaJuice = Juice(name: "바나나쥬스", ingredients: [.banana:2]) @@ -26,13 +27,31 @@ class MainViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() - NotificationCenter.default.addObserver(self, selector: #selector(alertMakingJuiceSuccess(_:)), - name: Notification.Name(rawValue: "makeJuiceSuccess"), object: nil) - NotificationCenter.default.addObserver(self, selector: #selector(alertMakingJuiceFail), - name: Notification.Name(rawValue: "makeJuiceFail"), object: nil) +// NotificationCenter.default.addObserver(self, selector: #selector(alertMakingJuiceSuccess(_:)), +// name: Notification.Name(rawValue: "makeJuiceSuccess"), object: nil) +// NotificationCenter.default.addObserver(self, selector: #selector(alertMakingJuiceFail), +// name: Notification.Name(rawValue: "makeJuiceFail"), object: nil) + + observations = [ + juiceMaker.fruitStore.observe(\.strawberry, options: [.new]) { _, _ in + self.updateUILabel(.strawberry) + }, + juiceMaker.fruitStore.observe(\.banana, options: [.new]) { _, _ in + self.updateUILabel(.banana) + }, + juiceMaker.fruitStore.observe(\.pineapple, options: [.new]) { _, _ in + self.updateUILabel(.pineapple) + }, + juiceMaker.fruitStore.observe(\.kiwi, options: [.new]) { _, _ in + self.updateUILabel(.kiwi) + }, + juiceMaker.fruitStore.observe(\.mango, options: [.new]) { _, _ in + self.updateUILabel(.mango) + } + ] for fruit in Fruit.allCases { - + updateUILabel(fruit) } } @@ -83,14 +102,16 @@ extension MainViewController { print("userInfoValue 에러"); return } let alert = UIAlertController(title: "\(juiceName) 쥬스 나왔습니다! 맛있게 드세요!", message: nil, preferredStyle: .alert) - let confirmAction = UIAlertAction(title: "감사합니다.", style: .default) { _ in print("주문성공")} + let confirmAction = UIAlertAction(title: "감사합니다.", style: .default) alert.addAction(confirmAction) present(alert, animated: true, completion: nil) } @objc func alertMakingJuiceFail() { let alert = UIAlertController(title: "재료가 모자라요. 재고를 수정할까요?", message: nil, preferredStyle: .alert) - let confirmAction = UIAlertAction(title: "예", style: .default){ _ in print("주문실패 - 예 선택")} - let cancelAction = UIAlertAction(title: "아니오", style: .default){ _ in print("주문실패 - 아니오 선택")} + let confirmAction = UIAlertAction(title: "예", style: .default){ _ in + + } + let cancelAction = UIAlertAction(title: "아니오", style: .default) alert.addAction(confirmAction) alert.addAction(cancelAction) present(alert, animated: true, completion: nil) diff --git a/JuiceMaker/JuiceMaker/Model/EnumTypes.swift b/JuiceMaker/JuiceMaker/Model/EnumTypes.swift deleted file mode 100644 index c472993ba..000000000 --- a/JuiceMaker/JuiceMaker/Model/EnumTypes.swift +++ /dev/null @@ -1,19 +0,0 @@ -// -// EnumTypes.swift -// JuiceMaker -// -// Created by Yongwoo Marco on 2021/06/15. -// - -struct Juice { - var name: String - var ingredients: Dictionary -} - -var strawberryJuice = Juice(name: "딸기쥬스", ingredients: [.strawberry:16]) -var bananaJuice = Juice(name: "바나나쥬스", ingredients: [.banana:2]) -var pineappleJuice = Juice(name: "파인애플쥬스", ingredients: [.pineapple:2]) -var kiwiJuice = Juice(name: "키위쥬스", ingredients: [.kiwi:3]) -var mangoJuice = Juice(name: "망고쥬스", ingredients: [.mango:3]) -var strawberryBananaJuice = Juice(name: "딸바쥬스", ingredients: [.strawberry:10, .banana:1]) -var mangoKiwiJuice = Juice(name: "망키쥬스", ingredients: [.mango:2, .kiwi:1]) diff --git a/JuiceMaker/JuiceMaker/Model/FruitStore.swift b/JuiceMaker/JuiceMaker/Model/FruitStore.swift index 244abc319..cb0e6f4b4 100644 --- a/JuiceMaker/JuiceMaker/Model/FruitStore.swift +++ b/JuiceMaker/JuiceMaker/Model/FruitStore.swift @@ -14,7 +14,7 @@ enum Fruit: CaseIterable { case mango } // 과일 타입 -class FruitStore { +class FruitStore: NSObject { @objc dynamic var strawberry = 10 @objc dynamic var banana = 10 @objc dynamic var pineapple = 10