简体   繁体   English

如何继续从另一个类更新arrayList?

[英]How keep updating an arrayList from another class?

I am creating a bank demo and have a menu choice where the user has an option to open an account. 我正在创建一个银行演示,并有一个菜单选项,用户可以选择打开一个帐户。 When the user chooses that option (from the home page class), I call another class called account which has set and get methods for the first and last name, and account number. 当用户选择该选项(从主页类)时,我调用另一个类称为account,该类已经设置并获取了名字和姓氏以及帐号的方法。 Then I have an accountDemo that tells the user to enter their information and it stores it in the arrayList and goes back to the home. 然后,我有一个accountDemo,它告诉用户输入他们的信息,并将其存储在arrayList中,然后返回家中。 The problem is when I choose that same choice again and enter new information, the previous old information in the arrayList is not there, probability overridden. 问题是当我再次选择相同的选择并输入新信息时,arrayList中的先前旧信息不存在,概率被覆盖。 How can I keep updating the arrayList when a user chooses the option to open a new account. 当用户选择打开新帐户的选项时,如何继续更新arrayList。 I saw in order posts they had an ArrayList getList method where it returns the arrayList, but I'm still stuck. 我按顺序看到帖子中有一个ArrayList getList方法,该方法返回arrayList,但我仍然遇到问题。 Here is my code; 这是我的代码; Thanks 谢谢

public class account 
{   
    private String firstName, lastName;
    private int accountNumber;

    public account()
    {
        firstName = " ";
        lastName = " ";
        accountNumber = 0;
    }

        public account(String firstName, String lastName, int accountNumber)
        {
            this.firstName = firstName;
            this.lastName = lastName;
            this.accountNumber = accountNumber;
        }

        public void setFirstName(String firstName)
        {
            this.firstName = firstName;
        }

        public void setLastName(String lastName)
        {
            this.lastName = lastName;
        }

        public void setAccountNumber(int accountNumber)
        {
            this.accountNumber = accountNumber;
        }

        public String getFirstName()
        {
            return firstName;
        }

        public String getLastName()
        {
            return lastName;
        }

        public int getAccountNumber()
        {
            return accountNumber;
        }

        public String toString()
        {
            return "Account Information [First Name = " + firstName + " Last Name = " + lastName + " account Number = " + accountNumber + "]"; 
        }


    }

    import java.util.ArrayList;
    import java.util.Scanner;

    public class accountDemo 
    {
        private ArrayList<account> arry = new ArrayList<account>();

        public void makeNewAccount()
        {
            String firstName, lastName;
            int accountNumber;

            Scanner keyboard = new Scanner(System.in);
            System.out.println("Welcome to opening a new account");
            System.out.println("--------------------------------");
            System.out.println("Please enter your first name");
            firstName = keyboard.nextLine();
            System.out.println("Please enter you last name");
            lastName = keyboard.nextLine();
            System.out.println("Please enter a 4 digit account number");
            accountNumber = keyboard.nextInt();

            String validityOfAccountNum = Integer.toString(accountNumber); // converting the integer account number to a string so we can check the length of the user input

            if(validityOfAccountNum.length() != 4) // checking if the length of the given length is greater or less than 4 digits
            {
                System.out.println("The length of your account number is invalid");
                System.out.println("Please try again");
                makeNewAccount();
            }
            else
            {
                //account info = new account(firstName, lastName, accountNumber); // creating a new account that passes in first, last name, and account number
                System.out.println("Storing account number......");
                System.out.println("You have successfully opened a new account");

                /**
                arry = new ArrayList<account>(); // creating a new array list
                arry.add(new account(firstName, lastName, accountNumber)); // adding the given information to the list

                for(int i = 0; i < arry.size(); i++)
                {
                    System.out.println("In the list is");
                    System.out.println(arry.get(i)); 
                }
                **/

                System.out.println("--------------------------------");
                System.out.println("Testing");
                accountDemo list = new accountDemo();
                System.out.println(list.getList());
                System.out.println("--------------------------------");
                System.out.println("--------------------------------");
                System.out.println("Testing");
                storeInfo(firstName, lastName, accountNumber);


                System.out.println("Going to Home Page......");
                System.out.println("--------------------------------");
                ATMHomePage home = new ATMHomePage();
                home.menuChoice();

            }
        }

        public ArrayList<account> getList()
        {
            return this.arry;
        }

        public ArrayList<account> storeInfo(String firstName, String lastName, int accountNumber)
        {
            arry.add(new account(firstName,lastName,accountNumber));
            for(int i = 0; i < arry.size(); i++)
            {
                System.out.println("In the list is");
                System.out.println(arry.get(i)); 
            }
            return arry;
        }


    }

