Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

java - 如何使用jtable在JAVA中创建简单的电子表格?(How to create a simple spreadsheet in JAVA using jtable?)

I am having a hard time doing a simple spreadsheet in Java Using jtable here is my output (我很难用Java做一个简单的电子表格,使用jtable是我的输出) 我的程序

balance should be 14,750 - 250 which is 14,500. (余额应为14,750-250,即14,500。) but the program is returning to -500 which i dont understand . (但是程序返回到我不明白的-500。)

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. (需要注意的一件事是,我使用mysql从数据库访问数据。)

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

    }
  ask by Joshua Salcedo translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

the program is returning -500 which I don't understand (程序返回-500,我不明白)

Ok, initial balance is 15000 then 2 transactions that should deduct 250 each (好吧,初始余额为15000,则2笔交易应分别扣除250笔)

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 (迭代0表示list[0] balance最初是15000)

iteration 1 indicates that balance of list[1] was initially +250 or -250, displayed value is 14750 (15000-250). (迭代1表示list[1] balance最初为+250或-250,显示值为14750(15000-250)。) calculation is list.get(1).getBalance() - list.get(0).getBalance (计算是list.get(1).getBalance() - list.get(0).getBalance)

  • assuming it was +250, calculation gives -14750 (假设是+250,计算得出-14750)
  • assuming it was -250, calculation gives -15250 (假设是-250,计算得出-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. (由于显示屏显示14750,因此它必须显示计算出的数量,但是要反转(数量* -1),这不包括在显示的代码片段中。)

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. (迭代2 :从1开始,我们可以假设list[2] balance最初为+ 250,balDisplay应该为0(250-250),但为-500。)

This is the best what I could provide given the available information. (给定可用信息,这是我能提供的最好的方法。) The analysis leads to the conclusion that the displayed balance of row 3 should be zero, but it is -500. (分析得出的结论是,第3行的显示余额应为零,但应为-500。) There isn't anything available that would explain this. (没有任何可用的方法可以解释这一点。) On the other hand, the balance also couldn't be 14500 either. (另一方面,余额也不能是14500。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...