dyonas
dyonas

Reputation: 1

Prints Duplicate set of Data

This is for addnew account.

private void btnSaveAActionPerformed(java.awt.event.ActionEvent evt)  
{                                         
    BankAccount account = new BankAccount();
    ButtonGroup bg = new ButtonGroup();
    bg.add(rad_savings);
    bg.add(rad_checking);

    account.setAccountName(txt_accountname.getText());
    account.setAccountNo(txt_accountnumber.getText());
    account.setBalance(Double.parseDouble(txt_initialbalance.getText()));

    list.add(account);


    if(rad_savings.isSelected()){
        //account = new SavingsAccount();
        account.setAccountType("Savings");
        list.add(account);
    }
    else
    { 
        //account = new CheckingAccount();
        account.setAccountType("Checking");
        list.add(account);
    }
}

I also have, Savings and Checkings Class

Savings:

public class SavingsAccount extends BankAccount {

public SavingsAccount(){

};

public SavingsAccount(String accountNo, String accountName, double initBalance) {
    super(accountNo, accountName, initBalance);
}

public SavingsAccount(String accountNo, String accountName) {
    super(accountNo, accountName);
}
}

Checking:

public class CheckingAccount extends BankAccount  {
private double overdraftProtection;


public CheckingAccount(){

};

public CheckingAccount(String accountNo, String accountName, 
        double initBalance) {
    super(accountNo, accountName, initBalance);
}

public CheckingAccount(String accountNo, String accountName) {
    super(accountNo, accountName);
}

public double getOverdraftProtection() {
    return overdraftProtection;
}

public void setOverdraftProtection(double overdraftProtection) {
    this.overdraftProtection = overdraftProtection;
}

public void withdraw(double amount) {
    // TODO: code for withdrawal
}

}

In my BankAccount Class, i have this:

public class BankAccount {
private String accountNo;
private String accountName;
protected double balance; 
private String accountType;

public String toString(){
    return "Account name: " +  accountName + "" + System.getProperty("line.separator") + 
        "Account Number: " + accountNo + "" +System.getProperty("line.separator")+ 
        "Balance: " + balance + "" + System.getProperty("line.separator")+ 
        "Account Type: " + accountType;

}

When I'm trying it, it replicates the set the of data: For example: I've entered Account Name: John, Account No: 101, Initial Balance: 500, Account Type: Savings.

It will output like this:

Account Name: John Account No: 101 Initial Balance: 500 Account Type: Savings Account Name: John Account No: 101 Initial Balance: 500 Account Type: Savings

I can't find what is wrong.

Upvotes: 0

Views: 71

Answers (2)

Juned Ahsan
Juned Ahsan

Reputation: 68715

**code is the cause of your problem as you are adding the account object twice in the list.

   **list.add(account);**

if(rad_savings.isSelected()){
    //account = new SavingsAccount();
    account.setAccountType("Savings");
    **list.add(account);**
}
else
{ 
    //account = new CheckingAccount();
    account.setAccountType("Checking");
    **list.add(account);**
}

Update your code like this:

if(rad_savings.isSelected()){
    //account = new SavingsAccount();
    account.setAccountType("Savings");
}
else
{ 
    //account = new CheckingAccount();
    account.setAccountType("Checking");
}
list.add(account);

Upvotes: 0

Steve
Steve

Reputation: 7271

In btnSaveAActionPerformed you call list.add(account); and then check whether the account is a checking or savings account. In the if statements of both of those you do another list.add(account);

This is causing the double addition of the account.

Upvotes: 1

Related Questions