-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathMyAccount.scala
More file actions
136 lines (117 loc) · 4.58 KB
/
MyAccount.scala
File metadata and controls
136 lines (117 loc) · 4.58 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.abc
import scala.collection.mutable.ListBuffer
import com.abc.AccountType._
import java.util.Date
class MyAccount(val accountType: AccountType, var transactions: ListBuffer[MyTransaction] = ListBuffer[MyTransaction]()) {
var balance: Double = 0.0d
def deposit(amount: Double) {
require(amount>0)
transactions += new MyTransaction(amount)
balance+=amount;
}
def deposit(amount: Double, transactionDate: Date= new Date): Unit ={
require(amount>0)
transactions += new MyTransaction(amount,transactionDate)
balance+=amount;
}
def withdraw(amount: Double) {
require(amount>0)
transactions += new MyTransaction(-amount)
balance-=amount;
}
def withdraw(amount: Double, transactionDate: Date= new Date) : Unit= {
require(amount>0)
transactions += new MyTransaction(-amount, transactionDate)
balance-=amount;
}
def interestEarned: Double = {
//val amount: Double = sumTransactions()
accountType match {
case SAVINGS =>
savingsAccountInterest
case MAXI_SAVINGS =>
maxiSavingsAccountInterest
case CHECKING =>
checkingAccountInterest
}
}
def checkingAccountInterest: Double = {
var totalCheckingInterest=0d;
val tArray: Array[MyTransaction]=transactions.sortBy(_.transactionDate).toArray
var balance : Double =0.0d;
if(tArray.size>=2){
for(i<- 1 until tArray.size-1){
balance+=tArray(i-1).amount
val days : Long = (tArray(i).transactionDate.getTime-tArray(i-1).transactionDate.getTime)/(24 * 60 * 60 * 1000);
totalCheckingInterest += balance * 0.001*(days/365d);
}
}
//last transaction to today/report date interest
if(tArray.size>=1){
balance+=tArray(tArray.size-1).amount
val d : Long = ((new Date).getTime-tArray(tArray.size-1).transactionDate.getTime)/(24 * 60 * 60 * 1000);
totalCheckingInterest += balance * 0.001*(d/365d);
}
totalCheckingInterest
}
def savingsAccountInterest: Double = {
var totalSavingsInterest=0d;
val tArray: Array[MyTransaction]=transactions.sortBy(_.transactionDate).toArray
var balance : Double =0.0d;
if(tArray.size>=2){
for(i<- 1 until tArray.size-1){
balance+=tArray(i-1).amount
val d : Long = (tArray(i).transactionDate.getTime-tArray(i-1).transactionDate.getTime)/(24 * 60 * 60 * 1000);
if (balance <= 1000) {
totalSavingsInterest += balance * 0.001*(d/365d);
}else {
totalSavingsInterest += 1*(d/365d) + (balance - 1000) * 0.002*(d/365d)
}
}
}
//last transaction to today/report date interest
if(tArray.size>=1){
balance+=tArray(tArray.size-1).amount
val d : Long = ((new Date).getTime-tArray(tArray.size-1).transactionDate.getTime)/(24 * 60 * 60 * 1000);
if (balance <= 1000) {
totalSavingsInterest += balance * 0.001*(d/365d);
}else {
totalSavingsInterest += 1*(d/365d) + (balance - 1000) * 0.002*(d/365d)
}
}
totalSavingsInterest
}
def maxiSavingsAccountInterest: Double = {
var totalSavingsInterest=0d;
val tArray: Array[MyTransaction]=transactions.sortBy(_.transactionDate).toArray
var balance : Double =0.0d;
if(tArray.size>=2){
for(i<- 1 until tArray.size-1){
balance+=tArray(i-1).amount
val d : Long = (tArray(i).transactionDate.getTime-tArray(i-1).transactionDate.getTime)/(24 * 60 * 60 * 1000);
if (balance <= 1000) {
totalSavingsInterest += balance * 0.02*(d/365d);
}else if(balance>1000 && balance<=2000){
totalSavingsInterest += 20*(d/365d) + (balance - 1000) * 0.05*(d/365d)
}else{
totalSavingsInterest += 70*(d/365d) + (balance - 2000) * 0.1*(d/365d)
}
}
}
//last transaction to today/report date interest
if(tArray.size>=1){
balance+=tArray(tArray.size-1).amount
val d : Long = ((new Date).getTime-tArray(tArray.size-1).transactionDate.getTime)/(24 * 60 * 60 * 1000);
if (balance <= 1000) {
totalSavingsInterest += balance * 0.02*(d/365d);
}else if(balance>1000 && balance<=2000){
totalSavingsInterest += 20*(d/365d) + (balance - 1000) * 0.05*(d/365d)
}else{
totalSavingsInterest += 70*(d/365d) + (balance - 2000) * 0.1*(d/365d)
}
}
totalSavingsInterest
}
//def sumTransactions(checkAllTransactions: Boolean = true): Double = transactions.map(_.amount).sum
def sumTransactions(): Double = balance
}