diff --git a/scala_project_2025/bank_system/.gitignore b/scala_project_2025/bank_system/.gitignore new file mode 100644 index 0000000..b52a582 --- /dev/null +++ b/scala_project_2025/bank_system/.gitignore @@ -0,0 +1,4 @@ +target/ +project/ +.bloop/ +.metals/ diff --git a/scala_project_2025/bank_system/.scalafmt.conf b/scala_project_2025/bank_system/.scalafmt.conf new file mode 100644 index 0000000..04f6251 --- /dev/null +++ b/scala_project_2025/bank_system/.scalafmt.conf @@ -0,0 +1,2 @@ +version = "3.7.15" +runner.dialect = scala213 diff --git a/scala_project_2025/bank_system/src/main/scala/Account.scala b/scala_project_2025/bank_system/src/main/scala/Account.scala index 30036e7..a0ac7be 100644 --- a/scala_project_2025/bank_system/src/main/scala/Account.scala +++ b/scala_project_2025/bank_system/src/main/scala/Account.scala @@ -1,11 +1,10 @@ +class Account(val code: String, val balance: Double) { -class Account(val code : String, val balance: Double) { + // TODO + // Implement functions. Account should be immutable. + // Change return type to the appropriate one + def withdraw(amount: Double): Unit = ??? - // TODO - // Implement functions. Account should be immutable. - // Change return type to the appropriate one - def withdraw(amount: Double) : Unit = ??? - - def deposit (amount: Double) : Unit = ??? + def deposit(amount: Double): Unit = ??? } diff --git a/scala_project_2025/bank_system/src/main/scala/Bank.scala b/scala_project_2025/bank_system/src/main/scala/Bank.scala index c669268..65c1488 100644 --- a/scala_project_2025/bank_system/src/main/scala/Bank.scala +++ b/scala_project_2025/bank_system/src/main/scala/Bank.scala @@ -2,64 +2,61 @@ import collection.mutable.Map class Bank(val allowedAttempts: Integer = 3) { - private val accountsRegistry : Map[String,Account] = Map() + private val accountsRegistry: Map[String, Account] = Map() - val transactionsPool: TransactionPool = new TransactionPool() - val completedTransactions: TransactionPool = new TransactionPool() + val transactionsPool: TransactionPool = new TransactionPool() + val completedTransactions: TransactionPool = new TransactionPool() + def processing: Boolean = !transactionsPool.isEmpty - def processing : Boolean = !transactionsPool.isEmpty + // TODO + // Adds a new transaction for the transfer to the transaction pool + def transfer(from: String, to: String, amount: Double): Unit = ??? - // TODO - // Adds a new transaction for the transfer to the transaction pool - def transfer(from: String, to: String, amount: Double): Unit = ??? + // TODO + // Process the transactions in the transaction pool + // The implementation needs to be completed and possibly fixed + def processTransactions: Unit = { - // TODO - // Process the transactions in the transaction pool - // The implementation needs to be completed and possibly fixed - def processTransactions: Unit = { + // val workers : List[Thread] = transactionsPool.iterator.toList + // .filter(/* select only pending transactions */) + // .map(processSingleTransaction) - // val workers : List[Thread] = transactionsPool.iterator.toList - // .filter(/* select only pending transactions */) - // .map(processSingleTransaction) + // workers.map( element => element.start() ) + // workers.map( element => element.join() ) - // workers.map( element => element.start() ) - // workers.map( element => element.join() ) + /* TODO: change to select only transactions that succeeded */ + // val succeded : List[Transaction] = transactionsPool - /* TODO: change to select only transactions that succeeded */ - // val succeded : List[Transaction] = transactionsPool + /* TODO: change to select only transactions that failed */ + // val failed : List[Transaction] = transactionsPool - /* TODO: change to select only transactions that failed */ - // val failed : List[Transaction] = transactionsPool + // succeded.map(/* remove transactions from the transaction pool */) + // succeded.map(/* add transactions to the completed transactions queue */) - // succeded.map(/* remove transactions from the transaction pool */) - // succeded.map(/* add transactions to the completed transactions queue */) - - //failed.map(t => { - /* transactions that failed need to be set as pending again; + // failed.map(t => { + /* transactions that failed need to be set as pending again; if the number of retry has exceeded they also need to be removed from the transaction pool and to be added to the queue of completed transactions */ - //}) + // }) - if(!transactionsPool.isEmpty) { - processTransactions - } + if (!transactionsPool.isEmpty) { + processTransactions } + } - // TODO - // The function creates a new thread ready to process - // the transaction, and returns it as a return value - private def processSingleTransaction(t : Transaction) : Thread = ??? + // TODO + // The function creates a new thread ready to process + // the transaction, and returns it as a return value + private def processSingleTransaction(t: Transaction): Thread = ??? + // TODO + // Creates a new account and returns its code to the user. + // The account is stored in the local registry of bank accounts. + def createAccount(initialBalance: Double): String = ??? - // TODO - // Creates a new account and returns its code to the user. - // The account is stored in the local registry of bank accounts. - def createAccount(initialBalance: Double) : String = ??? - - - // TODO - // Return information about a certain account based on its code. - // Remember to handle the case in which the account does not exist - def getAccount(code : String) : Option[Account] = ??? + // TODO + // Return information about a certain account based on its code. + // Remember to handle the case in which the account does not exist + def getAccount(code: String): Option[Account] = ??? } diff --git a/scala_project_2025/bank_system/src/main/scala/Main.scala b/scala_project_2025/bank_system/src/main/scala/Main.scala index bfcd923..e4b32da 100644 --- a/scala_project_2025/bank_system/src/main/scala/Main.scala +++ b/scala_project_2025/bank_system/src/main/scala/Main.scala @@ -1,13 +1,12 @@ - object Main extends App { - def thread(body: => Unit): Thread = { - val t = new Thread { - override def run() = body - } - - t.start - t + def thread(body: => Unit): Thread = { + val t = new Thread { + override def run() = body } - + + t.start + t + } + } diff --git a/scala_project_2025/bank_system/src/main/scala/Transaction.scala b/scala_project_2025/bank_system/src/main/scala/Transaction.scala index 1f3b9c5..d959748 100644 --- a/scala_project_2025/bank_system/src/main/scala/Transaction.scala +++ b/scala_project_2025/bank_system/src/main/scala/Transaction.scala @@ -4,27 +4,29 @@ object TransactionStatus extends Enumeration { class TransactionPool { - // Remove and the transaction from the pool - def remove(t: Transaction): Boolean = ??? + // Remove and the transaction from the pool + def remove(t: Transaction): Boolean = ??? - // Return whether the queue is empty - def isEmpty: Boolean = ??? + // Return whether the queue is empty + def isEmpty: Boolean = ??? - // Return the size of the pool - def size: Integer = ??? + // Return the size of the pool + def size: Integer = ??? - // Add new element to the back of the queue - def add(t: Transaction): Boolean = ??? + // Add new element to the back of the queue + def add(t: Transaction): Boolean = ??? - // Return an iterator to allow you to iterate over the queue - def iterator : Iterator[Transaction] = ??? + // Return an iterator to allow you to iterate over the queue + def iterator: Iterator[Transaction] = ??? } -class Transaction(val from: String, - val to: String, - val amount: Double, - val retries: Int = 3) { +class Transaction( + val from: String, + val to: String, + val amount: Double, + val retries: Int = 3 +) { private var status: TransactionStatus.Value = TransactionStatus.PENDING private var attempts = 0