0

I am having a hard time doing a simple spreadsheet in Java Using jtable here is my output My program

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);
        }

    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Maybe [this](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=7&ved=2ahUKEwixhf-K6JLmAhVTecAKHV7NCpwQFjAGegQIAhAB&url=https%3A%2F%2Fwww2.cs.duke.edu%2Fcourses%2Ffall06%2Fcps100%2Fcode%2Fspread%2FSpreadSheet.java&usg=AOvVaw2VGv2uM96PMYM0dz9aZADp) is of help. Did you try to [debug](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems?noredirect=1&lq=1) the thing? – Curiosa Globunznik Nov 30 '19 at 20:52
  • I did try but still no use :C – Joshua Salcedo Dec 01 '19 at 12:13
  • I tried to pluck the thing apart to some extent, arrived at the conclusion that nothing indicates, that the displayed value should be 14500. It indicates, it should be zero, when in fact it is -500. But maybe this gives further hints. It would be good to know, which values **actually** are contained in `list` (which I propose to rename to `transactions`, and to [debug](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems?noredirect=1&lq=1) it afterwards). – Curiosa Globunznik Dec 01 '19 at 13:43

1 Answers1

1

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

  • assuming it was +250, calculation gives -14750
  • assuming it was -250, calculation gives -15250

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)?

Curiosa Globunznik
  • 3,129
  • 1
  • 16
  • 24
  • First of all I will have to clarify that we deduct the amount to balance if and only if the type of transaction is exepense. So are you suggesting that `list.get(i).getBalance() - list.get(i-1).getBalance()` should be replaced by `list.get(i).getBalance() - list.get(i-1).getAmount()`? – Joshua Salcedo Dec 02 '19 at 14:39
  • 1
    I really don't know enough about the implementation to come up with suggestions. I was just wondering. What do you think about the "14500" is impossible conclusion btw? But you know, my intuition: you start with a balance, add or subract an amount -> next balance. just my personal 2 cents. – Curiosa Globunznik Dec 02 '19 at 14:40
  • Hmm ok well here is the source code : https://files.fm/u/zjqymyta its in the zip file. I just dont understand why my balance is not properly showing up. I set up my get and set method properly with a condition that if type is expense then the amount should be negative. – Joshua Salcedo Dec 02 '19 at 14:45