ArrayList problem z getName

0

Witam, mam problem w swojej aplikacji otóż gdy tworzę konto dla klienta banku to przy jego wyświetleniu pokazuje mi numer konta jego saldo ale brak imienia klienta i tak już siedzę i próbuje drugi dzień coś zdziałać ale nie mam pojęcia o co chodzi.

public class AccountList
{
    private ArrayList <Account> newAccount = new ArrayList<Account>();

    //Funkcja dodaj konto:
    public boolean addAccount (Account newAcc){
        if (findAccount(newAcc.getUserName()) >= 0){ // getScurityNo changed to getUserName.
            System.out.println("Cannot create account due to Security Number error.");
            return false;
        }
        this.newAccount.add(newAcc);
        return true;
    }

    //Funkcja znajdź konto:
    private int findAccount (Account acc){
        return newAccount.indexOf(acc);
    }
    private int findAccount (String userName){
        for (int i = 0; i < newAccount.size(); i++){
            Account acc = newAccount.get(i);
            if (acc.getUserName().equals(userName)){
                return i;
            }
        }
        return -1;
    }

    public String queryAccount (Account acc){
        if (findAccount(acc) >= 0){
            return acc.getUserName();
        }
        return null;
    }
    /*private int findAccount (int securityNo){
        for (int i = 0; i < newAccount.size(); i++){
            Account newAcc = newAccount.get(i);
            if (newAcc.getSecurityNumber() == securityNo){
                return i;
            }
        }
        return -1;
    }*/

    //Funkcja usuń konto:
    public void removeAccount (int securityNo){
        for (int i = 0; i < newAccount.size(); i++){
            Account newAcc = newAccount.get(i);
            if (securityNo == newAcc.getSecurityNumber()){
                newAccount.remove(newAcc);
            }
        }
    }
    //Funkcja wyświetl wszystkie konta:
    public void showAllAcc (){
        for (int i = 0; i < newAccount.size(); i++){
            System.out.println((i+1) + ". \nName: " + newAccount.get(i).getUserName() + "\nAccount number: " + newAccount.get(i).getAccountNo() + "\nBalance: " + newAccount.get(i).getBalance());
        }
    }
}
public class Account
{
    private String userName;
    private long accountNo;
    private int securityNumber;
    private double random = Math.random();
    private double balance;


    public Account (String userName, double balance, int securityNumber){
        this.userName= userName;
        this.accountNo = (long)(random*(Math.pow(10,16)));
        this.balance= balance;
        this.securityNumber = securityNumber;
    }

    public String getUserName() {
        return userName;
    }

    public long getAccountNo() {
        return accountNo;
    }

    public double getBalance() {
        return balance;
    }

    public int getSecurityNumber() {
        return securityNumber;
    }

    public static Account createAccount (String userName, double balance, int securityNumber)
    {
        return new Account(userName,balance, securityNumber);
    }
}
0

W sensie, że userName == null? Pokaż kod, gdzie wywołujesz konstruktor Account

0
public class Main
{
    private static Scanner sc = new Scanner(System.in);
    private static AccountList accList = new AccountList();

    public static void main(String[] args) {

        boolean logout = false;
        int choice = 0;

        while (!logout){
            System.out.println("Select operation:");
            choice = sc.nextInt();
            switch (choice){
                case 0:
                    System.out.println("You have successfully logout.");
                    logout = true;
                    break;
                case 1:
                    newAccount();
                    break;
                case 2:
                    accountList();
                    break;
            }
        }
    }


    public static void accountList(){
        accList.showAllAcc();
    }

    public static void newAccount (){
        System.out.println("Enter new user name: ");
        String userName = sc.nextLine();
        sc.nextLine();
        System.out.println("Enter initial user account balance:");
        double balance = sc.nextDouble();
        sc.nextLine();
        System.out.println("Enter unique security number:");
        int securityNo =sc.nextInt();

        Account newAcc = Account.createAccount(userName, balance,securityNo);
        accList.addAccount(newAcc);
    }
}
0

Wydrukuj/przedebuguj jaką wartość ma zmienna userName zaraz po String userName = sc.nextLine();. Pewnie scanner sczytuje ci pustego stringa;

0

No i nawet jeśli to jak to naprawić?

0

Zrob tak i powiedz czy tak samo :)

System.out.println("Enter new user name: ");
String userName = sc.nextLine();
System.out.println("Enter initial user account balance:");
double balance = sc.nextDouble();
sc.nextLine();
System.out.println("Enter unique security number:");
int securityNo =sc.nextInt();

    Account newAcc = Account.createAccount(userName, balance,securityNo);
    accList.addAccount(newAcc);

1 użytkowników online, w tym zalogowanych: 0, gości: 1