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