我在向arraylist添加对象时遇到问题,我似乎无法找到问题所在。当我添加新帐户时,它会删除上一个对象。请帮助!
public class bankmain {
static File f=new File("C:/Users/Guest/workspace/bank.txt");
private static ArrayList<Account> accountList = new ArrayList<>();
private static Account selectedAccount;
private static Account selectedAccount2;
private static boolean flag = false;
private static boolean fly=true;
private static double amount;
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
//readFile();
System.out.println("Enter the option for the operation you need:");
System.out.println("1-List of accounts");
System.out.println("2-Create new account");
System.out.println("3-Delete account");
System.out.println("4-Exit");
System.out.println("5-Deposit");
System.out.println("6-Withdraw");
System.out.println("7-Transfer");
while (fly) {
int choice = input.nextInt();
switch (choice) {
case 1:
// List
for (int i = 0; i < accountList.size(); i++) {
System.out.println("Owner:"+accountList.get(i).getOwner()+"-Account Number:"+accountList.get(i).getAccNumber()+"-Balance:"+accountList.get(i).getBalance());
}
break;
case 2:
// Create
int accNo;
Double bal;
String own;
System.out.println("Enter owners name: ");
own=scan.next();
System.out.println("Enter account number: ");
accNo = scan.nextInt();
System.out.println("Enter initial balance: ");
bal = scan.nextDouble();
System.out.println("Owner:" + own+ " Balance:" + bal + " Account Number:"+accNo );
Account k=new Account(bal, accNo, own);
accountList.add(k);
break;
}
}
}
}
输入您需要的操作选项:
1. List of accounts
2. Create new account
3. Delete account
4. Exit
5. Deposit
6. Withdraw
7. Transfer
Enter owners name:
Anna
Enter account number:
1
Enter initial balance:
1200
Owner:Anna Balance:1200.0 Account Number:1
2
Enter owners name:
Lana
Enter account number:
2
Enter initial balance:
3000
Owner:Lana Balance:3000.0 Account Number:2
1
Owner:Lana-Account Number:2-Balance:3000.0
Owner:Lana-Account Number:2-Balance:3000.0
答案 0 :(得分:3)
你应该查看你的Account
课程。您可能将属性声明为静态,这意味着所有Account
实例都具有相同的属性。删除static关键字。
答案 1 :(得分:0)
您的代码似乎没有问题。我已经运行了代码,我可以看到正确添加的帐户。
这是Account class
定义:
class Account {
private double balance;
private int accNumber;
private String owner;
public Account(Double bal, int accNo, String own) {
// TODO Auto-generated constructor stub
this.balance = bal;
this.accNumber = accNo;
this.owner = own;
}
public int getAccNumber() {
return accNumber;
}
public void setAccNumber(int accNumber) {
this.accNumber = accNumber;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getBalance() {
return balance;
}
}
结果如下:
输入您需要的操作选项:
1 - 账户清单
2 - 创建新帐户
3 - 删除帐户
4-退出
5存款
-6-提款
7-转移
2
输入所有者名称:
艾伦
输入帐号:
1
输入初始余额:
10000
所有者:Alan Balance:10000.0帐号:1
2
输入所有者名称:
巴兰
输入帐号:
2
输入初始余额:
20000
所有者:Balan余额:20000.0帐号:2
2
输入所有者名称:
结肠
输入帐号:
3
输入初始余额:
3000
所有者:冒号余额:3000.0帐号:3
1
所有者:Alan-Account Number:1-Balance:10000.0
所有者:Balan-账号:2-Balance:20000.0
所有者:冒号 - 帐号:3 - 余额:3000.0