Hey everyone, I need a little help with my budget program. Here is my situation, I have 2 tabs in a GUI, one tab adds a transactions when the add button is clicked, and in the other tab displays a table showing all the transactions. In my code, I want it so that when the user chooses a deposit(combo box variable name = cbType, indexnumber for deposit is 0) it will add to the total and when the user chooses withdraw(index number is 1) then it will subtract it from the total. Here is the code.... (note as well, the code also adds a new row to the table)
//add button clicked private class BtnAddActionListener implements ActionListener { public void actionPerformed(ActionEvent arg0) { ((DefaultTableModel)table.getModel()).addRow(new Object[]{ cbMonth.getSelectedItem() + "/" + txtDay.getText() + "/" + cbYear.getSelectedItem(), cbCategory.getSelectedItem(), txtItem.getText(), cbType.getSelectedItem(), txtAmount.getText()}); // calculates the total Double summl = 0.0; for(int i = 0; i < table.getRowCount(); i++){ if(cbType.getSelectedIndex() == 0){ summl=summl+Double.parseDouble(table.getValueAt(i,4).toString()); }else { summl=summl-Double.parseDouble(table.getValueAt(i,4).toString()); } } //displays the total toString(); moneyTotal.setText(String.valueOf(summl));
So when I tested the program with 2 transactions, the first transaction was a deposit and the 2nd transaction was a withdraw. The end product was that both amounts were subtracted from the total. When I did a withdraw first and a deposit second, the amounts were both added together.
Any help would be very grateful thanks!!