2013-05-30 97 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會創建一個新文件,如果它不存在 –