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 2 : 우선순위와 다중처리 (리나) #15

Merged
merged 19 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
19 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
13 changes: 5 additions & 8 deletions Bank.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ final class Bank {
private var tellers: [Teller] = []
private var finishedClientNumber = 0
private var businessTime: TimeInterval?

func operateBank(teller: Int, client: [Client]) {
let openTime = Date()

initTellers(teller)
clients = client
clients = client.sorted()
assignBusinessToTeller()
businessTime = Date().timeIntervalSince(openTime)
Dashboard.printCloseMessage(finishedClientNumber, businessTime)
Expand All @@ -30,7 +30,7 @@ final class Bank {
}

private func assignBusinessToTeller() {
let semaphore = DispatchSemaphore(value: 0)
let dispatchGroup = DispatchGroup()
var isContinue = true

while isContinue {
Expand All @@ -41,15 +41,12 @@ final class Bank {
}
if teller.isNotWorking {
let client = clients.removeFirst()
teller.workingQueue.async {
teller.handleBusiness(for: client)
semaphore.signal()
}
teller.handleBusiness(for: client, withDispatchGroup: dispatchGroup)
self.finishedClientNumber += 1
}
}
}
for _ in 0..<finishedClientNumber { semaphore.wait() }
dispatchGroup.wait()
}

private func closeBank() {
Expand Down
31 changes: 30 additions & 1 deletion BankManagerConsoleApp/BankManagerConsoleApp/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,40 @@
import Foundation

final class Client {
enum Priority: Comparable, CaseIterable {
case VVIP
case VIP
case normal
Copy link
Member

Choose a reason for hiding this comment

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

case 이름을 일반으로 하지 않은 이유가 있나요?


var description: String {
switch self {
case .VVIP:
return "VVIP"
case .VIP:
return "VIP"
case .normal:
return "일반"
}
}
}

let waitingNumber: Int
let businessType: BusinessType
let priority: Priority

init(waitingNumber: Int, businessType: BusinessType = .normal) {
init(waitingNumber: Int, businessType: BusinessType = .deposit, priority: Priority = .normal) {
self.waitingNumber = waitingNumber
self.businessType = businessType
self.priority = priority
}
}

extension Client: Comparable {
static func < (lhs: Client, rhs: Client) -> Bool {
return lhs.priority < rhs.priority
}

static func == (lhs: Client, rhs: Client) -> Bool {
return lhs.priority == rhs.priority
}
}
8 changes: 4 additions & 4 deletions BankManagerConsoleApp/BankManagerConsoleApp/Dashboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import Foundation

class Dashboard {
struct Dashboard {
Copy link
Member

Choose a reason for hiding this comment

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

구조체로 바꾼 이유는 무엇인가요?

static func printMenu() {
print(Menu.description, terminator: " ")
}

static func printStatus(for client: Client, about message: Message) {
let message = String(format: message.rawValue, client.waitingNumber)
static func printStatus(for client: Client, about message: String) {
let message = String(format: message, client.waitingNumber, client.priority.description, client.businessType.description)
print(message)
}

Expand All @@ -22,7 +22,7 @@ class Dashboard {
print(BankError.unknown.description)
return
}
let message = String(format: Message.close.rawValue, number, time)
let message = String(format: Message.close, number, time)
print(message)
}
}
26 changes: 19 additions & 7 deletions BankManagerConsoleApp/BankManagerConsoleApp/Enum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,31 @@ enum Menu: String {
"""
}

enum Message: String {
case close = "업무가 마감되었습니다. 오늘 업무를 처리한 고객은 총 %d명이며, 총 업무시간은 %.2f초입니다."
case tellerStart = "%d번 고객 업무 시작"
case tellerFinish = "%d번 고객 업무 완료"
enum Message {
static let close = "업무가 마감되었습니다. 오늘 업무를 처리한 고객은 총 %d명이며, 총 업무시간은 %.2f초입니다."
static let tellerStart = "%d번 %@고객 %@업무 시작"
static let tellerFinish = "%d번 %@고객 %@업무 완료"
}
Comment on lines -21 to 25
Copy link
Member

Choose a reason for hiding this comment

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

case 을 static let 으로 바꾼 이유는 무엇인가요?


enum BusinessType {
case normal
enum BusinessType: CaseIterable {
case deposit
case loan

var neededTime: TimeInterval {
switch self {
case .normal:
case .deposit:
return 0.7
case .loan:
return 1.1
}
}

var description: String {
switch self {
case .deposit:
return "예금"
case .loan:
return "대출"
}
}
}
Expand Down
15 changes: 8 additions & 7 deletions BankManagerConsoleApp/BankManagerConsoleApp/Teller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ final class Teller {
workingQueue = DispatchQueue(label: "\(windowNumber)")
}

func handleBusiness(for client: Client) {
let needTimeToWork = client.businessType.neededTime

func handleBusiness(for client: Client, withDispatchGroup group: DispatchGroup) {
isWorking = true
Dashboard.printStatus(for: client, about: .tellerStart)
Thread.sleep(forTimeInterval: needTimeToWork)
Dashboard.printStatus(for: client, about: .tellerFinish)
isWorking = false

workingQueue.async(group: group) {
Dashboard.printStatus(for: client, about: Message.tellerStart)
Thread.sleep(forTimeInterval: client.businessType.neededTime)
Dashboard.printStatus(for: client, about: Message.tellerFinish)
self.isWorking = false
}
}
}
43 changes: 20 additions & 23 deletions BankManagerConsoleApp/BankManagerConsoleApp/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,13 @@

import Foundation

private func randomNumber(from minNumber: Int = 0, to maxNumber: Int) -> Int {
return Int.random(in: minNumber...maxNumber)
}

private func initClients(_ number: Int) -> [Client]? {
var clients: [Client] = []

for waitingNumber in 1...number {
clients.append(Client(waitingNumber: waitingNumber))
}
return clients
}

private func main() {
lina0322 marked this conversation as resolved.
Show resolved Hide resolved
let bank = Bank()
let tellerNumber = 3
let maxClientNumber = 30
let minClientNumber = 10
var isContinue = true
lina0322 marked this conversation as resolved.
Show resolved Hide resolved

func initClients(_ number: Int) -> [Client] {
var clients: [Client] = []

for waitingNumber in 1...number {
clients.append(Client(waitingNumber: waitingNumber))
}
return clients
}

while isContinue {
Dashboard.printMenu()

Expand All @@ -45,12 +23,31 @@ private func main() {

switch command {
case .start:
let clients = initClients(randomNumber(from: minClientNumber, to: maxClientNumber))
guard let clients = initClients(randomNumber(from: minClientNumber, to: maxClientNumber)) else {
lina0322 marked this conversation as resolved.
Show resolved Hide resolved
return
}
bank.operateBank(teller: tellerNumber, client: clients)
case .end:
isContinue = false
}
}
}

private func randomNumber(from minNumber: Int = 0, to maxNumber: Int) -> Int {
lina0322 marked this conversation as resolved.
Show resolved Hide resolved
return Int.random(in: minNumber...maxNumber)
}

private func initClients(_ number: Int) -> [Client]? {
var clients: [Client] = []

for waitingNumber in 1...number {
guard let businessType = BusinessType.allCases.randomElement(), let priority = Client.Priority.allCases.randomElement() else {
return nil
}

clients.append(Client(waitingNumber: waitingNumber, businessType: businessType, priority: priority))
}
return clients
}
Copy link
Member

Choose a reason for hiding this comment

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

메인에서 메소드를 저장하는 것보다 좋은 방법이 있을 것 같아요.
혹시 [Client]를 저장하는 Clients라는 객체를 만들어볼 수 있을까요?


main()