Java 打印重复的数据集
这是给新帐户的Java 打印重复的数据集,java,swing,Java,Swing,这是给新帐户的 private void btnSaveAActionPerformed(java.awt.event.ActionEvent evt) { BankAccount account = new BankAccount(); ButtonGroup bg = new ButtonGroup(); bg.add(rad_savings); bg.add(rad_che
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,账户类型:储蓄
它将输出如下所示:
if(rad_savings.isSelected()){
//account = new SavingsAccount();
account.setAccountType("Savings");
}
else
{
//account = new CheckingAccount();
account.setAccountType("Checking");
}
list.add(account);
户名:约翰
账号:101
初始余额:500
账户类型:储蓄
户名:约翰
账号:101
初始余额:500
账户类型:储蓄
我找不到问题所在。在
btnsaveactionperformed
中,您调用列表。添加(帐户)
然后检查该帐户是支票帐户还是储蓄帐户。在if
语句中,您可以同时执行另一个列表。添加(帐户)代码>
这会导致双重添加帐户。**代码是问题的原因,因为您在列表中两次添加帐户对象
**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;尝试{file=newfilewriter(fileName,true);PrintWriter pw=newprintwriter(file);for(BankAccount str:list){pw.println(str);}pw.flush();pw.println(“\n”);}catch(FileNotFoundException ex){JOptionPane.showMessageDialog(这是“无法创建您的帐户!”);
,因为如果文件不存在,FileWriter将创建一个新文件