Skip to content

Commit

Permalink
Merge pull request #85 from Keeplo/step_2
Browse files Browse the repository at this point in the history
쥬스메이커 [STEP 2] yoshikim, Marco
  • Loading branch information
jinios authored Jun 19, 2021
2 parents 822fc64 + 45f0d2f commit 472bf71
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 39 deletions.
12 changes: 8 additions & 4 deletions JuiceMaker/JuiceMaker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@
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 */; };
C73DAF4C255D0D0400020D38 /* JuiceMaker.swift in Sources */ = {isa = PBXBuildFile; fileRef = C73DAF4B255D0D0400020D38 /* JuiceMaker.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
B463F9C2267796BA0073F37A /* ModifiyViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ModifiyViewController.swift; sourceTree = "<group>"; };
C71CD66A266C7ACB0038B9CB /* FruitStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FruitStore.swift; sourceTree = "<group>"; };
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 = "<group>"; };
C73DAF38255D0CDD00020D38 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
C73DAF3A255D0CDD00020D38 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
C73DAF3A255D0CDD00020D38 /* MainViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainViewController.swift; sourceTree = "<group>"; };
C73DAF3D255D0CDD00020D38 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
C73DAF3F255D0CDE00020D38 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
C73DAF42255D0CDF00020D38 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
Expand All @@ -46,7 +48,8 @@
children = (
C73DAF36255D0CDD00020D38 /* AppDelegate.swift */,
C73DAF38255D0CDD00020D38 /* SceneDelegate.swift */,
C73DAF3A255D0CDD00020D38 /* ViewController.swift */,
C73DAF3A255D0CDD00020D38 /* MainViewController.swift */,
B463F9C2267796BA0073F37A /* ModifiyViewController.swift */,
);
path = Controller;
sourceTree = "<group>";
Expand Down Expand Up @@ -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;
Expand Down
119 changes: 119 additions & 0 deletions JuiceMaker/JuiceMaker/Controller/MainViewController.swift
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 JuiceMaker/JuiceMaker/Controller/ModifiyViewController.swift
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()

}

}
18 changes: 0 additions & 18 deletions JuiceMaker/JuiceMaker/Controller/ViewController.swift

This file was deleted.

50 changes: 49 additions & 1 deletion JuiceMaker/JuiceMaker/Model/FruitStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
32 changes: 30 additions & 2 deletions JuiceMaker/JuiceMaker/Model/JuiceMaker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
func checkStock(_ ingredients: Dictionary<Fruit, Int>) throws {
for (fruit, removingQuantity) in ingredients {
if fruitStore.currentStock(fruit) < removingQuantity {
throw JuiceMakerError.outOfStock
}
}
}
}
Loading

0 comments on commit 472bf71

Please sign in to comment.