【问题标题】:Prints Duplicate set of Data打印重复的数据集
【发布时间】:2013-05-30 07:44:10
【问题描述】:

这是用于添加新帐户的。

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

我也有,储蓄和支票课

节省:

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,账户类型:Savings。

会这样输出:

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

我找不到问题所在。

【问题讨论】:

    标签: java swing


    【解决方案1】:

    btnSaveAActionPerformed 中调用list.add(account);,然后检查该帐户是支票帐户还是储蓄帐户。在这两个的if 语句中,您执行另一个list.add(account);

    这导致帐户被双重添加。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 谢谢。为什么它仍然在创建一个文件,即使没有文件名,例如“bank.txt”我用过这个:String fileName = "bank.txt"; FileWriter file = null; try { file = new FileWriter(fileName,true); PrintWriter pw = new PrintWriter(file); for(BankAccount str: list) { pw.println(str); } pw.flush(); pw.println("\n"); } catch(FileNotFoundException ex) { JOptionPane.showMessageDialog(this, "Can't create your account!");
      • 因为FileWriter如果不存在会创建一个新文件
      猜你喜欢
      • 2019-08-06
      • 2017-12-16
      • 1970-01-01
      • 2014-04-01
      • 2016-07-16
      • 1970-01-01
      • 2014-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多