-
Notifications
You must be signed in to change notification settings - Fork 36
/
BankAccountPublisher.scala
36 lines (29 loc) · 1013 Bytes
/
BankAccountPublisher.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package week4
class BankAccountPublisher extends Publisher {
//we publish everytime we change the state of the bank account
private var balance = 0
//accessor method
def currentBalance = balance
def deposit(amount: Int): Unit = {
if (amount > 0) balance = balance + amount
publish() // <--
}
def withdraw(amount: Int): Unit = {
if (0 < amount && amount <= balance) {
balance = balance - amount
publish() // <--
} else throw new Error("insufficient funds")
}
}
//consolidator is a subscriber to maintain the total balance of a list of account
class Consolidator(observed: List[BankAccountPublisher]) extends Subscriber {
observed.foreach(_.subscribe(this)) //subscribe to all BankAccounts
private var total: Int = _
compute()
private def compute() = {
total = observed.map(_.currentBalance).sum
}
//everytime something changes, you compute
def handler(pub: Publisher) = compute()
def totalBalance = total
}