double interest_pos = 1.0003, interest_neg = -0.002;
double balance = CustomersList.getItem(i).get_balance();
boolean transacted = false;
for ( int mth = 1; mth <= 31; mth++ )
{
S开发者_如何学运维ystem.out.println(balance);
for ( int j = 1; j <= numTrans; j++ )
{
if ( CustomersList.getItem(i).get_accountid() == TransList.getItem(j).get_accountid() )
{
String tempdate = TransList.getItem(j).get_date();
date = tempdate.split(delimiter);
if ( TransList.getItem(j).get_type().equals("credit") && Integer.parseInt(date[0]) == mth && !transacted )
{
balance += TransList.getItem(j).get_amount();
transacted = true;
if ( balance >= 0 ) balance *= interest_pos;
else balance *= interest_neg;
}
else if ( TransList.getItem(j).get_type().equals("credit") && Integer.parseInt(date[0]) == mth && transacted )
{
balance += TransList.getItem(j).get_amount();
}
else if ( TransList.getItem(j).get_type().equals("debit") && Integer.parseInt(date[0]) == mth && transacted )
{
System.out.println( "Deducted " + TransList.getItem(j).get_amount());
balance -= TransList.getItem(j).get_amount();
}
else if ( TransList.getItem(j).get_type().equals("debit") && Integer.parseInt(date[0]) == mth && !transacted )
{
balance -= TransList.getItem(j).get_amount();
transacted = true;
if ( balance >= 0 ) balance *= interest_pos;
else balance *= interest_neg;
}
else
{
if ( balance >= 0 ) balance *= interest_pos;
else balance *= interest_neg;
}
}
}
System.out.println ("Day " + mth + " Balance " + balance );
transacted = false;
Above is part of my code. The rest of my code reads in data from a text file, and everything works well until the part where the double value is supposed to be subtracted by 500.0.
Day 22 Balance 225.86301881168654
Day 23 Balance 0.5478672869645392
After Day 22, it's supposed to be -275.54...... but instead it shows what it's showing now. This is the last component of my assignment and I'm stuck here for hours looking for solutions! Thank you in advance for any help.
EDIT: I've put in more of my code, is it an issue with how java deals with mathematical operations in a binary form that when 225.86301881168654 - 500.0 it becomes the weird result i get?
I don't know what the exact issue is here, but use BigDecimal
(instead of double
) when working with money.
I suggest you step through your code using a debugger and you would see how your values are actually changing.
Your negative interest is interest_neg = -0.002. which you multiply by your amount.
Say you have the following situation
double balance = -100;
if ( balance >= 0 ) balance *= interest_pos;
else balance *= interest_neg; // balance = -100 * -0.002 = +0.2
A negative interest doesn't make any sense of this value as you can see. If I had a million dollar debt, the next month I would have $200. The more in debt I am the more money I will have next month.
So whenever you have a negative balance you are making it positive again. Perhaps your negative interest should be 1.002, similar to your positive interest.
else balance *= interest_neg; // balance = -100 * 1.002 = -100.2
Hard to say what happen when we don't see full of your code but this changes should help to you:
double balance;
System.out.println(balance);
balance -= TransList.getItem(j).get_amount();
System.out.println(balance);
transacted = true;
if ( balance >= 0 ) balance *= interest_pos;
else balance *= interest_neg;
System.out.println(balance);
I ran myself pretty ragged over this whole "Negative Double" variable issue, as the sum amount was definitely negative. Performing IF statements that if the 2nd variable was > the 1st variable it would flip the variables and then multiply by -1. That didn't work though.... The doubles actually calculate perfectly...I had a BOOLEAN statement lower in the code that was only looking for positive amounts. It saved me from having to rewrite dozens of variables. I hope this helps someone.
精彩评论