-
Notifications
You must be signed in to change notification settings - Fork 165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
쥬스메이커 [STEP 2] yoshikim, Marco #85
Changes from all commits
905c3f0
246a02a
e68220f
c5e76a3
a6d8205
2eb3ab8
12158b4
56adcec
2a1a6b0
dbb4679
ca4ff2a
6b7480c
202411e
b1381d5
3c43ff9
a5e679d
02a17b7
dcf3513
4aa4ae5
f37d256
b978ab9
fe8d284
f58c0a4
e7b1e5a
5646fe1
45f0d2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
} | ||
} |
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.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,35 @@ | |
|
||
import Foundation | ||
|
||
// 쥬스 메이커 타입 | ||
enum JuiceMakerError: Error { | ||
case outOfStock | ||
} | ||
|
||
struct Juice { | ||
var name: String | ||
var ingredients: Dictionary<Fruit, Int> | ||
} | ||
// 쥬스 메이커 타입 | ||
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) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if - else 는 두 가지 상황을 주어쥐고 상황에 맞게 동작하는 느낌이 있는 반면 makeJuice() 는 주스만들기 동작이 주 기능이고 주스만들기 실패(재고없음) 인 경우가 특수한경우 또는 오류의 개념으로 접근해서 do-catch 형태로 구현하고 Error Handling을 적용해 보았습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 에러핸들링을 이용해서 동작의 흐름을 제어하는 구현을 하신 점 좋습니다 👍 |
||
func checkStock(_ ingredients: Dictionary<Fruit, Int>) throws { | ||
for (fruit, removingQuantity) in ingredients { | ||
if fruitStore.currentStock(fruit) < removingQuantity { | ||
throw JuiceMakerError.outOfStock | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
notification을 통해서 객체의 상태값을 받고있어서 옵셔널 언래핑이나 타입 체크 코드가 필요한 상황으로 보이는데,
notification이 아닌 다른 방식으로 구현한다면 이런 guard문은 없어져도 되는 코드가 되겠네요!
그리고 guard문은 원하는 조건이 아닐 때 해당 블럭을(지금으로 치면 함수겠죠?) 다 타지 않고 리턴해버려야 할 때 주로 쓰게되는데요
현재 구조에서는 userInfo값의 오류때문에 에러처리를 할 필요는 없어보입니다!