From 3d3ade00b318a73ea3634aa7369692eda8d4fde6 Mon Sep 17 00:00:00 2001 From: hemil0102 Date: Mon, 18 Mar 2024 18:29:41 +0900 Subject: [PATCH] =?UTF-8?q?STEP3=20=EC=B5=9C=EC=A2=85=20PR?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .DS_Store | Bin 0 -> 10244 bytes BankManager.swift | 7 - BankManagerConsoleApp/.DS_Store | Bin 0 -> 10244 bytes .../project.pbxproj | 235 +++++++++++++++++- .../BankManagerConsoleAppTests.xcscheme | 74 ++++++ .../BankManagerConsoleApp/main.swift | 3 +- .../BankManagerConsoleAppTests/.DS_Store | Bin 0 -> 6148 bytes .../BankManagerLinkedListTests.swift | 51 ++++ .../BankManagerQueueTests.swift | 176 +++++++++++++ BankManagerConsoleApp/Controller/.DS_Store | Bin 0 -> 6148 bytes .../Controller/BankManager.swift | 21 ++ .../Controller/ConsoleManager.swift | 72 ++++++ .../Controller/CustomerManager.swift | 39 +++ .../Enum/BankingService.swift | 12 + BankManagerConsoleApp/Enum/InputError.swift | 13 + BankManagerConsoleApp/Model/Customer.swift | 14 ++ .../Model/CustomerNumber.swift | 5 + BankManagerConsoleApp/Model/Employee.swift | 53 ++++ BankManagerConsoleApp/Queue/LinkedList.swift | 64 +++++ BankManagerConsoleApp/Queue/Node.swift | 18 ++ BankManagerConsoleApp/Queue/Queue.swift | 25 ++ BankManagerUIApp/.DS_Store | Bin 0 -> 8196 bytes BankManagerUIApp/BankManagerUIApp/.DS_Store | Bin 0 -> 6148 bytes .../BankManagerUIApp/BankManager.swift | 8 + .../Base.lproj/Main.storyboard | 32 --- 25 files changed, 877 insertions(+), 45 deletions(-) create mode 100644 .DS_Store delete mode 100644 BankManager.swift create mode 100644 BankManagerConsoleApp/.DS_Store create mode 100644 BankManagerConsoleApp/BankManagerConsoleApp.xcodeproj/xcshareddata/xcschemes/BankManagerConsoleAppTests.xcscheme create mode 100644 BankManagerConsoleApp/BankManagerConsoleAppTests/.DS_Store create mode 100644 BankManagerConsoleApp/BankManagerConsoleAppTests/BankManagerLinkedListTests.swift create mode 100644 BankManagerConsoleApp/BankManagerConsoleAppTests/BankManagerQueueTests.swift create mode 100644 BankManagerConsoleApp/Controller/.DS_Store create mode 100644 BankManagerConsoleApp/Controller/BankManager.swift create mode 100644 BankManagerConsoleApp/Controller/ConsoleManager.swift create mode 100644 BankManagerConsoleApp/Controller/CustomerManager.swift create mode 100644 BankManagerConsoleApp/Enum/BankingService.swift create mode 100644 BankManagerConsoleApp/Enum/InputError.swift create mode 100644 BankManagerConsoleApp/Model/Customer.swift create mode 100644 BankManagerConsoleApp/Model/CustomerNumber.swift create mode 100644 BankManagerConsoleApp/Model/Employee.swift create mode 100644 BankManagerConsoleApp/Queue/LinkedList.swift create mode 100644 BankManagerConsoleApp/Queue/Node.swift create mode 100644 BankManagerConsoleApp/Queue/Queue.swift create mode 100644 BankManagerUIApp/.DS_Store create mode 100644 BankManagerUIApp/BankManagerUIApp/.DS_Store create mode 100644 BankManagerUIApp/BankManagerUIApp/BankManager.swift delete mode 100644 BankManagerUIApp/BankManagerUIApp/Base.lproj/Main.storyboard diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..274b90ff2d0d772f8017003414a80c1ab7555c4a GIT binary patch literal 10244 zcmeHMTWl0n7(V~B(3yeKX=xV=UD#MrsLN%yg%%5ByKSN6R>QWm6m*^48QZb5Gt18G z79_-~_#_$=Z_z|zygc|IiGn5&Zx5QF4?Y-T#GpPH@xl9>`lA0iXBOH5sS8H?FejPw z@A=R9&*hu%pL71Rgb;}5wFn_0LWqbDmCAg~u99dyeXdA?r%_M>+7q&qsKml&gy>ha z_7y$?J_0@hJ_0@hJ_54=0o=1`66bO0=RN{H0zLwl5TM_O5I$7KJUPK7`*dK&LI9NI zsAdU&({O;=gghDZ9E7s+Iddy2q>6bJcO^#c@r0|oc_*&s_R&(mZJVj$Z#A{Y(8rTXZKrLys$uCl zW?*PKHrYRDSeiW^xAT_fFxv#LkR(w`D)omCM_a<7NTg-mk#Oj6ql)Xz?@o@H8Owf@uM)g31*x32>SLuXS##263YS28DFG>SB$7zmnJgPzIM7|9wNvQi zI19fQasI-?S(M~?fkgj+oK$KG2xec_HEhe-qvsryNu|m`(0kLib!See^@C~K%x5eo zsZ^&`Go3e8SMM;*gmJ&_47vJ2m;M@d?3~-l8V(s~OLa*_$vgUP9Sw|v-i2mPEnCjz z45SnD>lQC-T(K(Jy0N3Xcjvjv1yzb%Bi9ZV497^B`oOrMxubhkC#_qWVT}yq8?{{H zUPI4G)k3?}Z>brbSp^ma>P4kd9LlGR19`(e#QSOpER~eE#QhqMzth&JrN-Mg1a20^ zeN^9>(=q32d3oSANm0dq2baLRXp+N{;z<2tNH1=>MvjPzCJmTsN;kQxMP4r|+oiz* z$B`>H$n9b^gry;Tl^LA3!_}JuF;O`y-c85ep*n6~R=0|?+8pQ-#bIYu&FOvP$nT;> zk0`QtgL-Pxtr@QERW<~6h~i$_S%;=!Kgxot%3Xbd0KNdjDj4WZ_+q(JDW8d=*cfZ# zuYiHq7+cFr67He(WHs*9J!C(6Y?d4%$H)uhIC-7CP2M9Pl26Hb@+J9-d`-S3Ka!uw zFXUJ92MACGb6_qkga!!0QfPu^Xo2<63LBvtwm=W`!Y&wuAsB{zpu&AH1{tuR01v&hqLf8d;%BXJGcnn!*B4XP$^Uiicl@o2up;Ju#!(& zD7xw3WmeH&0$pM${^k_CFOzWKY915ZvbAS+c+C0v)JLjo7S`4+rH-?{J$8v+oRCEh zPUbeF^AsmwbY={g&cnc>o9Y)Yk%I%scm3dW4=j47QbCz5o};aoh2)?z2R(upZ&{AT zx#$hNxUwl2R0MQFUR=E?ViL1$Zo(pXh1ND%R?5-Yc-_WLSR~ZXdATEwJyF-W z4idjX&X5ntS#l0Ze3ATs)cp%YSOB$1*;`=++zw$_g|uyhJD{CWcPkQi8*GPu*n{NV zi}X!{4kIuM2IRnnJfrY~jKYt={NauGrH7`+h8!s1g zc(*jFX6&vmZxa=NE|)JHJ@rvXsXb^n*h6-7()LuwcBvAat&qrBrfrSjtf~7d*Em27 zklxvGY-nAF$A)NIGdng!TiU{*wrKP^jtw&|bn;P{Q2oT@&Pmr97F^w zxHIEe8T&G0O0k>56M=0sGmbTl9k?t%`3U$3_z3t2%q9fN_+&!#{vYGhYyJN}o2Kl~ z#YezL;9o`n%DWO>aqP4hicjyK-nEDDxd$JbXuAn6nF(gJ3DvXfcr4KC_!XYcyPjb6 nF;6$aB{N4mW{&bd`ZM5vy)pCSe?R`e!RUQ*G literal 0 HcmV?d00001 diff --git a/BankManager.swift b/BankManager.swift deleted file mode 100644 index 90347121..00000000 --- a/BankManager.swift +++ /dev/null @@ -1,7 +0,0 @@ -// -// BankManager.swift -// Created by yagom. -// Copyright © yagom academy. All rights reserved. -// - -import Foundation diff --git a/BankManagerConsoleApp/.DS_Store b/BankManagerConsoleApp/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..9c70a6bd6f96ae90e37f94ad165714be88b3d6ef GIT binary patch literal 10244 zcmeHMPfrs;6n_K7wjv1!iNT92TnK5jyMRKtlv0jHu|y9l*rgR}x4WBeYmJ1Ycl`){ z0q;uCa55o!(|F)Rc=wEN{?N|A673cvhnZyNZFk>E%z!L46t1T+E~0gZr0KqGJ-5WsIXZHYln^{5fh z2xtV_2+;e%z)opK&=#lc>cD}e07zr_nl*gJJb=jbY`b~E(7lU=jMi;1- zE|79I%>24n^@Z`h?xd?;E+=qKOjf?_RCf0ly46tZQG>rJd%*71F8qIyuepxrZs4lv zMqK;*7=s(|64o*5iWqf1)bQ!Kf+qR8)4N6);ql$i*H`x#!ac6D>UB^*JFUu2^s$|| z+6A5~h+_bC2>$o!HE+$O3@bgwPOmjRn=)oCE5hlG2BPmc~5G)I0Fp?fQ@|NBG zR|g+h9Xt6K#doC6PO`(kB{g|7LK#%BC!4sKP(B&V(Xqo{ z&-gJug|`FNpLj#Sh`|zm4jWKa*G9hM+S+sSdcGXaJK0ZcbEWyt8p7sPy9TO;0sJiU z%g|9R54~4sW!pc-uKW~ONb>Ua6waaAPjmar=yi#9)qXP0?6+rS`)}W^PTb{tF#r-r z$NO-O5c_lux@iP70vZ90fJQ(ga7GC9b3F#F|1X{X|Nj|T(`qyV8iDhO0Ey-Dxh$^i zsA6_}k+jxsV1J68Hp + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/BankManagerConsoleApp/BankManagerConsoleApp/main.swift b/BankManagerConsoleApp/BankManagerConsoleApp/main.swift index 1aa9854c..8496ac3b 100644 --- a/BankManagerConsoleApp/BankManagerConsoleApp/main.swift +++ b/BankManagerConsoleApp/BankManagerConsoleApp/main.swift @@ -4,4 +4,5 @@ // Copyright © yagom academy. All rights reserved. // -import Foundation +var console: ConsoleManager = ConsoleManager() +console.operate() diff --git a/BankManagerConsoleApp/BankManagerConsoleAppTests/.DS_Store b/BankManagerConsoleApp/BankManagerConsoleAppTests/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0! + + override func setUpWithError() throws { + sutLinkedTest = LinkedList() + } + + override func tearDownWithError() throws { + sutLinkedTest = nil + } + + func test_입력이1개일때_노드를1개추가하고삭제를1번하면_입력한노드가없을것이다() { + // given + let input = "A" + // when + sutLinkedTest.appendNodeAtRear(with: input) + let _ = sutLinkedTest.removeNodeFromFront() + let result = sutLinkedTest.searchNodeLocation(with: input) + // then + XCTAssertNil(result) + } + + func test_입력이1개일때_노드를1개추가하고그값을조회하면_1번위치가반환된다() { + // given + let input = "A" + let expectedResult = 1 + // when + sutLinkedTest.appendNodeAtRear(with: input) + let result = sutLinkedTest.searchNodeLocation(with: input) + // then + XCTAssertEqual(expectedResult, result) + } + + func test_입력이3개일때_노드를3개추가하고3번째값을조회하면_3번위치가반환된다() { + // given + let input = [ "A", "B", "C" ] + let expectedResult = 3 + // when + for data in input { + sutLinkedTest.appendNodeAtRear(with: data) + } + let result = sutLinkedTest.searchNodeLocation(with: input[2]) + // then + XCTAssertEqual(expectedResult, result) + } +} + diff --git a/BankManagerConsoleApp/BankManagerConsoleAppTests/BankManagerQueueTests.swift b/BankManagerConsoleApp/BankManagerConsoleAppTests/BankManagerQueueTests.swift new file mode 100644 index 00000000..5831cfb0 --- /dev/null +++ b/BankManagerConsoleApp/BankManagerConsoleAppTests/BankManagerQueueTests.swift @@ -0,0 +1,176 @@ + +import XCTest +@testable import BankManagerConsoleApp + +final class BankManagerQueueTests: XCTestCase { + var sut: Queue! + + override func setUpWithError() throws { + sut = Queue() + } + + override func tearDownWithError() throws { + sut = nil + } + + func test_초기상태일때_대기열을확인하면_대기열이없을것이다() { + // given + // when + let result = sut.isEmpty + // then + XCTAssertTrue(result) + } + + func test_입력이1개있을때_enqueue를수행하면_대기열이있을것이다() { + // given + let input = "test" + // when + sut.enqueue(with: input) + let result = sut.isEmpty + // then + XCTAssertFalse(result) + } + + func test_입력이5개있을때_queue의길이를확인하면_길이가5일것이다() { + // given + let data = [ "A", "B", "C", "D", "E" ] + let expectedLengthOfQueue = 5 + // when + for input in data { + sut.enqueue(with: input) + } + let result = sut.totalLength() + // then + XCTAssertEqual(expectedLengthOfQueue, result) + } + + func test_입력이5개일때_enqueue를5번dequeue를4번하면_queue의길이가1일것이다() { + // given + let data = [ "A", "B", "C", "D", "E" ] + let lengthOfQueue = 1 + let countOfDeque = 4 + // when + for input in data { + sut.enqueue(with: input) + } + for _ in 1...countOfDeque { + let _ = sut.dequeue() + } + let result = sut.totalLength() + // then + XCTAssertEqual(lengthOfQueue, result) + } + + func test_입력이5개일때_enqueue를5번dequeue3번하면_4번째데이터를정상적으로peek한다() { + // given + let data = [ "A", "B", "C", "D", "E" ] + let countOfDeque = 3 + let expectedResult = data[countOfDeque] + // when + for input in data { + sut.enqueue(with: input) + } + for _ in 1...countOfDeque { + let _ = sut.dequeue() + } + let result = sut.peek() + // then + XCTAssertEqual(expectedResult, result) + } + + func test_초기상태일때_dequeue를1번수행하면_길이가0을유지한다() { + // given + let expectedCount = 0 + // when + let _ = sut.dequeue() + let result = sut.totalLength() + // then + XCTAssertEqual(expectedCount, result) + } + + func test_초기상태일때_dequeue를1번수행하면_가져오는것이없을것이다() { + // given + // when + let result = sut.dequeue() + // then + XCTAssertNil(result) + } + + func test_입력이2개일때_enqueue를2번dequeue를1번수행하면_peek되는값이second일것이다() { + // given + let input = [ "First", "Second" ] + let expectedResult = "Second" + // when + for data in input { + sut.enqueue(with: data) + } + let _ = sut.dequeue() + let result = sut.peek() + // then + XCTAssertEqual(expectedResult, result) + } + + func test_입력이2개일때_enqueue를2번수행하면_dequeue한값이First일것이다() { + // given + let input = [ "First", "Second" ] + let expectedResult = "First" + //when + for data in input { + sut.enqueue(with: data) + } + let result = sut.dequeue() + // then + XCTAssertEqual(expectedResult, result) + } + + func test_입력이2개일때_enqueue를2번하고peek를하면_반환되는값이First일것이다() { + // given + let input = [ "First", "Second" ] + let expectedResult = "First" + // when + for data in input { + sut.enqueue(with: data) + } + let result = sut.peek() + // then + XCTAssertEqual(expectedResult, result) + } + + func test_입력이4개일때_enqueue를4번하고clean을하면_대기열이없을것이다() { + // given + let input = [ "A", "B", "C", "D" ] + // when + for data in input { + sut.enqueue(with: data) + } + let _ = sut.clean() + let result = sut.isEmpty + // then + XCTAssertTrue(result) + } + + func test_입력이3개일때_enque를3번하면_head의다음다음노드가정상연결되고데이터를불러온다() { + // given + let input = [ "A", "B", "C" ] + let expectedResult = "C" + // when + for data in input { + sut.enqueue(with: data) + } + let result = sut.linkedList.head?.next?.next?.data + // then + XCTAssertEqual(expectedResult, result) + } + + func test_입력이3개일때_enqueue를3번하면_tail의다음연결이nil이다() { + // given + let input = [ "A", "B", "C" ] + // when + for data in input { + sut.enqueue(with: data) + } + let result = sut.linkedList.tail?.next + // then + XCTAssertNil(result) + } +} diff --git a/BankManagerConsoleApp/Controller/.DS_Store b/BankManagerConsoleApp/Controller/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 Bool { + if input == "1" { + return true + } else { + print("종료되었습니다.") + return false + } + } + + private mutating func executeBankingOperation() { + customerManager.arrangeCustomers() + + let bankingServiceStart = Date.timeIntervalSinceReferenceDate + handleCustomersTasks() + let bankingServiceEnd = Date.timeIntervalSinceReferenceDate + + bankManager.reportDeadlineSummary(with: customerManager.totalCustomerNumber, startTime: bankingServiceStart, endTime: bankingServiceEnd) + customerManager.resetCustomer() + } + + private func selectedByUser() throws -> String { + guard let input = readLine(), input == "1" || input == "2" else { + throw InputError.wrongInput + } + return input + } + + private func handleCustomersTasks() { + let group = DispatchGroup() + let semaphore = DispatchSemaphore(value: 1) + bankManager.employees[0].handleLoanTasks(atTable: 0, with: customerManager.loanTicketMachine, bankManager: bankManager, group: group) + bankManager.employees[1].handleDepositTasks(atTable: 1, with: customerManager.depositTicketMachine, bankManager: bankManager, group: group, semaphore: semaphore) + bankManager.employees[2].handleDepositTasks(atTable: 2, with: customerManager.depositTicketMachine, bankManager: bankManager, group: group, semaphore: semaphore) + group.wait() + } + + private func printMenuOfBank() { + print("1 : 은행개점") + print("2 : 종료") + print("입력 : ", terminator: "") + } +} diff --git a/BankManagerConsoleApp/Controller/CustomerManager.swift b/BankManagerConsoleApp/Controller/CustomerManager.swift new file mode 100644 index 00000000..94a78215 --- /dev/null +++ b/BankManagerConsoleApp/Controller/CustomerManager.swift @@ -0,0 +1,39 @@ + +struct CustomerManager { + private(set) var loanTicketMachine: Queue = Queue() + private(set) var depositTicketMachine: Queue = Queue() + var customerNumber = CustomerNumber(loan: 0, deposit: 0) + + var totalCustomerNumber: Int { + return customerNumber.loan + customerNumber.deposit + } + + mutating func arrangeCustomers() { + createCustomers() + customerNumber.loan = loanTicketMachine.totalLength() + customerNumber.deposit = depositTicketMachine.totalLength() + } + + func createCustomers() { + let number = Int.random(in: 10...30) + for ticketNumber in 1...number { + guard let customerChoice = BankingService.allCases.randomElement() else { return } + customerChoice == BankingService.loan ? + loanTicketMachine.enqueue(with: Customer(ticketNumber: ticketNumber, bankingService: customerChoice)) : + depositTicketMachine.enqueue(with: Customer(ticketNumber: ticketNumber, bankingService: customerChoice)) + } + } + + func dequeueLoanCustomerFromQueue() -> Customer? { + return loanTicketMachine.dequeue() + } + + func dequeueDepositCustomerFromQueue() -> Customer? { + return depositTicketMachine.dequeue() + } + + func resetCustomer() { + loanTicketMachine.clean() + depositTicketMachine.clean() + } +} diff --git a/BankManagerConsoleApp/Enum/BankingService.swift b/BankManagerConsoleApp/Enum/BankingService.swift new file mode 100644 index 00000000..a55dcfc7 --- /dev/null +++ b/BankManagerConsoleApp/Enum/BankingService.swift @@ -0,0 +1,12 @@ + +enum BankingService: CaseIterable { + case loan + case deposit + + var name : String { + switch self { + case .loan: return "대출" + case .deposit: return "예금" + } + } +} diff --git a/BankManagerConsoleApp/Enum/InputError.swift b/BankManagerConsoleApp/Enum/InputError.swift new file mode 100644 index 00000000..a2075992 --- /dev/null +++ b/BankManagerConsoleApp/Enum/InputError.swift @@ -0,0 +1,13 @@ + +import Foundation + +enum InputError: LocalizedError { + case wrongInput + + var errorDescription: String? { + switch self { + case .wrongInput: + return "보여지는 메뉴의 숫자만 입력하세요!" + } + } +} diff --git a/BankManagerConsoleApp/Model/Customer.swift b/BankManagerConsoleApp/Model/Customer.swift new file mode 100644 index 00000000..28385a31 --- /dev/null +++ b/BankManagerConsoleApp/Model/Customer.swift @@ -0,0 +1,14 @@ + +struct Customer: Equatable { + private var ticketNumber: Int? + private var bankingService: BankingService? + + init(ticketNumber: Int? = nil, bankingService: BankingService? = nil) { + self.ticketNumber = ticketNumber + self.bankingService = bankingService + } + + func askEmployeeHandleTasks() -> (Int?, BankingService?)? { + return (ticketNumber, bankingService) + } +} diff --git a/BankManagerConsoleApp/Model/CustomerNumber.swift b/BankManagerConsoleApp/Model/CustomerNumber.swift new file mode 100644 index 00000000..0e316d6a --- /dev/null +++ b/BankManagerConsoleApp/Model/CustomerNumber.swift @@ -0,0 +1,5 @@ + +struct CustomerNumber { + var loan: Int + var deposit: Int +} diff --git a/BankManagerConsoleApp/Model/Employee.swift b/BankManagerConsoleApp/Model/Employee.swift new file mode 100644 index 00000000..da26e58c --- /dev/null +++ b/BankManagerConsoleApp/Model/Employee.swift @@ -0,0 +1,53 @@ + +import Foundation + +struct Employee { + var loanTask: DispatchQueue? + var depositTask: DispatchQueue? + + init(loanTask: DispatchQueue? = nil, depositTask: DispatchQueue? = nil) { + self.loanTask = loanTask + self.depositTask = depositTask + } + + func handleLoanTasks(atTable index: Int, with customerLoanQueue: Queue, bankManager: BankManager, group: DispatchGroup) { + bankManager.employees[index].loanTask?.async(group: group) { + while let customer = customerLoanQueue.dequeue() { + guard let (customerTicketNumber, customerBankingService) = customer.askEmployeeHandleTasks(), let definedCustomerTicketNumber = customerTicketNumber, let definedCustomerBankingService = customerBankingService else { return } + print("🌝 \(definedCustomerTicketNumber)번 고객 \(definedCustomerBankingService.name)업무 시작") + Thread.sleep(forTimeInterval: 1.1) + print("🌝 \(definedCustomerTicketNumber)번 고객 \(definedCustomerBankingService.name)업무 종료") + } + } + } + + func handleDepositTasks(atTable index: Int, with customerDepositQueue: Queue, bankManager: BankManager, group: DispatchGroup, semaphore: DispatchSemaphore) { + + if index == 1 { + bankManager.employees[index].depositTask?.async(group: group) { + semaphore.wait() + while let customer = customerDepositQueue.dequeue() { + + guard let (customerTicketNumber, customerBankingService) = customer.askEmployeeHandleTasks(), let definedCustomerTicketNumber = customerTicketNumber, let definedCustomerBankingService = customerBankingService else { return } + semaphore.signal() + + print("🥵 \(definedCustomerTicketNumber)번 고객 \(definedCustomerBankingService.name)업무 시작") + Thread.sleep(forTimeInterval: 0.7) + print("🥵 \(definedCustomerTicketNumber)번 고객 \(definedCustomerBankingService.name)업무 종료") + } + } + } else if index == 2 { + bankManager.employees[index].depositTask?.async(group: group) { + semaphore.wait() + while let customer = customerDepositQueue.dequeue() { + guard let (customerTicketNumber, customerBankingService) = customer.askEmployeeHandleTasks(), let definedCustomerTicketNumber = customerTicketNumber, let definedCustomerBankingService = customerBankingService else { return } + semaphore.signal() + + print("🥶 \(definedCustomerTicketNumber)번 고객 \(definedCustomerBankingService.name)업무 시작") + Thread.sleep(forTimeInterval: 0.7) + print("🥶 \(definedCustomerTicketNumber)번 고객 \(definedCustomerBankingService.name)업무 종료") + } + } + } + } +} diff --git a/BankManagerConsoleApp/Queue/LinkedList.swift b/BankManagerConsoleApp/Queue/LinkedList.swift new file mode 100644 index 00000000..4c142840 --- /dev/null +++ b/BankManagerConsoleApp/Queue/LinkedList.swift @@ -0,0 +1,64 @@ + +struct LinkedList { + private(set) var head: Node? + private(set) var tail: Node? + private(set) var count: Int = 0 + var isEmpty: Bool { head == nil } + + mutating func appendNodeAtRear(with data: T) { + if isEmpty { + tail = Node(data: data) + head = tail + count += 1 + return + } + tail?.updateNext(next: Node(data: data)) + tail = tail?.next + count += 1 + } + + mutating func removeNodeFromFront() -> T? { + let removedNode = head + if isEmpty { + tail = nil + count = 0 + return nil + } + head = head?.next + count -= 1 + return removedNode?.data + } + + mutating func clean() { + head = nil + tail = nil + count = 0 + } + + func peek() -> T? { + head?.data + } + + mutating func searchNodeLocation(with data: T?) -> Int? { + var foundLocation = 0 + + while head?.next != nil { + foundLocation += 1 + if head?.data == data { + return foundLocation + } + head = head?.next + } + + if isEmpty { + return nil + } else if head?.next == nil { + if head?.data == data { + foundLocation += 1 + return foundLocation + } + } + + return nil + } +} diff --git a/BankManagerConsoleApp/Queue/Node.swift b/BankManagerConsoleApp/Queue/Node.swift new file mode 100644 index 00000000..8b68817d --- /dev/null +++ b/BankManagerConsoleApp/Queue/Node.swift @@ -0,0 +1,18 @@ + +final class Node { + private(set) var data: T? + private(set) var next: Node? + + init(data: T? = nil, next: Node? = nil) { + self.data = data + self.next = next + } + + func updateData(data: T?) { + self.data = data + } + + func updateNext(next: Node?) { + self.next = next + } +} diff --git a/BankManagerConsoleApp/Queue/Queue.swift b/BankManagerConsoleApp/Queue/Queue.swift new file mode 100644 index 00000000..1fb11ada --- /dev/null +++ b/BankManagerConsoleApp/Queue/Queue.swift @@ -0,0 +1,25 @@ + +class Queue { + private(set) var linkedList: LinkedList = LinkedList() + var isEmpty: Bool { linkedList.isEmpty } + + func enqueue(with item: T) { + linkedList.appendNodeAtRear(with: item) + } + + func dequeue() -> T? { + return linkedList.removeNodeFromFront() + } + + func clean() { + linkedList.clean() + } + + func peek() -> T? { + return linkedList.peek() + } + + func totalLength() -> Int { + return linkedList.count + } +} diff --git a/BankManagerUIApp/.DS_Store b/BankManagerUIApp/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..f5ca2c4fa278b34587b28b03fda510d64fcd42ea GIT binary patch literal 8196 zcmeI1v1{8v6vn@6LJ)LupeCje(9#SY0{#zFV-FrOxyjI>w6;q^YgfW{AzK6)vt?=j zi+1Re?14b((0`_D``x=Ek?aH^8A^lSgS$_h?tORu+0pS70H*b6*aR8?0ye9)$L#J1 zd*8N&c;rW(P!02eJ-o$7wnMyIn5_d!KnW-TC7=Y9!2dviXSR*to^#*b)m{lGft4g+ zpAR9M)yQk=R307dqy(U@b6A4ctOFz`^cs0hok}V_UG-pms_`X;ap}At+UeBDYwFac z!?<)9zq0Waijmcs7s_;)$f@>9KnW}ou(f*)Evef-lUsxj)#@xJxJlKB)fO!LIe_8jpNPI=B7U+7dKJ4<)}`uFQs?iX9i;r6#%%^^q99idx=B<#@)zU7vWUIT}L6;v$LkT=c0&8y94O{ZX;zQUuI;yVq{f!q?UdX13N2+1{|(u=7%S5T30?qJsA5ai5^rH%LQ#g5E?#`-37030A>ld?ODYd;uTD2k_$AZ+3>5 zkd}%E6`6tAZ!yZMM{KtvIgvA2U^h44CSM|x`EMHlwCr6mpNo@R8> z@ixO>WPtDPfX2ADB`v6Vf3+pM98G3LUP}7gZ<{0XB2A}RF@H1{nK@R&D)coFnHQfKZ}M&D={ZjmW55_N z27ZeH)NGb$PtZnVz!)$FY6kfG;GvAMVkHDv(pa6g|}mWAKb~sf;Jih#z32aEqNSp{Xbsc|F?te z$rvyO{uKkRpH9*QN7CBbIULto4?TynuwNxuhhP#)F?_icA3}q`?s)=?6)QnlAofQf M&|rfx@S_ZT0O4R_4*&oF literal 0 HcmV?d00001 diff --git a/BankManagerUIApp/BankManagerUIApp/BankManager.swift b/BankManagerUIApp/BankManagerUIApp/BankManager.swift new file mode 100644 index 00000000..9348c8bf --- /dev/null +++ b/BankManagerUIApp/BankManagerUIApp/BankManager.swift @@ -0,0 +1,8 @@ +// +// BankManager.swift +// BankManagerUIApp +// +// Created by Harry Ho on 2/7/24. +// + +import Foundation diff --git a/BankManagerUIApp/BankManagerUIApp/Base.lproj/Main.storyboard b/BankManagerUIApp/BankManagerUIApp/Base.lproj/Main.storyboard deleted file mode 100644 index ce80ec92..00000000 --- a/BankManagerUIApp/BankManagerUIApp/Base.lproj/Main.storyboard +++ /dev/null @@ -1,32 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -