2013-05-30 71 views
0

这是用于addnew帐户。打印重复数据集

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

我也有,储蓄和Checkings类

储蓄:

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

检查:

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 
} 

} 

在我的BankAccount类,我有这个:

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; 

} 

当我尝试它时,它会复制数据集: 例如:我输入了帐户名称:John,帐户号:101,初始余额:500,帐户类型:储蓄。

它会喜欢这样的输出:

账户名:约翰 帐号:101 初始余额:500 账户类型:储蓄 帐户名:约翰 帐号:101 初始余额:500 账户类型:储蓄

我找不到错在哪里。

回答

1

btnSaveAActionPerformed您致电list.add(account);然后检查帐户是否是支票或储蓄帐户。在if声明中,你做另一个list.add(account);

这导致帐户的双重增加。

0

**代码是问题的原因,因为您正在列表中添加两次帐户对象。

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

更新你的代码是这样的:

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

谢谢both.Why它仍然是创建一个文件eventhough有例如“bank.txt” 我用这个没有文件名: 'String fileName =“bank.txt”; FileWriter file = null; try { file = new FileWriter(fileName,true); PrintWriter pw = new PrintWriter(file); (BankAccount str:list) { pw.println(str); } pw.flush(); pw.println(“\ n”); (FileNotFoundException ex){ JOptionPane.showMessageDialog(this,“Can not create your account!”);' – dyonas

+1

因为FileWriter会创建一个新文件,如果它不存在 –