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