diff --git a/JuiceMaker/JuiceMaker.xcodeproj/project.pbxproj b/JuiceMaker/JuiceMaker.xcodeproj/project.pbxproj index 6ddf01d1e..e90d2afc4 100644 --- a/JuiceMaker/JuiceMaker.xcodeproj/project.pbxproj +++ b/JuiceMaker/JuiceMaker.xcodeproj/project.pbxproj @@ -7,10 +7,11 @@ objects = { /* Begin PBXBuildFile section */ + 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 */; }; C73DAF39255D0CDD00020D38 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C73DAF38255D0CDD00020D38 /* SceneDelegate.swift */; }; - C73DAF3B255D0CDD00020D38 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C73DAF3A255D0CDD00020D38 /* ViewController.swift */; }; + C73DAF3B255D0CDD00020D38 /* MainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C73DAF3A255D0CDD00020D38 /* MainViewController.swift */; }; C73DAF3E255D0CDD00020D38 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C73DAF3C255D0CDD00020D38 /* Main.storyboard */; }; C73DAF40255D0CDE00020D38 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C73DAF3F255D0CDE00020D38 /* Assets.xcassets */; }; C73DAF43255D0CDF00020D38 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C73DAF41255D0CDF00020D38 /* LaunchScreen.storyboard */; }; @@ -18,11 +19,12 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 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; }; C73DAF36255D0CDD00020D38 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; C73DAF38255D0CDD00020D38 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; - C73DAF3A255D0CDD00020D38 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; + C73DAF3A255D0CDD00020D38 /* MainViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainViewController.swift; sourceTree = ""; }; C73DAF3D255D0CDD00020D38 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; C73DAF3F255D0CDE00020D38 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; C73DAF42255D0CDF00020D38 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; @@ -46,7 +48,8 @@ children = ( C73DAF36255D0CDD00020D38 /* AppDelegate.swift */, C73DAF38255D0CDD00020D38 /* SceneDelegate.swift */, - C73DAF3A255D0CDD00020D38 /* ViewController.swift */, + C73DAF3A255D0CDD00020D38 /* MainViewController.swift */, + B463F9C2267796BA0073F37A /* ModifiyViewController.swift */, ); path = Controller; sourceTree = ""; @@ -168,9 +171,10 @@ buildActionMask = 2147483647; files = ( C71CD66B266C7ACB0038B9CB /* FruitStore.swift in Sources */, - C73DAF3B255D0CDD00020D38 /* ViewController.swift in Sources */, + C73DAF3B255D0CDD00020D38 /* MainViewController.swift in Sources */, C73DAF37255D0CDD00020D38 /* AppDelegate.swift in Sources */, C73DAF39255D0CDD00020D38 /* SceneDelegate.swift in Sources */, + B463F9C3267796BA0073F37A /* ModifiyViewController.swift in Sources */, C73DAF4C255D0D0400020D38 /* JuiceMaker.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/JuiceMaker/JuiceMaker/Controller/MainViewController.swift b/JuiceMaker/JuiceMaker/Controller/MainViewController.swift new file mode 100644 index 000000000..bea5ab61a --- /dev/null +++ b/JuiceMaker/JuiceMaker/Controller/MainViewController.swift @@ -0,0 +1,119 @@ +// +// JuiceMaker - ViewController.swift +// Created by yagom. +// Copyright © yagom academy. All rights reserved. +// + +import UIKit + +class MainViewController: UIViewController { + @IBOutlet weak var strawberryLabel: UILabel! + @IBOutlet weak var bananaLabel: UILabel! + @IBOutlet weak var mangoLabel: UILabel! + @IBOutlet weak var kiwiLabel: UILabel! + @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]) + 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() + +// 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) + } + } + + @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) + } +} + +extension MainViewController { + func updateUILabel(_ fruit: Fruit) { + let currentStock = String(juiceMaker.fruitStore.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 alertMakingJuiceSuccess(_ notification: Notification) { + guard let userInfo = notification.userInfo else { + print("userInfo 에러"); return + } + 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) + 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 + + } + let cancelAction = UIAlertAction(title: "아니오", style: .default) + alert.addAction(confirmAction) + alert.addAction(cancelAction) + present(alert, animated: true, completion: nil) + } +} diff --git a/JuiceMaker/JuiceMaker/Controller/ModifiyViewController.swift b/JuiceMaker/JuiceMaker/Controller/ModifiyViewController.swift new file mode 100644 index 000000000..93e104099 --- /dev/null +++ b/JuiceMaker/JuiceMaker/Controller/ModifiyViewController.swift @@ -0,0 +1,17 @@ +// +// ModifiyViewController.swift +// JuiceMaker +// +// Created by 김태영 on 2021/06/14. +// + +import UIKit + +class ModifiyViewController: UIViewController { + + override func viewDidLoad() { + super.viewDidLoad() + + } + +} diff --git a/JuiceMaker/JuiceMaker/Controller/ViewController.swift b/JuiceMaker/JuiceMaker/Controller/ViewController.swift deleted file mode 100644 index 19f26dc83..000000000 --- a/JuiceMaker/JuiceMaker/Controller/ViewController.swift +++ /dev/null @@ -1,18 +0,0 @@ -// -// JuiceMaker - ViewController.swift -// Created by yagom. -// Copyright © yagom academy. All rights reserved. -// - -import UIKit - -class ViewController: UIViewController { - - override func viewDidLoad() { - super.viewDidLoad() - // Do any additional setup after loading the view. - } - - -} - diff --git a/JuiceMaker/JuiceMaker/Model/FruitStore.swift b/JuiceMaker/JuiceMaker/Model/FruitStore.swift index a1e925afc..cb0e6f4b4 100644 --- a/JuiceMaker/JuiceMaker/Model/FruitStore.swift +++ b/JuiceMaker/JuiceMaker/Model/FruitStore.swift @@ -6,7 +6,55 @@ import Foundation +enum Fruit: CaseIterable { + case strawberry + case banana + case pineapple + case kiwi + case mango +} // 과일 타입 -class FruitStore { +class FruitStore: NSObject { + @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 + 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 { + return self[fruit] + } + func changeStock(_ fruit: Fruit, _ changingQuantity: Int) { + self[fruit] = currentStock(fruit) - changingQuantity + } } diff --git a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift index 0c8629e1d..25bf8b48c 100644 --- a/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift +++ b/JuiceMaker/JuiceMaker/Model/JuiceMaker.swift @@ -6,7 +6,35 @@ import Foundation -// 쥬스 메이커 타입 +enum JuiceMakerError: Error { + case outOfStock +} + +struct Juice { + var name: String + var ingredients: Dictionary +} +// 쥬스 메이커 타입 struct JuiceMaker { - + let fruitStore = FruitStore() + + func makeJuice(_ juice: Juice) { + do { + try checkStock(juice.ingredients) + + 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) + } + } + func checkStock(_ ingredients: Dictionary) throws { + for (fruit, removingQuantity) in ingredients { + if fruitStore.currentStock(fruit) < removingQuantity { + throw JuiceMakerError.outOfStock + } + } + } } diff --git a/JuiceMaker/JuiceMaker/View/Base.lproj/Main.storyboard b/JuiceMaker/JuiceMaker/View/Base.lproj/Main.storyboard index 7abdcc72a..2dc4227b0 100644 --- a/JuiceMaker/JuiceMaker/View/Base.lproj/Main.storyboard +++ b/JuiceMaker/JuiceMaker/View/Base.lproj/Main.storyboard @@ -12,7 +12,7 @@ - + @@ -59,7 +59,7 @@ - @@ -167,9 +179,12 @@ - + + + + @@ -185,9 +203,12 @@ - + + + + @@ -217,10 +238,17 @@ + + + + + + + - + @@ -238,12 +266,12 @@ - + - + - + @@ -302,7 +330,7 @@ - - + + + diff --git a/Readme.md b/Readme.md new file mode 100644 index 000000000..d31a20c7c --- /dev/null +++ b/Readme.md @@ -0,0 +1 @@ +#쥬스 메이커