0

I have the Accounts Java class as follows:

  public class Accounts {
            These are the fields
            private int accountNumber;
            private String accountName;
            private double accountBalance;

        public Accounts(int accountNumber, String accountName, double accountBalance) {
            this.accountNumber = accountNumber;
            this.accountName = accountName;
            this.accountBalance = accountBalance;
        }

        public int getAccountNumber() {
            return accountNumber;
        }
        public String getAccountName() {
            return accountName;
        }
        public double getAccountBalance() {
            return accountBalance;
        }
        public void setAccountNumber(int accountNumber) {
            this.accountNumber = accountNumber;
        }
        public void setAccountName(String accountName) {
            this.accountName = accountName;
        }
        public void setAccountBalance(double accountBalance) {
            this.accountBalance = accountBalance;
        }    
    }
   
    public class AccountController {
        ArrayList<Accounts> accounts = new ArrayList<>();
        public boolean addAccount(int accountNumber, String accountName, double accountBalance) {
            Accounts acc = new Accounts(accountNumber, accountName, accountBalance);
            if (findAccount(accountNumber) == null) {
                accounts.add(acc);
                return true;
            }
            return false;
        }

These are the classes I have: 1: Accounts 2: Account Controller 3: MyMainView

I want to print the list of the Accounts I add to the ArrayList along which contains accountNumber , accountBalance and accountName. I want to add the create the method inside the MyMainView and call it inside the main method of the MyMainView.

Yanji Lama
  • 58
  • 7
  • 2
    So, what's preventing you from doing so? What's your question? You might also want to (re)read [ask] and then update your post. – Thomas Jan 11 '22 at 11:22
  • I just don't know how to do so. I need some ideas so that I can print the accounts that I add using addAccount method to the arrayList – Yanji Lama Jan 11 '22 at 11:25
  • Well, you might want to start by defining "print": do you need to return the list to a client that somehow displays it or do you just need the controller to print the list to stdout (aka System.out)? Also what did you try? What's preventing you from just iterating over the list and printing the elements or just returning the list to the client? – Thomas Jan 11 '22 at 11:53
  • You need to create an `Accounts` object in `MyMainView` populate the `ArrayList` there and then loop through the list with a [loop](https://www.interviewsansar.com/how-to-print-arraylist-elements-in-java/) and [print the elements(override `toString()`)](https://stackoverflow.com/a/10734148/16653700). – Alias Cartellano Jan 11 '22 at 22:14

1 Answers1

0

You could try overriding the toString() method in your Accounts class. Next, create a method in Account Controller that iterates over all instances of Accounts, calling their respective toString() method and collecting the outputs. Now you can call that method in the main in MyMainView.