Skip to content

Commit 6e0196a

Browse files
committed
Rework interface definition to cleanly show openAccount. Add HINTS.md with note about mutable state. Refs exercism#214
1 parent db0889e commit 6e0196a

File tree

4 files changed

+41
-23
lines changed

4 files changed

+41
-23
lines changed

exercises/bank-account/HINTS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
## Hints
2+
3+
This exercise is testing mutable state that can be accessed saftely from multiple threads. Scala provides a variety of ways to protect
4+
mutable state. For developers familiar with Java concurrency, Scala can utilize the Java concurrency support such as synchronized.

exercises/bank-account/example.scala

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,8 @@ trait BankAccount {
77
def incrementBalance(increment: Int): Option[Int]
88
}
99

10-
protected case class Account(var balance: Option[Int] = Some(0)) extends BankAccount {
11-
12-
private def runThreadSafe[A](block: => A): A = this.synchronized(block)
13-
14-
override def closeAccount(): Unit = runThreadSafe(balance = None)
15-
16-
override def getBalance: Option[Int] = runThreadSafe(balance)
17-
18-
override def incrementBalance(increment: Int): Option[Int] = runThreadSafe {
19-
balance flatMap { amount =>
20-
balance = Some(amount + increment)
21-
balance
22-
}
23-
}
24-
}
25-
26-
object BankAccount {
27-
def apply(): BankAccount = Account()
10+
object Bank {
11+
def openAccount(): BankAccount = ???
2812
}
2913

3014

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
trait BankAccount {
2+
3+
def closeAccount(): Unit
4+
5+
def getBalance: Option[Int]
6+
7+
def incrementBalance(increment: Int): Option[Int]
8+
}
9+
10+
protected case class Account(var balance: Option[Int] = Some(0)) extends BankAccount {
11+
12+
private def runThreadSafe[A](block: => A): A = this.synchronized(block)
13+
14+
override def closeAccount(): Unit = runThreadSafe(balance = None)
15+
16+
override def getBalance: Option[Int] = runThreadSafe(balance)
17+
18+
override def incrementBalance(increment: Int): Option[Int] = runThreadSafe {
19+
balance flatMap { amount =>
20+
balance = Some(amount + increment)
21+
balance
22+
}
23+
}
24+
}
25+
26+
object Bank {
27+
def openAccount(): BankAccount = Account()
28+
}
29+
30+

exercises/bank-account/src/test/scala/BankAccountTest.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import org.scalatest.{Matchers, FunSuite}
33

44
class BankAccountTest extends FunSuite with Matchers with Conductors with IntegrationPatience {
55
test("open account") {
6-
BankAccount().getBalance should be (Some(0))
6+
Bank.openAccount().getBalance should be (Some(0))
77
}
88

99
test("incrementing and checking balance") {
1010
pending
11-
val acct = BankAccount()
11+
val acct = Bank.openAccount()
1212
acct.getBalance should be (Some(0))
1313
acct.incrementBalance(10) should be (Some(10))
1414
acct.getBalance should be (Some(10))
@@ -18,7 +18,7 @@ class BankAccountTest extends FunSuite with Matchers with Conductors with Integr
1818

1919
test("closed account should hold no balance") {
2020
pending
21-
val acct = BankAccount()
21+
val acct = Bank.openAccount()
2222
acct.closeAccount()
2323
acct.incrementBalance(10)
2424
acct.incrementBalance(10)
@@ -30,7 +30,7 @@ class BankAccountTest extends FunSuite with Matchers with Conductors with Integr
3030
val conductor = new Conductor
3131
import conductor._
3232

33-
val acct = BankAccount()
33+
val acct = Bank.openAccount()
3434

3535
thread("t1") {
3636
acct.incrementBalance(10)
@@ -54,7 +54,7 @@ class BankAccountTest extends FunSuite with Matchers with Conductors with Integr
5454
val conductor = new Conductor
5555
import conductor._
5656

57-
val acct = BankAccount()
57+
val acct = Bank.openAccount()
5858

5959
thread("t1") {
6060
for (a <- 1 to 10)

0 commit comments

Comments
 (0)