-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathAccount.scala
More file actions
66 lines (51 loc) · 1.56 KB
/
Account.scala
File metadata and controls
66 lines (51 loc) · 1.56 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.abc
import scala.collection.mutable.ListBuffer
import java.util.Calendar
import java.util.Calendar._
abstract class Account() {
val transactions: ListBuffer[Transaction] = ListBuffer()
private var total = 0.0
def description: String
def interestEarned: Double
def deposit(amount: Double) {
if (amount <= 0)
throw new IllegalArgumentException("amount must be greater than zero")
transactions += Transaction(amount)
total += amount
}
def withdraw(amount: Double) {
if (amount <= 0)
throw new IllegalArgumentException("amount must be greater than zero")
transactions += Transaction(-amount)
total -= amount
}
def sumTransactions(checkAllTransactions: Boolean = true): Double = total
}
class CheckingAccount extends Account {
def description = "Checking Account"
def interestEarned: Double = {
val amount: Double = sumTransactions()
amount * 0.001
}
}
class SavingAccount extends Account {
def description = "Savings Account"
def interestEarned: Double = {
val amount: Double = sumTransactions()
if (amount <= 1000) amount * 0.001
else 1 + (amount - 1000) * 0.002
}
}
class MaxiSavingAccount extends Account {
def description = "Maxi Savings Account"
def interestEarned: Double = {
val amount: Double = sumTransactions()
val calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, -10)
val tenDaysAgo = calendar.getTime
if (!transactions.exists(t => t.date.after(tenDaysAgo) && t.amount < 0))
amount * 0.05
else
amount * 0.001
}
}