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?
insertaccountsArray? You probably have a loop around your third code block. You have to initializeaccountsArrayoutside of this loop.