java - 将对象添加到数组列表,银行帐户

标签 java 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

最佳答案

您应该查看您的 Account 类。您可能将属性声明为静态,这意味着所有 Account 实例都将具有相同的属性。删除 static 关键字。

关于java - 将对象添加到数组列表,银行帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27616651/

相关文章:

java - Derby 错误 : Could not find or load main class org. apache.derby.drda.NetworkServerControl

Java排序ArrayList<Integer>

Android,存储自定义ArrayList

java - ArrayList 问题 - 计算航空里程份额

javascript - Spring RESTful GET方法具有相同的参数和不同的返回类型

java - java中自定义异常中的父类(super class)构造函数

java - spring + tomcat + axis2 == jax-ws 网络服务?

java - Android 查找部分匹配的数组索引值

java - 在方法参数列表中使用ArrayList或List

Java RXTX - 通过串行通信检测正确的设备