Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions JuiceMaker/JuiceMaker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
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 /* JuiceOrderViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C73DAF3A255D0CDD00020D38 /* JuiceOrderViewController.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 */; };
Expand All @@ -29,7 +29,7 @@
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 /* JuiceOrderViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JuiceOrderViewController.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 @@ -56,7 +56,7 @@
children = (
C73DAF36255D0CDD00020D38 /* AppDelegate.swift */,
C73DAF38255D0CDD00020D38 /* SceneDelegate.swift */,
C73DAF3A255D0CDD00020D38 /* ViewController.swift */,
C73DAF3A255D0CDD00020D38 /* JuiceOrderViewController.swift */,
);
path = Controller;
sourceTree = "<group>";
Expand Down Expand Up @@ -138,6 +138,14 @@
C73DAF2B255D0CDC00020D38 /* Project object */ = {
isa = PBXProject;
attributes = {
KnownAssetTags = (

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tag를 사용하셨군요! 멋있습니다 👍🏻

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

질문이 있습니다🙋
만약 프로젝트의 규모가 커지고, 협업을 하는 사람들이 늘어난다면

  1. tag를 사용하는 방식과
  2. IBAction 메소드 혹은 storyboard에서 action을 연결하는 방법

중에 어떤 것이 더 유지보수에서 편리할 것이라고 생각하는지, 왜 그렇게 생각하는지 수꿍과 바드의 의견이 궁금합니다.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아무래도 IBAction함수를 각각 선언해주면 코드만 보아도 이해를 할 수 있지만, tag나 currentTitle을 사용한다면, 다른 파일 내에서도 어떻게 tag나 currentTitle를 달았는지 확인을 해봐야 할 것 같습니다.
IBAction함수는 각각 선언해주기에 명확하지만, 코드가 길어지고, 여러개의 버튼에 하나의 IBAction이 사용이 된다면 동일한 동작이 아닌 이상, 사용이 어려울 것 같습니다.
대신 tag나 currentTitle은 사용하는 기준이 명확해 보이지 않을수는 있지만, 하나의 IBAction에 모아서 각각의 기능으로 분기할 수 있다는 장점이 있습니다.
이로 인해 stroryboard에서 action을 연결하는 방법이 더 유지보수에 용이할 것 같습니다

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

좋습니다👏
이 부분은 코드로 화면을 만드는 것과 스토리보드로 화면을 만드는 것도 연관이 있을 것 같네요.
물론 회사 by 회사이지만 협업을 할 때는 스토리보드를 사용하면 직접 화면을 확인해야하는 문제나, 각 view component들 사이의 constraint를 파악하는 것이 코드보다 어렵다는 단점들이 있긴합니다.

앞으로의 프로젝트에서 스토리보드를 통해서 협업을 해보는 경험과 또 코드로 화면을 구현하는 협업을 경험을 해보시면서 직접 느껴보시는 것을 추천드립니다! 물론 둘 중에 뭐가 더 좋다 라는 정답은 당연히 없고, 둘 가지 방법 모두 사용되는 방법이니 두 방법을 잘 사용할 줄 알아야합니다 🙏🏻

0,
1,
2,
3,
4,
5,
);
LastSwiftUpdateCheck = 1200;
LastUpgradeCheck = 1200;
TargetAttributes = {
Expand Down Expand Up @@ -188,7 +196,7 @@
D7CB1413281793F900B24611 /* FruitStoreError.swift in Sources */,
C71CD66B266C7ACB0038B9CB /* FruitStore.swift in Sources */,
D7FC4E4C2816F0120093A003 /* Juice.swift in Sources */,
C73DAF3B255D0CDD00020D38 /* ViewController.swift in Sources */,
C73DAF3B255D0CDD00020D38 /* JuiceOrderViewController.swift in Sources */,
C73DAF37255D0CDD00020D38 /* AppDelegate.swift in Sources */,
C73DAF39255D0CDD00020D38 /* SceneDelegate.swift in Sources */,
C73DAF4C255D0D0400020D38 /* JuiceMaker.swift in Sources */,
Expand Down
99 changes: 99 additions & 0 deletions JuiceMaker/JuiceMaker/Controller/JuiceOrderViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//
// JuiceMaker - JuiceOrderViewController.swift
// Created by 수꿍, 바드.
// Copyright © yagom academy. All rights reserved.
//

import UIKit

class JuiceOrderViewController: UIViewController {
private let juiceMaker = JuiceMaker()

@IBOutlet private weak var strawberryLabel: UILabel?
@IBOutlet private weak var bananaLabel: UILabel?
@IBOutlet private weak var pineappleLabel: UILabel?
@IBOutlet private weak var kiwiLabel: UILabel?
@IBOutlet private weak var mangoLabel: UILabel?

override func viewDidLoad() {
super.viewDidLoad()
updateFruitStock()
}

private func updateFruitStock() {
strawberryLabel?.text = String(juiceMaker.fruitStore.fruitWarehouse[.strawberry] ?? 0)
bananaLabel?.text = String(juiceMaker.fruitStore.fruitWarehouse[.banana] ?? 0)
pineappleLabel?.text = String(juiceMaker.fruitStore.fruitWarehouse[.pineapple] ?? 0)
kiwiLabel?.text = String(juiceMaker.fruitStore.fruitWarehouse[.kiwi] ?? 0)
mangoLabel?.text = String(juiceMaker.fruitStore.fruitWarehouse[.mango] ?? 0)
}

@IBAction private func makeJuice(_ sender: UIButton) {

var juice: Juice {
switch sender.currentTitle {
case "딸기쥬스 주문":
return .strawberry
case "바나나쥬스 주문":
return .banana
case "파인애플쥬스 주문":
return .pineapple
case "키위쥬스 주문":
return .kiwi
case "딸바쥬스 주문":
return .strawberryBanana
case "망고쥬스 주문":
return .mango
case "망키쥬스 주문":
return .mangoKiwi
default:
return .unknownFruit
}
}

tryMaking(juice)
updateFruitStock()
}

private func tryMaking(_ juice: Juice) {
do {
try juiceMaker.make(juice)
showSuccessAlert(juice)
} catch FruitStoreError.outOfStock {
showFailAlert()
} catch {
print(error.localizedDescription)
}
}

private func showSuccessAlert(_ juice: Juice) {
let successAlert = UIAlertController(title: "\(juice.name) 쥬스 나왔습니다!\n 맛있게 드세요!", message: nil, preferredStyle: .alert)

let ok = UIAlertAction(title: "확인", style: .default, handler: nil)

successAlert.addAction(ok)
present(successAlert, animated: true)
}

private func showFailAlert() {
let failAlert = UIAlertController(title: "재료가 모자라요.\n 재고를 수정할까요?", message: nil, preferredStyle: .alert)
let no = UIAlertAction(title: "아니오", style: .cancel)
let yes = UIAlertAction(title: "예", style: .default) { action in
self.goToFruitStockViewController()
}

failAlert.addAction(no)
failAlert.addAction(yes)
present(failAlert, animated: true)
}

@IBAction private func navigationRightButtonTapped(_ sender: UIBarButtonItem) {
goToFruitStockViewController()
}

private func goToFruitStockViewController() {
guard let controller = self.storyboard?.instantiateViewController(withIdentifier: "FruitStockViewController") else { return }

self.navigationController?.pushViewController(controller, animated: true)
}
}
18 changes: 0 additions & 18 deletions JuiceMaker/JuiceMaker/Controller/ViewController.swift

This file was deleted.

1 change: 1 addition & 0 deletions JuiceMaker/JuiceMaker/Model/CostOfJuiceProduction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ enum CostOfJuiceProduction {
static let mangoInMangoJuice = 3
static let mangoInMangoKiwiJuice = 2
static let kiwiInMangoKiwiJuice = 1
static let unknownFruitInUnknownFruitJuice = -1
}
1 change: 1 addition & 0 deletions JuiceMaker/JuiceMaker/Model/Fruit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ enum Fruit: String {
case pineapple
case kiwi
case mango
case unknownFruit
}
17 changes: 10 additions & 7 deletions JuiceMaker/JuiceMaker/Model/FruitStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
// Copyright © yagom academy. All rights reserved.
//

// 과일 저장소 타입
class FruitStore {
private var fruitWarehouse: Dictionary<Fruit, Int> = [
private(set) var fruitWarehouse: Dictionary<Fruit, Int> = [

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private(set) 을 사용하셨군요! 멋집니다 👍🏻
private과 어떤 차이가 있고 왜 변경하셨나요? 👀

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private은 읽고 쓰기가 가능하지만, private(set)은 쓰지 않고 외부에서 읽기만 가능하고 내부에서만 수정이 가능하도록 하는 코드입니다.
현 상황에서는 내부에서 수정이 필요없어 보여 변경해보았습니다🙃

Fruit.strawberry: InitialFruitStock.stock,
Fruit.banana: InitialFruitStock.stock,
Fruit.pineapple: InitialFruitStock.stock,
Expand Down Expand Up @@ -72,6 +71,8 @@ class FruitStore {
case Juice.mangoKiwi:
try function(Fruit.mango)
try function(Fruit.kiwi)
case Juice.unknownFruit:
try function(Fruit.unknownFruit)
}
}

Expand All @@ -93,19 +94,21 @@ class FruitStore {
case Juice.mangoKiwi:
try function(Fruit.mango, CostOfJuiceProduction.mangoInMangoKiwiJuice)
try function(Fruit.kiwi, CostOfJuiceProduction.kiwiInMangoKiwiJuice)
case Juice.unknownFruit:
try function(Fruit.unknownFruit, CostOfJuiceProduction.unknownFruitInUnknownFruitJuice)
}
}

func handle(_ error: Error) {
func handle(_ error: Error) -> String {
switch error {
case FruitStoreError.outOfStock:
print(FruitStoreError.outOfStock.localizedDescription)
return FruitStoreError.outOfStock.localizedDescription
case FruitStoreError.missingProduct:
print(FruitStoreError.missingProduct.localizedDescription)
return FruitStoreError.missingProduct.localizedDescription
case FruitStoreError.invalidAmount:
print(FruitStoreError.invalidAmount.localizedDescription)
return FruitStoreError.invalidAmount.localizedDescription
default:
print("알 수 없는 오류입니다")
return "알 수 없는 오류입니다"
}
}
}
1 change: 1 addition & 0 deletions JuiceMaker/JuiceMaker/Model/FruitStoreError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ extension FruitStoreError: LocalizedError {
case .invalidAmount:
return NSLocalizedString("잘못된 수량입니다", comment: "Description of invalid amount")
}

}
}
24 changes: 24 additions & 0 deletions JuiceMaker/JuiceMaker/Model/Juice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,28 @@ enum Juice {
case strawberryBanana
case mango
case mangoKiwi
case unknownFruit
}

extension Juice {
var name: String {
switch self {
case .strawberry:
return "딸기"
case .banana:
return "바나나"
case .pineapple:
return "파인애플"
case .kiwi:
return "키위"
case .strawberryBanana:
return "딸바"
case .mango:
return "망고"
case .mangoKiwi:
return "망키"
case .unknownFruit:
return ""
}
}
}
12 changes: 4 additions & 8 deletions JuiceMaker/JuiceMaker/Model/JuiceMaker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@
// Copyright © yagom academy. All rights reserved.
//

// 쥬스 메이커 타입
struct JuiceMaker {
let fruitStore = FruitStore()

func make(_ juice: Juice) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

쥬스메이커의 역할은 쥬스를 만들 수 있는지 검사만 하면 되는 걸까요? 🤔

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

쥬스를 만들수 있는지 검사하고 쥬스메이커가 쥬스를 만들어야 하기 때문에 위 코멘트에서 말한 것처럼 다시 함수를 넣어주었습니다!

do {
try checkFruitAvailable(for: juice)
try checkPossibilityOfMaking(juice)
} catch (let error) {
fruitStore.handle(error)
}
func make(_ juice: Juice) throws {
try checkFruitAvailable(for: juice)
try checkPossibilityOfMaking(juice)
}


private func checkFruitAvailable(for juice: Juice) throws {
try fruitStore.goToWarehouse(of: fruitStore.checkFruitKind(_:), juice: juice)
}
Expand Down
Loading