System.out.print不显示ArrayList <object>

时间:2018-11-08 22:21:45

标签: java object arraylist system.out

对象“ accountData”将不会在控制台中使用CloseAccount()方法显示。我已经尝试过for循环的各种变体,但是它要么显示为“ 0”。在标题之后或在标题之后什么也没有。我只希望能够将ArrayList的所有元素(及其元素对象数据)打印给用户。

import java.util.*;
    public class AccountManagement {
        Scanner PETE = new Scanner(System.in);
        ArrayList<BankAccount> accountData = new ArrayList<BankAccount>();
        public AccountManagement() {
            do {
                System.out.println("MANAGER (0) OR CUSTOMER (1): ");
                int userSelect = PETE.nextInt();
                if(userSelect == 0) {
                    Manager();
                    break;
                }else if(userSelect == 1){
                    Customer();
                    break;
                }else {
                    System.out.println("Please pick a valid selection");
                }
            }while(true);
            PETE.close();
        }
        private void Manager() {
            do {
                System.out.println("\t0. Open Account\n\t1. Close Account\n");
                int accountOpt = PETE.nextInt();
                if(accountOpt == 0) {
                    OpenAccount();
                }else if(accountOpt == 1) {
                    CloseAccount();
                }else {
                    System.out.println("Please pick a valid selection");
                }
            }while(true);
        }
        private void Customer() {
            do {
                //things
            }while(true);
        }
        private void OpenAccount() {
            do {
                System.out.println("Checking (0) or Savings (1): ");
                int CoS = PETE.nextInt();
                if(CoS == 0) {
                    System.out.println("SSN: ");
                    long SSN = PETE.nextLong();
                    System.out.println("PIN: ");
                    int PIN = PETE.nextInt();
                    Random rand = new Random();
                    int ACCNUM = rand.nextInt(10000);

                    CheckingAccount CA = new CheckingAccount(ACCNUM, PIN, SSN, 0);

                    System.out.println("Would you like to also add a savings account? (0 = Yes, 1 = No)");
                    int sav = PETE.nextInt();
                    if(sav == 0) {
                        ACCNUM = rand.nextInt(10000);
                        SavingsAccount SA = new SavingsAccount(ACCNUM, PIN, SSN, 0);
                        BankAccount BA = new BankAccount(CA, SA);
                        accountData.add(BA);
                    }else if (sav == 1) {
                        break;
                    }else {
                        System.out.println("Please enter a valid selection");
                    }
                }else if(CoS == 1) {
                    System.out.println("SSN: ");
                    long SSN = PETE.nextLong();
                    System.out.println("PIN: ");
                    int PIN = PETE.nextInt();
                    Random rand = new Random();
                    int ACCNUM = rand.nextInt(10000);

                    SavingsAccount SA = new SavingsAccount(ACCNUM, PIN, SSN, 0);

                    System.out.println("Would you like to also add a checking account? (0 = Yes, 1 = No)");
                    int check = PETE.nextInt();
                    if(check == 0) {
                        ACCNUM = rand.nextInt(10000);
                        CheckingAccount CA = new CheckingAccount(ACCNUM, PIN, SSN, 0);
                        BankAccount BA = new BankAccount(CA, SA);
                        accountData.add(BA);
                    }else if (check == 1) {
                        break;
                    }else {
                        System.out.println("Please enter a valid selection");
                    }
                }else {
                    System.out.println("Please pick a valid selection");
                }
            }while(true);
        }
        private void CloseAccount() {
            System.out.println("Which account would you like to close?");
            System.out.println("Selection\tCA ACCTNUM\tSA ACCTNUM");
            for(int x = 0; x < accountData.size(); x++) {
                System.out.println(x+".\t");
                System.out.print(accountData.get(x).CA.getAccountNumber()+"\t"+accountData.get(x).SA.getAccountNumber());
            }
            int selection = PETE.nextInt();
            accountData.remove(selection);
        }
        public ArrayList<BankAccount> getAccounts() {
            return accountData;
        }

    }

1 个答案:

答案 0 :(得分:1)

我刚刚在本地进行了测试,并且可以为我打印。只需使用println而不是print进行测试。

My local test

当我将print更改为println时,得到的打印数据如下:

Printed data using println instead of print