Skip to content
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

은행창구 매니저 [Step3] 토이, 쥬봉이 #330

Open
wants to merge 9 commits into
base: ic_10_toy123
Choose a base branch
from
25 changes: 22 additions & 3 deletions BankManager/Sources/BankManager/Model/BankManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,28 @@ public struct BankManager<BankClerk: CustomerReceivable> {
customerQueue.enqueue(customer)
}

public func assignCustomer(to bankClerk: BankClerk) {
while let customer = customerQueue.dequeue() as? BankClerk.Customer {
bankClerk.receive(customer: customer)
public func assignCustomer(to bankClerk: [Banking: BankClerk]) {
let group = DispatchGroup()
let semaphore = DispatchSemaphore(value: 2)
let depositQueue = DispatchQueue(label: "depositQueue", attributes: .concurrent)
let loanQeueue = DispatchQueue(label: "loanQueue")

while let customer = customerQueue.dequeue() as? BankClerk.Customer,
let banking = customer.banking {
switch banking {
case .deposit:
depositQueue.async(group: group) {
semaphore.wait()
bankClerk[banking]?.receive(customer: customer)
semaphore.signal()
}
case .loan:
loanQeueue.async(group: group) {
bankClerk[banking]?.receive(customer: customer)
}
}
Comment on lines +12 to +31

Choose a reason for hiding this comment

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

한번에 최대 실행될 수 있는 작업의 수는 3개인데, 생성된 쓰레드의 수는 3개보다 훨씬 많네요.
image

총 3개의 쓰레드만 생성하고싶다면 어떻게 해야할까요?

}

group.wait()
}
}
11 changes: 11 additions & 0 deletions BankManager/Sources/BankManager/Model/Banking.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// Banking.swift
//
//
// Created by jyubong, Toy on 11/22/23.
//

public enum Banking: String, CaseIterable {
case deposit = "예금"
case loan = "대출"
}
Comment on lines +8 to +11

Choose a reason for hiding this comment

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

CustomStringConvertible이라는 프로토콜을 사용해도 좋을 것 같아요.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
public protocol CustomerNumbering {
var number: UInt { get }
var banking: Banking? { get }

Choose a reason for hiding this comment

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

굳이 옵셔널일 필요가 있을까요? randomElement()가 옵셔널을 반환한다면, 기본값을 지정해줘도 좋을 것 같아요.

}
4 changes: 2 additions & 2 deletions BankManagerConsoleApp/BankManagerConsoleApp/Model/Bank.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import BankManager

struct Bank: BankBusinesable {
private let bankManager = BankManager<BankClerk>()
private let bankClerk = BankClerk()
private let bankClerks: [Banking: BankClerk] = [.deposit: BankClerk(work: .deposit), .loan: BankClerk(work: .loan)]
private let customerNumber: UInt

init(customerNumber: UInt) {
Expand All @@ -22,7 +22,7 @@ struct Bank: BankBusinesable {

let startTime = CFAbsoluteTimeGetCurrent()

bankManager.assignCustomer(to: bankClerk)
bankManager.assignCustomer(to: bankClerks)

let finishTime = CFAbsoluteTimeGetCurrent() - startTime
let time = String(format: "%.2f", finishTime)
Expand Down
17 changes: 13 additions & 4 deletions BankManagerConsoleApp/BankManagerConsoleApp/Model/BankClerk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,29 @@ import Foundation
import BankManager

struct BankClerk: CustomerReceivable {
private let pace = 0.7
private let work: Banking
private var pace: Double {
switch work {
case .deposit:
return 0.7
case .loan:
return 1.1
}
}
Comment on lines +11 to +19

Choose a reason for hiding this comment

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

저는 이 정보가 은행원보다는 업무에 들어가야한다고 생각해요. 현재 은행원에 따라 속도차이가 나는게 아니라, 업무에 따라 속도 차이가 나기 때문이에요.

private let startWork: (String) -> Void
private let endWork: (String) -> Void

init(startWork: @escaping (String) -> Void = { print($0) }, endWork: @escaping (String) -> Void = { print($0) }) {
init(work: Banking, startWork: @escaping (String) -> Void = { print($0) }, endWork: @escaping (String) -> Void = { print($0) }) {
self.work = work
self.startWork = startWork
self.endWork = endWork
}

func receive(customer: Customer) {
let number = customer.number

startWork("\(number)번 고객 업무 시작")
startWork("\(number)번 고객 \(work.rawValue)업무 시작")
Thread.sleep(forTimeInterval: pace)
endWork("\(number)번 고객 업무 종료")
endWork("\(number)번 고객 \(work.rawValue)업무 종료")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ import BankManager

struct Customer: CustomerNumbering {
let number: UInt
let banking = Banking.allCases.randomElement()
}
74 changes: 71 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,74 @@
## iOS 커리어 스타터 캠프
# 💰 은행창구 매니저
은행에서 여러명의 은행원이 동시에 고객의 업무를 나누어 처리하는 앱

### 은행 매니저 프로젝트 저장소
---
## 🔎 목차
- [팀원](#-팀원)
- [타임라인](#-타임라인)
- [UML](#-UML)
- [실행 화면](#-실행-화면)
- [트러블 슈팅](#-트러블-슈팅)
- [참고 링크](#-참고-링크)

- 이 저장소를 자신의 저장소로 fork하여 프로젝트를 진행합니다
---
## 👥 팀원
|쥬봉이🐱|토이🐶|
|---|---|
|<img src="https://avatars.githubusercontent.com/u/126065608?v=4" width="200" height="200">|<img src="https://avatars.githubusercontent.com/u/123448121?v=4" width="200" height="200">|
|[쥬봉 GitHub](https://github.com/jyubong)|[토이 GitHub](https://github.com/DevWooHyeon)|


## 📅 타임라인
|날짜|내용|
|------|---|
|23.11.13|- 공식문서 등 각자 공부|
|23.11.14|- Queue, LinkedList 구현 <br> - Queue UnitTest|
|23.11.15|- SPM <br> - SPM내 Protocol 및 BankManager 구현 <br> - Console App내 protocol을 채택한 실제 타입 구현|
|23.11.16|- ConsoleManager 구현|
|23.11.17|- 코드 리팩토링 <br> - BankClerk을 test에 용이하도록 수정(클로저 주입)|
|23.11.20 ~ 23.11.21|- concurrency programming 공부(GCD, Operation 등)|
|23.11.22|- GCD로 3명의 은행원이 비동기적으로 업무를 처리하도록 구현 |
|23.11.23|- 은행원수만큼 스레드 제한하는 방법, DispatchQueue와 OperationQueue 비교 등 피드백 받은 부분 공부|
|23.11.24|- ReadMe 작성 |

## 👀 UML


## 💻 실행 화면
| 첫번째 은행개점 | 두번째 은행개점 + 종료 |
| -------- | -------- |
| ![gcd1](https://hackmd.io/_uploads/BkB-_2TN6.gif) | ![gcd2](https://hackmd.io/_uploads/r1imu2pNa.gif) |


## 🔥 트러블 슈팅



## 📚 참고 링크
[애플공식문서 - Protocols](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/protocols/)
[애플공식문서 - Generics](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/generics)
[애플공식문서 - Dispatch](https://developer.apple.com/documentation/DISPATCH)
[애플공식문서 - Operation](https://developer.apple.com/documentation/foundation/operation)
[애플공식문서 - OperationQueue](https://developer.apple.com/documentation/foundation/operationqueue)
[야곰닷넷 Concurrency](https://yagom.net/courses/%eb%8f%99%ec%8b%9c%ec%84%b1-%ed%94%84%eb%a1%9c%ea%b7%b8%eb%9e%98%eb%b0%8d-concurrency-programming/)


---
### 팀 회고
<details>
<summary>우리팀이 잘한 점</summary>


</details>

<details>
<summary>우리팀이 개선할 점</summary>


</details>

<details>
<summary>서로에게 피드백</summary>


</details>