From dbc3ae50fefef80f2a84bf676e6b382b20d4cc7a Mon Sep 17 00:00:00 2001 From: h7x4 Date: Fri, 11 Nov 2022 15:36:17 +0100 Subject: [PATCH] Solve task 1.2 and 1.3 --- project_tasks/src/main/scala/Account.scala | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/project_tasks/src/main/scala/Account.scala b/project_tasks/src/main/scala/Account.scala index 542d7bb..977c4d3 100644 --- a/project_tasks/src/main/scala/Account.scala +++ b/project_tasks/src/main/scala/Account.scala @@ -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) } - - }