From 2a3141ed9f04407005f577681ed1f5f5ac525e29 Mon Sep 17 00:00:00 2001 From: fredrikr79 Date: Fri, 7 Nov 2025 11:31:09 +0100 Subject: [PATCH] scp2: task 1.2 + 1.3 --- .../bank_system/src/main/scala/Account.scala | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) 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 a0ac7be..428cdb4 100644 --- a/scala_project_2025/bank_system/src/main/scala/Account.scala +++ b/scala_project_2025/bank_system/src/main/scala/Account.scala @@ -1,10 +1,13 @@ class Account(val code: String, val balance: Double) { + def withdraw(amount: Double): Either[String, Account] = + if (amount < 0) + Left("cannot withdraw negative amounts") + else if (amount > balance) + Left("cannot withdraw more than you have") + else Right(new Account(code, balance - amount)) - // 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): Either[String, Account] = + if (amount < 0) + Left("cannot deposit negative amounts") + else Right(new Account(code, balance + amount)) }