interest calculation
Here's what I got. A linkedList of objects of Customer class of different account types (Savings,current and fixed). Each Customer object has a LinkedList of transactions(another class) as an attribute. 2 types of transactions can be made i.e Debit(withdrawal) or Credit(deposit). Given: A savings account can go into negative while the other two accounts can not. No debit transactions (no withdrawals) from the Fixed account allowed.
if the account balance is positive then the interest rate is 0.0003 while if the account balance is negative (only possible for Saving account)the rate is -0.002. The interest is calculated as follows:
For positive interest, it is based on the money that has been in the account for the last 24 hours (i.e. from midnight to midnight). For example, if you have $100 at hour 0 but you have withdrawn $50 at hour 1 and deposited back $50 at hour 2, you will be seen as having only $50 staying in your account for 24 hours at end of the day (hour 24). At hour 24, the money in your account will be $100 plus the daily interests computed according to $50.
For negative interest, it is based on the sum of the largest negative money that you owe the bank on that day. If you borrow money from the bank, they will charge you interest even if you return the money 1 minute later. For example, if your saving account has $100 at hour 0 but you withdraw $200 at hour 22 and then deposit $1000 back at hour 23. You will not be paid any positive interest by today mid-night but will be charged for negative interest for borrow $100 from the bank for today.
For a savings account with initially $566.00 and transactions on the account are as follows: debit:50 (date:11-09-2008), debit:500(15-09-2008); credit:200(22-09-2008); debit:500(23-09-2008 ).
the sample calculation is given as:
(((566*1.0003^10-50)*1.0003^4-500)*1.0003^8+200-500)*1.002^8 ~= 286.17.
I get some figure of the order of 1377.68 which obviously doesn't match.
Here's what i have for the savings account but i'm pretty sure it's wrong. My question is how to calculate the interest when looping through the transactions for each customer. my calculation is wrong. so i would appreciate it if someone can help me fix the logic
public void update(double rate){ // Savings account interest calc
Transactions ctr = new Transactions();
Node<Transactions> counter = new Node<Transactions>(ctr);
counter=this.trans.head;
int i=0;
double negRate = -0.002;
开发者_如何转开发 double posRate = 0.0003;
double updatedBal = this.get_balance();
while(counter!=null){
if (updatedBal >0){
if(trans.getItem(i).AccType.equals("Crebit")){
double exponent = Double.parseDouble(trans.getItem(i).get_Date().substring(0, 2));
updatedBal= (updatedBal*(Math.pow((1+ posRate),exponent-1))+trans.getItem(i).get_Amount());
}
else if(trans.getItem(i).AccType.equals("Debit")){
double exponent = Double.parseDouble(trans.getItem(i).get_Date().substring(0, 2));
updatedBal= (updatedBal*(Math.pow((1+ posRate),exponent-1))-trans.getItem(i).get_Amount());
}
}
else
{
if(trans.getItem(i).AccType.equals("Crebit")){
double exponent = Double.parseDouble(trans.getItem(i).get_Date().substring(0, 2));
updatedBal= (updatedBal*(Math.pow((1+ negRate),exponent-1))+trans.getItem(i).get_Amount());
}
else if(trans.getItem(i).AccType.equals("Debit")){
double exponent = Double.parseDouble(trans.getItem(i).get_Date().substring(0, 2));
updatedBal= (updatedBal*(Math.pow((1+ negRate),exponent-1))-trans.getItem(i).get_Amount());
}
}
counter=counter.next;
}
this.set_balance(updatedBal);
}
Your code has trans.getItem(i).AccType.equals("Crebit")
in two places. Presumably that should be Credit. If the value of the AccType field of the transaction items is using the correct spelling, then your if
blocks won't "see" the credit transactions when computing the interest and that could lead to a wrong answer.
精彩评论