Reputation: 13
public class Bank {
private ArrayList<Account> accounts;
private int numberOfAccounts;
public Bank() {
numberOfAccounts = 0;
accounts = new ArrayList<Account>();
}
public int getNumOfAccounts() {
return numberOfAccounts;
}
public void addAccount(Account a) {
numberOfAccounts++;
accounts.add(a);
}
public Account findAccount(int id) {
int index = id - 1;
if (accounts.size() >= id){
return accounts.get(index);
}
else return null;
}
public void addMonthlyInterest() {
for (Account x : accounts) {
x.addMonthlyInterest();
}
}
public void removeAccount(Account a) {
numberOfAccounts--;
accounts.remove(a);
}
}
So when I add the accounts to the ArrayList, and then I check the size, it keeps telling me there is nothing in it. Is there something wrong with the way I am adding using the .add()
?
Here is part of my BankTest. I am not exactly sure what to use for the testAddAccount ( JUnit wise), but I know that they are not being added since when I try to find accounts, none of them exist.
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import org.junit.Test;
public class BankTest {
private final ArrayList<Account> accounts = new ArrayList<Account>();
private Bank bank = new Bank();
private java.util.Random rand;
// private static final double[] RATES;
// private static final int[] TYPES;
SavingsAccount sa = new SavingsAccount(0.034);
CheckingAccount ca = new CheckingAccount(0.034, 100);
CreditCardAccount cca = new CreditCardAccount(0.034, 100);
@Test
public void testAddAccount() {
// Tests Bank.addAccount() by adding many random accounts to the bank.
// SavingsAccount sa = new SavingsAccount(0.034);
SavingsAccount sa = new SavingsAccount(0.034);
CheckingAccount ca = new CheckingAccount(0.034, 100);
CreditCardAccount cca = new CreditCardAccount(0.034, 100);
bank.addAccount(sa);
bank.addAccount(ca);
bank.addAccount(cca);
if (accounts.size() == 3){
System.out.println("true");
}else System.out.println("false");
}
@Test
public void testFindAccount() {
// Tests Bank.findAccount() by finding all existing accounts and
// attempting to find some non-existing accounts.
bank.addAccount(sa);
bank.addAccount(ca);
bank.addAccount(cca);
int size = accounts.size();
System.out.println(size);
accounts.get(0);
assertEquals(sa, bank.findAccount(1));
assertEquals(ca, bank.findAccount(2));
assertEquals(cca, bank.findAccount(3));
assertNull(bank.findAccount(50));
assertNull(bank.findAccount(80));
assertNull(bank.findAccount(230));
}
}
Upvotes: 0
Views: 5387
Reputation: 285405
You're not checking the right ArrayList! Your code:
// what is accounts variable for?? Get rid of it.
private final ArrayList<Account> accounts = new ArrayList<Account>();
private Bank bank = new Bank();
private java.util.Random rand;
// private static final double[] RATES;
// private static final int[] TYPES;
SavingsAccount sa = new SavingsAccount(0.034);
CheckingAccount ca = new CheckingAccount(0.034, 100);
CreditCardAccount cca = new CreditCardAccount(0.034, 100);
@Test
public void testAddAccount() {
// Tests Bank.addAccount() by adding many random accounts to the bank.
// SavingsAccount sa = new SavingsAccount(0.034);
SavingsAccount sa = new SavingsAccount(0.034);
CheckingAccount ca = new CheckingAccount(0.034, 100);
CreditCardAccount cca = new CreditCardAccount(0.034, 100);
bank.addAccount(sa);
bank.addAccount(ca);
bank.addAccount(cca);
if (accounts.size() == 3){ // **** accounts.size()???
System.out.println("true");
}else System.out.println("false");
You're checking accounts.size()
, and accounts is a local variable and has no knowledge of the accounts held by banks, the Bank object. It makes perfect sense that accounts.size() should be 0. Instead why not check the size held by the banks variable? That's why Bank has this method:
if (banks.getNumOfAccounts() == 3){
System.out.println("true");
}else System.out.println("false");
Upvotes: 1
Reputation: 857
I need to see how you instantiate and use the Bank object, but I'm going to guess that's where your problem is. The accounts list is initialized whenever you create a new Bank - are you creating a new Bank whenever you try to access the accounts list?
Upvotes: 2
Reputation: 121699
I would not keep track of the #/accounts manually. That's "bad" for several different reasons. Here's an alternative:
public class Bank {
private ArrayList<Account> accounts;
// private int numberOfAccounts; // No!
public Bank() {
// numberOfAccounts = 0; // No!
accounts = new ArrayList<Account>();
}
public int getNumOfAccounts() {
return accounts.size();
}
public void addAccount(Account a) {
// numberOfAccounts++; // No!
accounts.add(a);
}
Upvotes: 1