-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from Keeplo/step_2
쥬스메이커 [STEP 2] yoshikim, Marco
- Loading branch information
Showing
8 changed files
with
268 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
JuiceMaker/JuiceMaker/Controller/MainViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
JuiceMaker/JuiceMaker/Controller/ModifiyViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// ModifiyViewController.swift | ||
// JuiceMaker | ||
// | ||
// Created by 김태영 on 2021/06/14. | ||
// | ||
|
||
import UIKit | ||
|
||
class ModifiyViewController: UIViewController { | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.