From 9c93da087db5d7879b5d99722bff8f2dd43c4f81 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Fri, 11 Nov 2022 16:42:06 +0100 Subject: [PATCH] Solve task 1.2 (maybe?) --- project_tasks/src/main/scala/Bank.scala | 33 ++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/project_tasks/src/main/scala/Bank.scala b/project_tasks/src/main/scala/Bank.scala index 3cb66b0..e3e43de 100644 --- a/project_tasks/src/main/scala/Bank.scala +++ b/project_tasks/src/main/scala/Bank.scala @@ -3,14 +3,41 @@ class Bank(val allowedAttempts: Integer = 3) { private val transactionsQueue: TransactionQueue = new TransactionQueue() private val processedTransactions: TransactionQueue = new TransactionQueue() - def addTransactionToQueue(from: Account, to: Account, amount: Double): Unit = ??? + def addTransactionToQueue(from: Account, to: Account, amount: Double): Unit = { + transactionsQueue.push(new Transaction( + transactionsQueue, + processedTransactions, + from, + to, + amount, + 10, + )) + + Main.thread(processTransaction(transactionsQueue.pop())) + } // TODO // project task 2 // create a new transaction object and put it in the queue // spawn a thread that calls processTransactions - private def processTransactions: Unit = ??? - // TOO + // There are mixed instructions for this method. + // It's called `processTransactions`, indicating that it should + // process all lists, the part in the assigment pdf indicates this as well. + // However the comment below is written as if there is only one transaction to + // be processed, and the fact that `addTransactionToQueue` calls this method every + // time something is added, supports that theory as well. + // We just went with the most logical option... + private def processTransactions(trx: Transaction): Unit = { + // thread = Main.thread(trx) + // thread.join() + trx() + if (trx.status == TransactionStatus.PENDING && trx.attempt < trx.allowedAttemps) { + processTransactions(trx) + } else { + processedTransactions.push(trx); + } + } + // TODO // project task 2 // Function that pops a transaction from the queue // and spawns a thread to execute the transaction.