import java.util.Scanner;
public class ATMHomePage 
{
    public static void main(String [] args)
    {
        ATMHomePage homePage = new ATMHomePage();
        homePage.menuChoice();
    }

    public void menuChoice()
    {
        Scanner keyboard = new Scanner(System.in);
        int userInputChoice;

        System.out.println("Welcome to the ATM" + "\n");
        System.out.println("Please choose from one of the following choices:");
        System.out.println("1. Deposit " + "\n"
                        + "2. Withdraw " + "\n"
                        + "3. Check Balance " + "\n"
                        + "4. Open a New Account " + "\n"
                        + "5. Quit" + "\n");

        userInputChoice = keyboard.nextInt();

        if(userInputChoice == 1)
        {
            Deposit deposit = new Deposit();
            deposit.Introduction();
        }
        else if(userInputChoice == 2)
        {
            // go to a new class
        }
        else if(userInputChoice == 3)
        {
            // go to a new class
        }
        else if(userInputChoice == 4)
        {
            accountDemo info = new accountDemo();
            info.makeNewAccount();
        }
        else if(userInputChoice == 5)
        {
            System.out.println("Good-Bye");
            System.exit(0);
        }
        else
        {
            System.out.println("You have entered a wrong chocie");
            System.out.println("Please try again");
            System.out.println("--------------------------------------------------");
            menuChoice();
        }
    }

}

You keep creating a new accountDemo() object. 您继续创建一个新的accountDemo()对象。 Each time you do that it becomes a new object. 每次您执行此操作都会成为一个新对象。

You need to create the object once, and reference it again and again. 您需要创建一个对象,然后一次又一次地引用它。 Look into a concept called variable scoping. 研究一个称为变量作用域的概念。

You should use proper Java syntax and capitalize all your class names: 您应该使用正确的Java语法并大写所有类名:

public class Account

public class AccountDemo

remove accountDemo info = new accountDemo() from choice 4. 从选项4中删除accountDemo info = new accountDemo()。

else if(userInputChoice == 4)
{
    info.makeNewAccount();
}

Put that statement at the start of the public class ATMHomePage. 将该语句放在公共类ATMHomePage的开头。

public class ATMHomePage 
{
     AccountDemo info = new AccountDemo();
...

This will create one and only one AccountDemo object that can be referenced within the ATMHomePage class. 这将创建一个并且只有一个AccountDemo对象,该对象可以在ATMHomePage类中引用。

Remove accountDemo list = new accountDemo(); 删除accountDemo列表= new accountDemo(); for your AccountDemo class in the method makeNewAccount(). makeNewAccount()方法中的AccountDemo类。 Change list.getList() to just getList(). 将list.getList()更改为getList()。

System.out.println("Testing");

System.out.println(getList());

I would even go a step further and put listing the account information into its own method. 我什至更进一步,将帐户信息列出到自己的方法中。

    else if(userInputChoice == 4)
    {
        // This line should not occur here since you are creating a new object
        // accountDemo info = new accountDemo();         
        // every time a new. You should be creating it once and reference it as follows
        info.makeNewAccount();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM