-
Notifications
You must be signed in to change notification settings - Fork 0
/
Account.java
115 lines (97 loc) · 3.19 KB
/
Account.java
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
//package com.abc;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.junit.Assert;
public class Account {
public static final int CHECKING = 0;
public static final int SAVINGS = 1;
public static final int MAXI_SAVINGS = 2;
private final int accountType;
public List<Transaction> transactions;
public Account(int accountType) {
this.accountType = accountType;
this.transactions = new ArrayList<Transaction>();
}
public void deposit(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("amount must be greater than zero");
} else {
transactions.add(new Transaction(amount));
}
//System.out.println("deposit: " +amount +", sum: " + sumTransactions());
}
public void withdraw(double amount) {
if (amount <= 0) {
throw new IllegalArgumentException("amount must be greater than zero");
} else {
transactions.add(new Transaction(-amount));
}
//System.out.println("withdraw: " +amount +", sum: " + sumTransactions());
}
public double interestEarned() {
double amount = sumTransactions();
switch(accountType){
//case CHECKING:
// return amount * 0.001;
case SAVINGS:
if (amount <= 1000)
return amount * 0.001;
else
return 1 + ((amount-1000) * 0.002);
// case SUPER_SAVINGS:
// if (amount <= 4000)
// return 20;
case MAXI_SAVINGS:
if(checkWithdrawDates() == true){
return amount * 0.05;
}
else
{
if (amount <= 1000)
return amount * 0.02;
else if (amount > 1000 && amount< 2000)
return 20 + ((amount-1000) * 0.05);
else if (amount > 2000)
return 70 + ((amount-2000) * 0.1);
}
default:
return amount * 0.001;
}
}
public double sumTransactions() {
return checkIfTransactionsExist(true);
}
private double checkIfTransactionsExist(boolean checkAll) {
double amount = 0.0;
//System.out.print("check : ");
for (Transaction t: transactions){
Assert.assertNotNull(t);
amount += t.amount;
//System.out.print(t.amount);
}
return amount;
}
private boolean checkWithdrawDates() {
//System.out.print("check : ");
int count=0;
for (Transaction t: transactions){
// should check the dates
int timediff = (int)((t.getDate().getTime() - (new java.util.Date().getTime())) / (1000 * 60 * 60 * 24));
System.out.print("check date : " + timediff);
if(t.amount < 0 && timediff < 10)
return false;
if(t.amount >0){
count++;
}
}
if(count == transactions.size())
return false;
else
return true;
}
public int getAccountType() {
return accountType;
}
}