-
Notifications
You must be signed in to change notification settings - Fork 167
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] 제이티, Ari #119
Changes from 12 commits
68927c4
c8f2ac6
e083fff
293f811
a560ab2
57031d7
eb48bef
0331594
8edd150
b28fab2
5d1c1da
f7bb20f
8bfece8
5f09d5c
d252b60
0484cd1
494468c
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 |
---|---|---|
|
@@ -8,11 +8,128 @@ import UIKit | |
|
||
class JuiceMakerViewController: UIViewController { | ||
|
||
private let juiceMaker = JuiceMaker() | ||
|
||
@IBOutlet weak var strawberryStockLabel: UILabel! | ||
@IBOutlet weak var bananaStockLabel: UILabel! | ||
@IBOutlet weak var pineappleStockLabel: UILabel! | ||
@IBOutlet weak var kiwiStockLabel: UILabel! | ||
@IBOutlet weak var mangoStockLabel: UILabel! | ||
|
||
@IBOutlet weak var orderStrawberryBananaJuiceButton: UIButton! | ||
@IBOutlet weak var orderMangoKiwiJuiceButton: UIButton! | ||
@IBOutlet weak var orderStrawberryJuiceButton: UIButton! | ||
@IBOutlet weak var orderBananaJuiceButton: UIButton! | ||
@IBOutlet weak var orderPineappleJuiceButton: UIButton! | ||
@IBOutlet weak var orderKiwiJuiceButton: UIButton! | ||
@IBOutlet weak var orderMangoJuiceButton: UIButton! | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
// Do any additional setup after loading the view. | ||
updateFruitLabels() | ||
|
||
NotificationCenter.default.addObserver(self, selector: #selector(fruitLabelChanged(notification:)), name: .changedFruitStockNotification, object: nil) | ||
} | ||
|
||
@IBAction func orderJuiceButtonTapped(_ sender: UIButton) { | ||
let juice: Juice | ||
|
||
switch sender { | ||
case orderStrawberryBananaJuiceButton: | ||
juice = .strawberryBanana | ||
case orderMangoKiwiJuiceButton: | ||
juice = .mangoKiwi | ||
case orderStrawberryJuiceButton: | ||
juice = .strawberry | ||
case orderBananaJuiceButton: | ||
juice = .banana | ||
case orderPineappleJuiceButton: | ||
juice = .pineapple | ||
case orderKiwiJuiceButton: | ||
juice = .kiwi | ||
case orderMangoJuiceButton: | ||
juice = .mango | ||
default: | ||
showNotificationAlert(message: "잘못된 접근입니다.") | ||
return | ||
} | ||
|
||
mixFruit(juice: juice) | ||
} | ||
|
||
func mixFruit(juice: Juice) { | ||
do { | ||
try juiceMaker.mixFruit(juice: juice) | ||
showNotificationAlert(message: "\(juice) 쥬스 나왔습니다! 맛있게 드세요!") | ||
} catch RequestError.fruitStockOut { | ||
showOutOfStockAlert() | ||
} catch let error as RequestError { | ||
showNotificationAlert(message: error.errorDescription) | ||
} catch { | ||
showNotificationAlert(message: "알 수 없는 에러가 발생했습니다.") | ||
} | ||
} | ||
|
||
@objc func fruitLabelChanged(notification: Notification) { | ||
guard let fruit = notification.object as? Fruit else { | ||
showNotificationAlert(message: "알 수 없는 에러가 발생했습니다.") | ||
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. 코드 전반에 걸쳐서 "알수없는~" 이 문장이 반복해서 사용되는데 따로 관리할 수 있는 법은 없나요? |
||
return | ||
} | ||
currentStockLabelUpdate(fruit: fruit, label: fruitlabel(of: fruit)) | ||
} | ||
|
||
func updateFruitLabels() { | ||
currentStockLabelUpdate(fruit: .strawberry, label: strawberryStockLabel) | ||
currentStockLabelUpdate(fruit: .banana, label: bananaStockLabel) | ||
currentStockLabelUpdate(fruit: .pineapple, label: pineappleStockLabel) | ||
currentStockLabelUpdate(fruit: .kiwi, label: kiwiStockLabel) | ||
currentStockLabelUpdate(fruit: .mango, label: mangoStockLabel) | ||
} | ||
|
||
func currentStockLabelUpdate(fruit: Fruit, label: UILabel) { | ||
do { | ||
let stock = try FruitStore.shared.stock(fruit: fruit) | ||
label.text = String(stock) | ||
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. CustomStringConvertible 을 채택해서 String(stock) 으로 표현해서 label.text 에서 사용한 이유가 있나요? stock.descriptionText 같은 스타일로 사용할 수도 있지 않나요? |
||
} catch let error as RequestError { | ||
showNotificationAlert(message: error.errorDescription) | ||
} catch { | ||
showNotificationAlert(message: "알 수 없는 에러가 발생했습니다.") | ||
} | ||
} | ||
|
||
func fruitlabel(of fruit: Fruit) -> UILabel { | ||
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. 이 메소드는 어떤 역할을 하는 메소드인가요? |
||
switch fruit { | ||
case .strawberry: | ||
return strawberryStockLabel | ||
case .banana: | ||
return bananaStockLabel | ||
case .pineapple: | ||
return pineappleStockLabel | ||
case .kiwi: | ||
return kiwiStockLabel | ||
case .mango: | ||
return mangoStockLabel | ||
} | ||
} | ||
|
||
func showNotificationAlert(message: String, title: String = "OK") { | ||
let alert = UIAlertController(title: nil, message: message, preferredStyle: .alert) | ||
let cancel = UIAlertAction(title: title, style: .cancel, handler: nil) | ||
alert.addAction(cancel) | ||
present(alert, animated: true, completion: nil) | ||
} | ||
|
||
func showOutOfStockAlert() { | ||
let alert = UIAlertController(title: nil, message: "재료가 모자라요. 재고를 수정할까요?", preferredStyle: .alert) | ||
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) | ||
let okAction = UIAlertAction(title: "OK", style: .default) { _ in | ||
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. 여기 message 나 title에 들어가는 문자열들을 따로 관리할 방법이 없나요? 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. enum으로 따로 분리하여 관리하도록 개선하였습니다! 감사합니다! |
||
guard let viewController = self.storyboard?.instantiateViewController(withIdentifier: "FruitStoreView") else { return } | ||
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.
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. 해당 부분도 피드백 반영하여 개선해보았습니다 😄 |
||
self.present(viewController, animated: true, completion: nil) | ||
} | ||
alert.addAction(cancelAction) | ||
alert.addAction(okAction) | ||
present(alert, animated: true, completion: nil) | ||
} | ||
|
||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,18 +8,16 @@ import Foundation | |
|
||
private let defaultFruitCount = 10 | ||
|
||
enum Fruit: CaseIterable { | ||
case strawberry | ||
case banana | ||
case pineapple | ||
case kiwi | ||
case mango | ||
} | ||
|
||
class FruitStore { | ||
|
||
static let shared: FruitStore = FruitStore() | ||
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. JuiceMaker에 FruitStore 객체를 외부에서 주입해주지 않고 singleton 으로 넣은 이유가 있나요? |
||
|
||
enum Fruit: CaseIterable { | ||
case strawberry | ||
case banana | ||
case pineapple | ||
case kiwi | ||
case mango | ||
} | ||
|
||
private var fruitBasket: [Fruit: Int] | ||
|
||
init(count: Int = defaultFruitCount) { | ||
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. defaultFruitCount 가 FruitStore 내에서만 쓰이는데 안에서 관리하는게 낫지 않을까요? |
||
|
@@ -29,6 +27,13 @@ class FruitStore { | |
self.fruitBasket = Dictionary(uniqueKeysWithValues: zip(allFruits, fruitscount)) | ||
} | ||
|
||
func stock(fruit: Fruit) throws -> Int { | ||
guard let fruitStock = fruitBasket[fruit] else { | ||
throw RequestError.fruitNotFound | ||
} | ||
return fruitStock | ||
} | ||
|
||
func addFruitStock(fruit: Fruit, count: Int) throws { | ||
try changeAmount(count: count, of: fruit, by: +) | ||
} | ||
|
@@ -49,6 +54,8 @@ class FruitStore { | |
} | ||
let newFruitCount = calculator(oldFruitCount, count) | ||
fruitBasket[fruit] = newFruitCount | ||
|
||
NotificationCenter.default.post(name: .changedFruitStockNotification, object: fruit) | ||
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. NotificationCenter 를 사용해보셨군요 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 hasFruitStock(of fruit: Fruit, count fruitCountSubtracted: Int) -> Bool { | ||
|
@@ -58,3 +65,6 @@ class FruitStore { | |
} | ||
|
||
|
||
extension Notification.Name { | ||
static let changedFruitStockNotification = Notification.Name("changeFruitStock") | ||
} |
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.
모든 button 의 action method 를 개별로 구현할 때와 이렇게 하나에 구현할 때의 장단점은 무엇인가요?