Reputation: 95
I am having a hard time doing a simple spreadsheet in Java Using jtable here is my output
balance should be 14,750 - 250 which is 14,500. but the program is returning to -500 which i dont understand .
I tried to tweak my code regarding this but I just tend to mess it all up. here are the codes related to the balance. One thing to note that I used mysql to access the data from my database.
private void setBalance(int balance, int amount, String type) {
if(type.equalsIgnoreCase("expense")) {
balance = getBalance() - amount;
}
else if(type.equalsIgnoreCase("deposit")) {
balance = amount;
}
else{
balance = getBalance() + amount;
}
this.balance = balance;
}
public int getBalance() {
return balance;
}
//constructor for my transaction class
public Transaction(int transactionID, String transactionDetails, String transactionType, String purpose, int amount, Date date) {
this.transactionID = transactionID;
this.transactionDetails = transactionDetails;
this.transactionType = transactionType;
this.purpose = purpose;
this.amount = amount;
this.date = date;
setBalance(getBalance(), amount, transactionType);
}
There here are the codes where I add the values to my table
public void showTable() {
ArrayList<Transaction>list = transactionList();
DefaultTableModel model = (DefaultTableModel) table.getModel();
Object row[] = new Object[7];
for(int i = 0; i < list.size(); i++) {
row[0] = list.get(i).getTransactionID();
row[1] = list.get(i).getDate();
row[2] = list.get(i).getTransactionDetails();
row[3] = list.get(i).getTransactionType();
row[4] = list.get(i).getPurpose();
row[5] = list.get(i).getAmount();
if(i == 0) {
row[6] = list.get(i).getBalance();
}
else {
row[6] = list.get(i).getBalance() - list.get(i-1).getBalance(); //in order to retrieve the previous balance
}
model.addRow(row);
}
}
Upvotes: 0
Views: 187
Reputation: 3205
the program is returning -500 which I don't understand
Ok, initial balance is 15000 then 2 transactions that should deduct 250 each
iteration balList balDisplay 0 15000 15000 set by if(i == 0) {list.get(i).getBalance()} 1 +/-250 14750 set by else {list.get(1).getBalance() - list.get(0).getBalance} 2 250 -500 set by else {list.get(2).getBalance() - list.get(1).getBalance}
iteration 0 indicates that balance
of list[0]
was initially 15000
iteration 1 indicates that balance
of list[1]
was initially +250 or -250, displayed value is 14750 (15000-250). calculation is list.get(1).getBalance() - list.get(0).getBalance
Since the display displays 14750 it must display the calculated amount, but inverted (amount*-1), which is not included in the code snippets presented.
iteration 2: following from 1 we can assume, that balance
of list[2]
is initially +250, balDisplay should be 0 (250-250), but is -500.
Now one could start all over, assuming that initial balance is actually -15000, gets displayed as +15000 based on the reasoning in 1 etc...
This is what I could provide in reasonable time given the available information. The analysis suggests that the displayed balance of row 3 should be zero, but it is -500. There isn't anything available that would explain this. On the other hand, the balance also couldn't be 14500 either, based on the same reasoning.
What occured most suspicious was list.get(i).getBalance() - list.get(i-1).getBalance()
. Why would one deduct one balance from another balance rather than an amount from some balance. And also, why deduct the earlier (i-1) balance from the later one (i)?
Upvotes: 1