Skip to content

Commit

Permalink
Merge pull request #4 from jryoun1/step1-lina-develop
Browse files Browse the repository at this point in the history
Step 1 : 타입 및 Console App 구현 (리나)
  • Loading branch information
O-O-wl committed Jan 9, 2021
2 parents bba83a0 + 08b2219 commit 7e7242f
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 12 deletions.
60 changes: 60 additions & 0 deletions Bank.swift
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
}
}
7 changes: 0 additions & 7 deletions BankManager.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
objects = {

/* Begin PBXBuildFile section */
4CB8E9C425A99CE000C021F6 /* Dashboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CB8E9C325A99CE000C021F6 /* Dashboard.swift */; };
4CDDB11825A422B700EDC9CE /* Teller.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CDDB11725A422B700EDC9CE /* Teller.swift */; };
4CDDB11B25A422CD00EDC9CE /* Client.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CDDB11A25A422CD00EDC9CE /* Client.swift */; };
4CDDB11E25A422EF00EDC9CE /* Enum.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4CDDB11D25A422EF00EDC9CE /* Enum.swift */; };
C7694E7A259C3EC00053667F /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7694E79259C3EC00053667F /* main.swift */; };
C7D65D1B259C8190005510E0 /* BankManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7D65D1A259C8190005510E0 /* BankManager.swift */; };
C7D65D1B259C8190005510E0 /* Bank.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7D65D1A259C8190005510E0 /* Bank.swift */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand All @@ -24,9 +28,13 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
4CB8E9C325A99CE000C021F6 /* Dashboard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Dashboard.swift; sourceTree = "<group>"; };
4CDDB11725A422B700EDC9CE /* Teller.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Teller.swift; sourceTree = "<group>"; };
4CDDB11A25A422CD00EDC9CE /* Client.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Client.swift; sourceTree = "<group>"; };
4CDDB11D25A422EF00EDC9CE /* Enum.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Enum.swift; sourceTree = "<group>"; };
C7694E76259C3EC00053667F /* BankManagerConsoleApp */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = BankManagerConsoleApp; sourceTree = BUILT_PRODUCTS_DIR; };
C7694E79259C3EC00053667F /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
C7D65D1A259C8190005510E0 /* BankManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = BankManager.swift; path = ../../BankManager.swift; sourceTree = "<group>"; };
C7D65D1A259C8190005510E0 /* Bank.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Bank.swift; path = ../../Bank.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -59,7 +67,11 @@
C7694E78259C3EC00053667F /* BankManagerConsoleApp */ = {
isa = PBXGroup;
children = (
C7D65D1A259C8190005510E0 /* BankManager.swift */,
4CB8E9C325A99CE000C021F6 /* Dashboard.swift */,
C7D65D1A259C8190005510E0 /* Bank.swift */,
4CDDB11725A422B700EDC9CE /* Teller.swift */,
4CDDB11A25A422CD00EDC9CE /* Client.swift */,
4CDDB11D25A422EF00EDC9CE /* Enum.swift */,
C7694E79259C3EC00053667F /* main.swift */,
);
path = BankManagerConsoleApp;
Expand Down Expand Up @@ -92,7 +104,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 1210;
LastUpgradeCheck = 1210;
LastUpgradeCheck = 1220;
TargetAttributes = {
C7694E75259C3EC00053667F = {
CreatedOnToolsVersion = 12.1;
Expand Down Expand Up @@ -123,7 +135,11 @@
buildActionMask = 2147483647;
files = (
C7694E7A259C3EC00053667F /* main.swift in Sources */,
C7D65D1B259C8190005510E0 /* BankManager.swift in Sources */,
C7D65D1B259C8190005510E0 /* Bank.swift in Sources */,
4CDDB11E25A422EF00EDC9CE /* Enum.swift in Sources */,
4CDDB11B25A422CD00EDC9CE /* Client.swift in Sources */,
4CB8E9C425A99CE000C021F6 /* Dashboard.swift in Sources */,
4CDDB11825A422B700EDC9CE /* Teller.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -248,6 +264,7 @@
C7694E7E259C3EC00053667F /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand All @@ -257,6 +274,7 @@
C7694E7F259C3EC00053667F /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 5.0;
Expand Down
18 changes: 18 additions & 0 deletions BankManagerConsoleApp/BankManagerConsoleApp/Client.swift
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 BankManagerConsoleApp/BankManagerConsoleApp/Dashboard.swift
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)
}
}
50 changes: 50 additions & 0 deletions BankManagerConsoleApp/BankManagerConsoleApp/Enum.swift
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 "알 수 없는 에러가 발생했습니다."
}
}
}
32 changes: 32 additions & 0 deletions BankManagerConsoleApp/BankManagerConsoleApp/Teller.swift
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
}
}
49 changes: 49 additions & 0 deletions BankManagerConsoleApp/BankManagerConsoleApp/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,52 @@
//

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() {
let bank = Bank()
let tellerNumber = 3
let maxClientNumber = 30
let minClientNumber = 10
var isContinue = true

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

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

while isContinue {
Dashboard.printMenu()

guard let input = readLine(), let command = Menu(rawValue: input) else {
print(BankError.wrongInput.description)
continue
}

switch command {
case .start:
let clients = initClients(randomNumber(from: minClientNumber, to: maxClientNumber))
bank.operateBank(teller: tellerNumber, client: clients)
case .end:
isContinue = false
}
}
}

main()

0 comments on commit 7e7242f

Please sign in to comment.