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

은행창구 관리앱 [ Step 4 ] 랄라, 범 #53

Open
wants to merge 6 commits into
base: 2_Rarla
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
50 changes: 20 additions & 30 deletions BankManager.swift
Original file line number Diff line number Diff line change
@@ -1,62 +1,52 @@
import Foundation

final class BankManager {
private let customerCount: Int
private let depositTellers: Tellers
private let loanTellers: Tellers
private var customerCount: Int = 0
private let depositCustomerQueue = Queue<Int>()
private let loanCustomerQueue = Queue<Int>()
private var startTime: Date?
var depositTellers: Tellers
var loanTellers: Tellers
weak var delegate: BankUIDelegate?


init(depositTellerCount: Int, loanTellerCount: Int) {
self.customerCount = Int.random(in: 10...30)
self.depositTellers = Tellers(tellerCount: depositTellerCount, tellerType: .Deposit)
self.loanTellers = Tellers(tellerCount: loanTellerCount, tellerType: .Loan)
}

func openBank() {
startTime = Date()
createCustomerQueue(customerCount: customerCount)
startTask()
}

func finishTask() {
let endTime = Date()
guard let startTime = startTime else { return }
let time = endTime.timeIntervalSince(startTime)
let totalSecond = String(format: "%.2f", time)
print("업무가 마감되었습니다. 오늘 업무를 처리한 고객은 총 \(customerCount)명이며, 총 업무시간은 \(totalSecond)초 입니다.")
}

private func createCustomerQueue(customerCount: Int) {
for n in 1...customerCount {
func createCustomerQueue() {
for n in 1...10 {
guard let work = TypeOfWork(rawValue: Int.random(in: 0...1)) else {
return
}

switch work {

case .Deposit:
depositCustomerQueue.enqueue(data: n)
depositCustomerQueue.enqueue(data: n + customerCount)
delegate?.addTaskLabel(type: .Deposit, number: n + customerCount, textColor: .black)
case .Loan:
loanCustomerQueue.enqueue(data: n)
loanCustomerQueue.enqueue(data: n + customerCount)
delegate?.addTaskLabel(type: .Loan, number: n + customerCount, textColor: .orange)
}
}
customerCount += 10
}

private func startTask() {
func startTask() {
let depositWork = (depositTellers, depositCustomerQueue)
let loanWork = (loanTellers, loanCustomerQueue)

let group = DispatchGroup()
[depositWork, loanWork].forEach { (tellers, queue) in
group.enter()
DispatchQueue.global().async {
DispatchQueue.global(qos: .utility ).async {
tellers.doTask(queue: queue)
group.leave()
}
}

group.wait()
}

func reset() {
customerCount = 0
depositCustomerQueue.clear()
loanCustomerQueue.clear()
}
}
37 changes: 17 additions & 20 deletions BankManagerConsoleApp/BankManagerConsoleApp/Tellers.swift
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
import Foundation


struct Tellers {
private let tellerCount: Int
private let tellerType: TypeOfWork
let tellerType: TypeOfWork
private let semaphore: DispatchSemaphore
weak var delegate: BankUIDelegate?

init(tellerCount: Int, tellerType: TypeOfWork) {
self.tellerCount = tellerCount
self.tellerType = tellerType
self.semaphore = DispatchSemaphore(value: tellerCount)
}

func doTask(queue: Queue<Int>) {
let name = tellerType.name
let time = tellerType.time

let semaphore = DispatchSemaphore(value: tellerCount)
let group = DispatchGroup()
var workingTellerCount = 0

while !queue.isEmpty() {

guard let data = queue.dequeue() else { return }

group.enter()
semaphore.wait()
guard let data = queue.dequeue() else {
DispatchQueue.global().async {
delegate?.changeToLabelState(tellerType: tellerType, data: data)

usleep(time)

delegate?.removeLabel(data: data)
semaphore.signal()
return
}

if workingTellerCount < tellerCount {
group.enter()
DispatchQueue.global().async {
workingTellerCount += 1
print("\(name) 은행원 \(data)번 고객 \(name)업무 시작")
usleep(time)
print("\(name) 은행원 \(data)번 고객 \(name)업무 완료")
workingTellerCount -= 1
semaphore.signal()
group.leave()
}
group.leave()
}
}
group.wait()
Expand Down
32 changes: 20 additions & 12 deletions BankManagerUIApp/BankManagerUIApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
objects = {

/* Begin PBXBuildFile section */
6B5F8FC42B03305F00D23FF8 /* Protocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B5F8FC32B03305F00D23FF8 /* Protocol.swift */; };
6BAA0FCD2B05B077007579A1 /* BankView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6BAA0FCC2B05B077007579A1 /* BankView.swift */; };
C7694E3B259C3E9F0053667F /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7694E3A259C3E9F0053667F /* AppDelegate.swift */; };
C7694E3D259C3E9F0053667F /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7694E3C259C3E9F0053667F /* SceneDelegate.swift */; };
C7694E3F259C3E9F0053667F /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7694E3E259C3E9F0053667F /* ViewController.swift */; };
C7694E42259C3E9F0053667F /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C7694E40259C3E9F0053667F /* Main.storyboard */; };
C7694E44259C3EA20053667F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = C7694E43259C3EA20053667F /* Assets.xcassets */; };
C7694E47259C3EA20053667F /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = C7694E45259C3EA20053667F /* LaunchScreen.storyboard */; };
C7694E52259C3EA20053667F /* BankManagerUIAppTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7694E51259C3EA20053667F /* BankManagerUIAppTests.swift */; };
C7694E5D259C3EA20053667F /* BankManagerUIAppUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7694E5C259C3EA20053667F /* BankManagerUIAppUITests.swift */; };
C7D65D1E259C81BD005510E0 /* BankManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7D65D1D259C81BD005510E0 /* BankManager.swift */; };
D0D1B85B2AFDEC140017D3A6 /* Queue.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0D1B85A2AFDEC140017D3A6 /* Queue.swift */; };
D0D1B85D2AFDEC600017D3A6 /* Tellers.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0D1B85C2AFDEC600017D3A6 /* Tellers.swift */; };
D0D1B85F2AFDEC670017D3A6 /* TypeOfWork.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0D1B85E2AFDEC670017D3A6 /* TypeOfWork.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -36,11 +40,12 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
6B5F8FC32B03305F00D23FF8 /* Protocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Protocol.swift; sourceTree = "<group>"; };
6BAA0FCC2B05B077007579A1 /* BankView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BankView.swift; sourceTree = "<group>"; };
C7694E37259C3E9F0053667F /* BankManagerUIApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BankManagerUIApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
C7694E3A259C3E9F0053667F /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
C7694E3C259C3E9F0053667F /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
C7694E3E259C3E9F0053667F /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
C7694E41259C3E9F0053667F /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
C7694E43259C3EA20053667F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
C7694E46259C3EA20053667F /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
C7694E48259C3EA20053667F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand All @@ -51,6 +56,9 @@
C7694E5C259C3EA20053667F /* BankManagerUIAppUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BankManagerUIAppUITests.swift; sourceTree = "<group>"; };
C7694E5E259C3EA20053667F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
C7D65D1D259C81BD005510E0 /* BankManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = BankManager.swift; path = ../../BankManager.swift; sourceTree = "<group>"; };
D0D1B85A2AFDEC140017D3A6 /* Queue.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Queue.swift; path = ../BankManagerConsoleApp/BankManagerConsoleApp/Queue.swift; sourceTree = "<group>"; };
D0D1B85C2AFDEC600017D3A6 /* Tellers.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Tellers.swift; path = ../BankManagerConsoleApp/BankManagerConsoleApp/Tellers.swift; sourceTree = "<group>"; };
D0D1B85E2AFDEC670017D3A6 /* TypeOfWork.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TypeOfWork.swift; path = ../BankManagerConsoleApp/BankManagerConsoleApp/TypeOfWork.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -81,6 +89,9 @@
C7694E2E259C3E9F0053667F = {
isa = PBXGroup;
children = (
D0D1B85E2AFDEC670017D3A6 /* TypeOfWork.swift */,
D0D1B85C2AFDEC600017D3A6 /* Tellers.swift */,
D0D1B85A2AFDEC140017D3A6 /* Queue.swift */,
C7694E39259C3E9F0053667F /* BankManagerUIApp */,
C7694E50259C3EA20053667F /* BankManagerUIAppTests */,
C7694E5B259C3EA20053667F /* BankManagerUIAppUITests */,
Expand All @@ -105,7 +116,8 @@
C7694E3A259C3E9F0053667F /* AppDelegate.swift */,
C7694E3C259C3E9F0053667F /* SceneDelegate.swift */,
C7694E3E259C3E9F0053667F /* ViewController.swift */,
C7694E40259C3E9F0053667F /* Main.storyboard */,
6BAA0FCC2B05B077007579A1 /* BankView.swift */,
6B5F8FC32B03305F00D23FF8 /* Protocol.swift */,
C7694E43259C3EA20053667F /* Assets.xcassets */,
C7694E45259C3EA20053667F /* LaunchScreen.storyboard */,
C7694E48259C3EA20053667F /* Info.plist */,
Expand Down Expand Up @@ -236,7 +248,6 @@
files = (
C7694E47259C3EA20053667F /* LaunchScreen.storyboard in Resources */,
C7694E44259C3EA20053667F /* Assets.xcassets in Resources */,
C7694E42259C3E9F0053667F /* Main.storyboard in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -261,10 +272,15 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D0D1B85B2AFDEC140017D3A6 /* Queue.swift in Sources */,
C7694E3F259C3E9F0053667F /* ViewController.swift in Sources */,
C7694E3B259C3E9F0053667F /* AppDelegate.swift in Sources */,
6B5F8FC42B03305F00D23FF8 /* Protocol.swift in Sources */,
D0D1B85F2AFDEC670017D3A6 /* TypeOfWork.swift in Sources */,
C7694E3D259C3E9F0053667F /* SceneDelegate.swift in Sources */,
6BAA0FCD2B05B077007579A1 /* BankView.swift in Sources */,
C7D65D1E259C81BD005510E0 /* BankManager.swift in Sources */,
D0D1B85D2AFDEC600017D3A6 /* Tellers.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -300,14 +316,6 @@
/* End PBXTargetDependency section */

/* Begin PBXVariantGroup section */
C7694E40259C3E9F0053667F /* Main.storyboard */ = {
isa = PBXVariantGroup;
children = (
C7694E41259C3E9F0053667F /* Base */,
);
name = Main.storyboard;
sourceTree = "<group>";
};
C7694E45259C3EA20053667F /* LaunchScreen.storyboard */ = {
isa = PBXVariantGroup;
children = (
Expand Down
Loading