How do I store multiple values ​​(with different types) in a table and print them?

advertisements

My program asks the user to create a bank account. At the moment it can create only one account and print it out. I want to extend my program so that it can create muliple accounts by choosing the menu option again and again and finally print all the account details.

package bankapp_assignment;

import java.util.Scanner;

public class BankApp_Assignment {

    public static void main(String[] args) {
        BankAccount[] accounts = new BankAccount[10];

        Scanner sc = new Scanner(System.in);

        int userChoose;
        String name = null;
        int accNum = 0;
        double initiateAmount = 0;
        double newAmm = 0;

        double depOrWith = 0;
        System.out.println("WELCOME TO OUR BANK!\n\n"
                + "...................\n"
                + "...................\n\n"
                + "Choose your optiin:\n"
                + "1. Create new account\n"
                + "2. View all the accounts property\n"
                + "3. Quit\n\n");

        System.out.println("*************\n"
                + "************");
        while (true) {
            userChoose = sc.nextInt();
            sc.nextLine();
            if (userChoose == 1) {
                /*the user must be able to create multiple accounts, let's say 10 accounts for instance
                 To open another new account the user should choose the menu option "1" again and continue...
                 */
                System.out.println("Enter your full name:");
                name = sc.nextLine();

                System.out.println("Choose an account number:");

                accNum = sc.nextInt();

                System.out.println("Enter an initiating amount:");

                initiateAmount = sc.nextDouble();
                System.out.println("\n-----------\n"
                        + "------------");
            } else if (userChoose == 2) {//view all the accounts property (including account number and initial balance)

            } else if (userChoose == 3) {
                System.exit(0);

            }
        }

    }

}

BankAccount:

package bankapp_assignment;

public class BankAccount {
    public void createAcc(){

    }
}

EDIT after Naman's answer:

    public static void main(String[] args) {
        BankAccount[] accounts = new BankAccount[10];

        Scanner sc = new Scanner(System.in);

        int userChoose;
        String name = null;
        int accNum = 0;
        double initiateAmount = 0;
        double newAmm = 0;

        double depOrWith = 0;
        System.out.println("WELCOME TO OUR BANK!\n\n"
                + "...................\n"
                + "...................\n\n"
                + "Choose your optiin:\n"
                + "1. Create new account\n"
                + "2. View all the accounts property\n"
                + "3. Quit\n\n");

        System.out.println("*************\n"
                + "************");
        while (true) {
            userChoose = sc.nextInt();
            sc.nextLine();
            if (userChoose == 1) {
                /*the user must be able to create multiple accounts, let's say 10 accounts for instance
                 To open another new account the user should choose the menu option "1" again and continue...
                 */
                System.out.println("Enter your full name:");
                name = sc.nextLine();

                System.out.println("Choose an account number:");

                accNum = sc.nextInt();

                System.out.println("Enter an initiating amount:");

                initiateAmount = sc.nextDouble();
                System.out.println("\n-----------\n"
                        + "------------");

accounts[numOfAcc]=bankAcc;
                numOfAcc++;

            } else if (userChoose == 2) {//view all the accounts property (including account number and initial balance)

   for(BankAccount bankAccount: accounts){

                  System.out.println("Your name: " + name);
                 System.out.println("Your account number: " + accNum);
                 System.out.println("Your current balance: " + initiateAmount);
                 System.out.println("\n-----------\n"
                 + "------------");  

                  }

            } else if (userChoose == 3) {
                System.exit(0);

            }
        }

    }

}


The complete code with the working answer

package tester;

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

public class BankApp {

    static class BankAccount {
        private String name;
        private int accNum;
        private double initiateAmount;

        public BankAccount() {
            this.name = null;
            this.accNum = 0;
            this.initiateAmount = 0;
        }

        public void setAccNum(int accNum) {
            this.accNum = accNum;
        }

        public void setInitiateAmount(double initiateAmount) {
            this.initiateAmount = initiateAmount;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return name + "\t" + accNum + "\t" + initiateAmount;
        }
    }

    public static void main(String[] args) {
        List<BankAccount> bankAccounts = new ArrayList<BankAccount>();
        Scanner sc = new Scanner(System.in);
        while(true){
        System.out.println("WELCOME TO OUR BANK!\n\n"
                + "Choose your option:\n"
                + "1. Create new account\n"
                + "2. View all the accounts property\n"
                + "3. Quit\n");

            int option = sc.nextInt();
            sc.nextLine();
            switch(option){
                case 1:
                    BankAccount account = new BankAccount();
                    System.out.println("Enter your full name:");
                    account.setName(sc.nextLine());
                    System.out.println("Choose an account number:");
                    account.setAccNum(sc.nextInt());
                    System.out.println("Enter an initiating amount:");
                    account.setInitiateAmount(sc.nextDouble());
                    bankAccounts.add(account);
                    break;
                case 2:
                    System.out.println("Name\tAcc No\tAmount");
                    for (BankAccount bankAccount : bankAccounts) {
                        System.out.println(bankAccount);
                    }
                    System.out.println("\n\n");
                    break;
                case 3:
                    return;
            }
        }
    }
}

If you want to have the BankAccount in seperate file do the following.

file BankAccount.java

public class BankAccount {
...
...
}