Solve task 1.2 and 1.3

This commit is contained in:
Oystein Kristoffer Tveit 2022-11-11 15:36:17 +01:00
parent d047efd8e6
commit dbc3ae50fe
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146

View File

@ -9,13 +9,19 @@ class Account(val bank: Bank, initialBalance: Double) {
// TODO
// for project task 1.2: implement functions
// for project task 1.3: change return type and update function bodies
def withdraw(amount: Double): Unit = ???
def deposit (amount: Double): Unit = ???
def getBalanceAmount: Double = ???
def withdraw(amount: Double): Either[Unit, String] = this.synchronized({
if (amount < 0 || balance.amount < amount) return Right("OOF");
Left({ balance.amount -= amount })
})
def deposit (amount: Double): Either[Unit, String] = this.synchronized({
if (amount < 0) return Right("OOF");
Left({ balance.amount += amount })
})
def getBalanceAmount: Double = this.synchronized(balance.amount)
def transferTo(account: Account, amount: Double) = {
bank addTransactionToQueue (this, account, amount)
}
}