-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from jryoun1/step1-lina-develop
Step 1 : 타입 및 Console App 구현 (리나)
- Loading branch information
Showing
8 changed files
with
260 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// BankManager.swift | ||
// Created by yagom. | ||
// Copyright © yagom academy. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
final class Bank { | ||
private var clients: [Client] = [] | ||
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 | ||
assignBusinessToTeller() | ||
businessTime = Date().timeIntervalSince(openTime) | ||
Dashboard.printCloseMessage(finishedClientNumber, businessTime) | ||
closeBank() | ||
} | ||
|
||
private func initTellers(_ number: Int) { | ||
for windowNumber in 1...number { | ||
tellers.append(Teller(windowNumber: windowNumber)) | ||
} | ||
} | ||
|
||
private func assignBusinessToTeller() { | ||
let semaphore = DispatchSemaphore(value: 0) | ||
var isContinue = true | ||
|
||
while isContinue { | ||
for teller in self.tellers { | ||
if self.clients.count == 0 { | ||
isContinue = false | ||
break | ||
} | ||
if teller.isNotWorking { | ||
let client = clients.removeFirst() | ||
teller.workingQueue.async { | ||
teller.handleBusiness(for: client) | ||
semaphore.signal() | ||
} | ||
self.finishedClientNumber += 1 | ||
} | ||
} | ||
} | ||
for _ in 0..<finishedClientNumber { semaphore.wait() } | ||
} | ||
|
||
private func closeBank() { | ||
tellers.removeAll() | ||
clients.removeAll() | ||
finishedClientNumber = 0 | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// Client.swift | ||
// BankManagerConsoleApp | ||
// | ||
// Created by 임리나 on 2021/01/05. | ||
// | ||
|
||
import Foundation | ||
|
||
final class Client { | ||
let waitingNumber: Int | ||
let businessType: BusinessType | ||
|
||
init(waitingNumber: Int, businessType: BusinessType = .normal) { | ||
self.waitingNumber = waitingNumber | ||
self.businessType = businessType | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
BankManagerConsoleApp/BankManagerConsoleApp/Dashboard.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// Dashboard.swift | ||
// BankManagerConsoleApp | ||
// | ||
// Created by 임리나 on 2021/01/09. | ||
// | ||
|
||
import Foundation | ||
|
||
class Dashboard { | ||
static func printMenu() { | ||
print(Menu.description, terminator: " ") | ||
} | ||
|
||
static func printStatus(for client: Client, about message: Message) { | ||
let message = String(format: message.rawValue, client.waitingNumber) | ||
print(message) | ||
} | ||
|
||
static func printCloseMessage(_ number: Int, _ time: TimeInterval?) { | ||
guard let time = time else { | ||
print(BankError.unknown.description) | ||
return | ||
} | ||
let message = String(format: Message.close.rawValue, number, time) | ||
print(message) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// String.swift | ||
// BankManagerConsoleApp | ||
// | ||
// Created by 임리나 on 2021/01/05. | ||
// | ||
|
||
import Foundation | ||
|
||
enum Menu: String { | ||
case start = "1" | ||
case end = "2" | ||
|
||
static let description = """ | ||
1: 은행 개점 | ||
2: 종료 | ||
입력 : | ||
""" | ||
} | ||
|
||
enum Message: String { | ||
case close = "업무가 마감되었습니다. 오늘 업무를 처리한 고객은 총 %d명이며, 총 업무시간은 %.2f초입니다." | ||
case tellerStart = "%d번 고객 업무 시작" | ||
case tellerFinish = "%d번 고객 업무 완료" | ||
} | ||
|
||
enum BusinessType { | ||
case normal | ||
|
||
var neededTime: TimeInterval { | ||
switch self { | ||
case .normal: | ||
return 0.7 | ||
} | ||
} | ||
} | ||
|
||
enum BankError: Error { | ||
case wrongInput | ||
case unknown | ||
|
||
var description: String { | ||
switch self { | ||
case .wrongInput: | ||
return "잘못된 입력입니다. 다시 입력해주세요" | ||
case .unknown: | ||
return "알 수 없는 에러가 발생했습니다." | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// Teller.swift | ||
// BankManagerConsoleApp | ||
// | ||
// Created by 임리나 on 2021/01/05. | ||
// | ||
|
||
import Foundation | ||
|
||
final class Teller { | ||
private var windowNumber: Int | ||
var workingQueue: DispatchQueue | ||
private var isWorking: Bool = false | ||
var isNotWorking: Bool { | ||
return !isWorking | ||
} | ||
|
||
init(windowNumber: Int) { | ||
self.windowNumber = windowNumber | ||
workingQueue = DispatchQueue(label: "\(windowNumber)") | ||
} | ||
|
||
func handleBusiness(for client: Client) { | ||
let needTimeToWork = client.businessType.neededTime | ||
|
||
isWorking = true | ||
Dashboard.printStatus(for: client, about: .tellerStart) | ||
Thread.sleep(forTimeInterval: needTimeToWork) | ||
Dashboard.printStatus(for: client, about: .tellerFinish) | ||
isWorking = false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters