diff --git a/exercises/bank-account/HINTS.md b/exercises/bank-account/HINTS.md new file mode 100644 index 00000000..e563ace0 --- /dev/null +++ b/exercises/bank-account/HINTS.md @@ -0,0 +1,11 @@ +## Hints + +This exercise is testing mutable state that can be accessed saftely from multiple threads. Scala provides a variety of ways to protect +mutable state. For developers familiar with Java concurrency, Scala can utilize the Java concurrency support such as the Java synchronized block. + +### Common Pitfalls + +In Scala there are two ways to achieve mutable state: Use a "var" or a mutable object. +Two common mistakes here are: +- Do not use a "var" that is also a mutable object. One is enough, but not both together. +- Don't expose the "var" or mutable object to the outside world. So make them "private" and change the mutable object into immutable before you return it as a value. diff --git a/exercises/bank-account/example.scala b/exercises/bank-account/example.scala index f1e59cbc..753820b0 100644 --- a/exercises/bank-account/example.scala +++ b/exercises/bank-account/example.scala @@ -23,8 +23,6 @@ protected case class Account(var balance: Option[Int] = Some(0)) extends BankAcc } } -object BankAccount { - def apply(): BankAccount = Account() +object Bank { + def openAccount(): BankAccount = Account() } - - diff --git a/exercises/bank-account/src/main/scala/BankAccount.scala b/exercises/bank-account/src/main/scala/BankAccount.scala new file mode 100644 index 00000000..a4d2c960 --- /dev/null +++ b/exercises/bank-account/src/main/scala/BankAccount.scala @@ -0,0 +1,13 @@ +trait BankAccount { + + def closeAccount(): Unit + + def getBalance: Option[Int] + + def incrementBalance(increment: Int): Option[Int] +} + +object Bank { + def openAccount(): BankAccount = ??? +} + diff --git a/exercises/bank-account/src/test/scala/BankAccountTest.scala b/exercises/bank-account/src/test/scala/BankAccountTest.scala index cc1da846..c8fdafc4 100644 --- a/exercises/bank-account/src/test/scala/BankAccountTest.scala +++ b/exercises/bank-account/src/test/scala/BankAccountTest.scala @@ -3,12 +3,12 @@ import org.scalatest.{Matchers, FunSuite} class BankAccountTest extends FunSuite with Matchers with Conductors with IntegrationPatience { test("open account") { - BankAccount().getBalance should be (Some(0)) + Bank.openAccount().getBalance should be (Some(0)) } test("incrementing and checking balance") { pending - val acct = BankAccount() + val acct = Bank.openAccount() acct.getBalance should be (Some(0)) acct.incrementBalance(10) should be (Some(10)) acct.getBalance should be (Some(10)) @@ -18,7 +18,7 @@ class BankAccountTest extends FunSuite with Matchers with Conductors with Integr test("closed account should hold no balance") { pending - val acct = BankAccount() + val acct = Bank.openAccount() acct.closeAccount() acct.incrementBalance(10) acct.incrementBalance(10) @@ -30,7 +30,7 @@ class BankAccountTest extends FunSuite with Matchers with Conductors with Integr val conductor = new Conductor import conductor._ - val acct = BankAccount() + val acct = Bank.openAccount() thread("t1") { acct.incrementBalance(10) @@ -54,7 +54,7 @@ class BankAccountTest extends FunSuite with Matchers with Conductors with Integr val conductor = new Conductor import conductor._ - val acct = BankAccount() + val acct = Bank.openAccount() thread("t1") { for (a <- 1 to 10)