1

I have a class of credit card account information. The information is being read in from the file and needs to be stored into an array. So far the class is as defined:

    private CreditCardAccount[] accountsArray;
    private int fillLevel;
    
    public SortedArray() {
        accountsArray = new CreditCardAccount[1000];
    }
    
    private class CreditCardAccount{
        
        long accountNum;
        String name;
        String address;
        double creditLimit;
        double balance;
        
        CreditCardAccount (long accountNum, String name, String address, double crerditLimit, double balance) {
            
            this.accountNum = accountNum;
            this.name = name;
            this.address = address;
            this.creditLimit = creditLimit;
            this.balance = balance;
            
        }
        
    }

I am trying to insert the objects to an array but when I print out index 0 for example it keeps printing out the first credit card number repeatedly. Or when I try to print out index 1, it says the credit card number is null. I have tried this to insert:

private static void insert(SortedArray.CreditCardAccount[] accounts, CreditCardAccount account, int fillLevel) {
        
        for(int i = 0; i < accounts.length; i++) {
            
            if(accounts[i] == null) {
                
                accounts[i] = account;
                
                System.out.println(accounts[i].name);
                
                break;
                
                
            } 
        }
    }

This part is from where I am reading in and calling createAccount

operation = opsFile.nextLine();
    long acocuntNum = Long.parseLong(operation);
                    
    operation = opsFile.nextLine();
    String name = operation;
                    
    operation = opsFile.nextLine();
    String address = operation;
                    
    operation = opsFile.nextLine();
    double creditLimit = Double.parseDouble(operation);
                    
    operation = opsFile.nextLine();
    double balance = Double.parseDouble(operation);
                    
    database.createAccount(acocuntNum, name, address, creditLimit, balance);

This is where insert is getting called:

public boolean createAccount(long accountNumber, String name, String address, double creditLimit, double balance) {
        // TODO Auto-generated method stub
        
        boolean accountCreated = true;
        
        
        CreditCardAccount newAccount = new CreditCardAccount(accountNumber, name, address, creditLimit, balance);
        
        SortedArray.insert(accountsArray, newAccount, fillLevel);
        
        return accountCreated;
    }

I think it is adding the first object to every index in the array. Does anyone have any suggestions as to how to fix this?

4
  • 2
    Please add the code where your read the credit card information from the file and where you call the function insert Commented Oct 8, 2022 at 18:42
  • edited it to add where information is being read and where insert is being called Commented Oct 8, 2022 at 18:57
  • where do you initialize accountsArray ? You probably have a loop around your third code block. You have to initialize accountsArray outside of this loop. Commented Oct 8, 2022 at 19:49
  • accountsArray is being initialized in the class field at the top of my class Commented Oct 8, 2022 at 20:00

1 Answer 1

1

You should remove the break and the if statement. You're breaking out of the loop after the first iteration, sou you dont insert anything in accounts [1]. Also the if statement says after the first iteration dont add anything anymore, because the first index is not null. Thats why you keep getting the first account back.

Sign up to request clarification or add additional context in comments.

1 Comment

i've tried that but it would duplicate each account number 15 times

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.