scp2: init

This commit is contained in:
2025-11-07 10:43:16 +01:00
parent 48c1baf230
commit ead6b7a915
6 changed files with 76 additions and 73 deletions

View File

@@ -0,0 +1,4 @@
target/
project/
.bloop/
.metals/

View File

@@ -0,0 +1,2 @@
version = "3.7.15"
runner.dialect = scala213

View File

@@ -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 = ???
}

View File

@@ -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] = ???
}

View File

@@ -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
}
}

View File

@@ -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