-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/develop' into develop
- Loading branch information
Showing
64 changed files
with
2,059 additions
and
223 deletions.
There are no files selected for viewing
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
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
47 changes: 47 additions & 0 deletions
47
BankManagerConsoleApp/BankManagerConsoleApp/App/BankManagerApp.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,47 @@ | ||
// | ||
// BankManagerApp.swift | ||
// BankManagerConsoleApp | ||
// | ||
// Created by Effie on 2/1/24. | ||
// | ||
|
||
final class BankManagerApp { | ||
func start() { | ||
startBank() | ||
} | ||
} | ||
|
||
private extension BankManagerApp { | ||
func startBank() { | ||
let tasks: [BankTask: ClientQueueManagable] = [ | ||
.loan: ClientManager(), | ||
.deposit: ClientManager(), | ||
] | ||
|
||
let orders: [BankTask: Int] = [.loan: 2, .deposit: 3] | ||
let bankers = makeBankers( | ||
tasks: tasks, | ||
orders: orders | ||
) | ||
|
||
BankManager( | ||
bankers: bankers, | ||
clientManager: tasks | ||
).start() | ||
} | ||
|
||
func makeBankers( | ||
tasks: [BankTask: any ClientQueueManagable], | ||
orders: [BankTask: Int] | ||
) -> [Banker] { | ||
var result = [Banker]() | ||
for (type, bankerCount) in orders { | ||
(1...bankerCount).forEach { _ in | ||
guard let clientManager = tasks[type] else { return } | ||
let banker = Banker(clientManager: clientManager) | ||
result.append(banker) | ||
} | ||
} | ||
return result | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
BankManagerConsoleApp/BankManagerConsoleApp/App/BankManagerAppError.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,10 @@ | ||
// | ||
// BankManagerAppError.swift | ||
// BankManagerConsoleApp | ||
// | ||
// Created by Effie on 2/2/24. | ||
// | ||
|
||
enum BankManagerAppError: Error { | ||
case outOfIndex | ||
} |
55 changes: 55 additions & 0 deletions
55
BankManagerConsoleApp/BankManagerConsoleApp/App/BankManagerAppMenu.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,55 @@ | ||
// | ||
// BankManagerAppMenu.swift | ||
// BankManagerConsoleApp | ||
// | ||
// Created by 강창현 on 2/1/24. | ||
// | ||
|
||
enum BankManagerAppMenu { | ||
case open | ||
case end | ||
|
||
private var menuNumebr: Int { | ||
switch self { | ||
case .open: return 1 | ||
case .end: return 2 | ||
} | ||
} | ||
|
||
private var description: String { | ||
switch self { | ||
case .open: return "은행 개점" | ||
case .end: return "종료" | ||
} | ||
} | ||
|
||
private init?(number: Int) { | ||
switch number { | ||
case Self.open.menuNumebr: self = .open | ||
case Self.end.menuNumebr: self = .end | ||
default: return nil | ||
} | ||
} | ||
|
||
init(input: String) throws { | ||
let trimmedInput = input.trimmingCharacters(in: .whitespacesAndNewlines) | ||
guard | ||
trimmedInput.isEmpty == false, | ||
let number = Int(input), | ||
let menu = Self.init(number: number) | ||
else { | ||
throw IOError.invalidInput | ||
} | ||
self = menu | ||
} | ||
} | ||
|
||
extension BankManagerAppMenu: CaseIterable { | ||
static var allMenusPrompt: String { | ||
return allCases.enumerated().reduce(into: "") { result, indexAndMenu in | ||
let adding = "\(indexAndMenu.element.menuNumebr) : \(indexAndMenu.element.description)" | ||
let terminator = (indexAndMenu.offset + 1 == allCases.count) ? "" : "\n" | ||
result += adding + terminator | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
BankManagerConsoleApp/BankManagerConsoleApp/Model/BankManager.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,69 @@ | ||
// | ||
// BankManager.swift | ||
// Created by yagom. | ||
// Copyright © yagom academy. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct BankManager { | ||
private let textOut: TextOutputDisplayable | ||
|
||
private let dispenser: TicketDispenser | ||
|
||
init( | ||
textOut: TextOutputDisplayable, | ||
dispenser: TicketDispenser | ||
) { | ||
self.textOut = textOut | ||
self.dispenser = dispenser | ||
} | ||
} | ||
|
||
extension BankManager: BankRunnable { | ||
func runBank(with orders: [Order], numberOfClient: Int) { | ||
let group = DispatchGroup() | ||
let totalWorkTime = measure { | ||
for order in orders { | ||
let taskManager = TaskManager() | ||
makeClients(order: order, taskManager: taskManager) | ||
makeBankers(order: order, taskManager: taskManager) | ||
taskManager.startTaskManaging(group: group) | ||
} | ||
group.wait() | ||
} | ||
|
||
summarizeDailyStatistics( | ||
totalWorkTime: totalWorkTime, | ||
numberOfClient: numberOfClient | ||
) | ||
} | ||
} | ||
|
||
private extension BankManager { | ||
func makeBankers(order: Order, taskManager: TaskManager) { | ||
(1...order.bankerCount).forEach { _ in | ||
let banker = Banker(bankerEnqueuable: taskManager, resultOut: self.textOut) | ||
taskManager.enqueueBanker(banker) | ||
} | ||
} | ||
|
||
func makeClients(order: Order, taskManager: TaskManager) { | ||
while let number = self.dispenser.provideTicket(of: order.taskType) { | ||
let client = Client(number: number, task: order.taskType) | ||
taskManager.enqueueClient(client) | ||
} | ||
} | ||
|
||
func measure(_ progress: () -> Void) -> TimeInterval { | ||
let start = Date() | ||
progress() | ||
return Date().timeIntervalSince(start) | ||
} | ||
|
||
func summarizeDailyStatistics(totalWorkTime: Double, numberOfClient: Int) { | ||
let roundedWorkTimeString = String(format: "%.2f", totalWorkTime) | ||
let output = "업무가 마감되었습니다. 오늘 업무를 처리한 고객은 총 \(numberOfClient)명이며, 총 업무시간은 \(roundedWorkTimeString)초입니다." | ||
self.textOut.display(output: output) | ||
} | ||
} |
Oops, something went wrong.