-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathCustomer.scala
More file actions
41 lines (30 loc) · 1.08 KB
/
Customer.scala
File metadata and controls
41 lines (30 loc) · 1.08 KB
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
37
38
39
package com.abc
import com.abc.AccountTypes.AccountType
import com.abc.Formatting._
import scala.collection.mutable.ListBuffer
class Customer(val name: String) {
private val accounts: ListBuffer[Account] = ListBuffer()
def openAccount(accountType: AccountType): Account = {
val account = Account(accountType)
accounts += account
account
}
def numberOfAccounts: Int =
accounts.size
def totalInterestEarned: Double =
accounts.map(_.interestEarned).sum
def transfer(from: Account, to: Account, amount: Double) = {
if (!accounts.contains(from))
throw new IllegalArgumentException("Customer must own 'from' account")
if (!accounts.contains(to))
throw new IllegalArgumentException("Customer must own 'to' account")
from.withdraw(amount)
to.deposit(amount)
}
def getStatement: String = {
val totalAcrossAllAccounts = accounts.map(_.sumTransactions).sum
s"""Statement for $name
|${accounts.map(_.getStatement).mkString("\n", "\n\n", "\n")}
|Total In All Accounts ${toDollars(totalAcrossAllAccounts)}""".stripMargin
}
}