• Post Reply Bookmark Topic Watch Topic
  • New Topic

JUnit testing, find Account object in ArrayList, Bank class  RSS feed

 
Leesa Gabor
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to figure out how to work the findAccount method (line 21). It should return the account that has the specified id or null if no such account exists. As accounts are created they are numbered starting with one, so that means the index is one less than the id. This is part of my project to simulate a bank teller. All my Account classes (Account, SavingsAccount, CheckingAccount, and CreditCardACcount) work, we are testing using JUnit. I am trying to fix the Bank class. We are using an array list.

My Bank class:



The BankTest class should test Bank.findAccount() by finding all existing accounts and attempting to find some non-existing accounts.



To test my findAccount method, do I have to create a bunch of Accounts within the testFindAccount? I am getting junit.framework.AssertionFailedError: null, and (referring to remove account) java.lang.IndexOutOfBoundsException: Index: 75, Size: 20
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Leesa,

What's happened here is you've posted way too much code, and way too little explanation. People see hundreds of lines of code, and instantly move on to the next question, so it's good if you can pare it down to a few dozen lines or so that still demonstrate the problem. It takes some practice to do that effectively, but I promise it's worth it. The ability to ask good questions is one of the single most important skills for a software developer.

In the same vein, describing something as "totally failing" isn't as useful as saying it doesn't compile, or the assertion at a particular line fails, or it throws an exception. If you had said your test throws an IndexOutOfBoundsException, for example, I could have directed you the JavaDocs for ArrayList, where you'd see that passing an index to the get(int) method that is bigger than the size of the list will cause that exception to be thrown. I would recommend checking the size of the ArrayList before trying to retrieve an element by index, and point out that checking if the list contains an element that you've just retrieved from it doesn't really make sense.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leesa Gabor wrote:I need to figure out how to work the findAccount method...

Well, first off, a List of any choice was not a good one, because they allow duplicates (unless you specifically disallow them).

Second: Thanks for all the code, but you may have overdone it a tad (but better overdone than underdone )

So now I am trying to figure out how to do the Bank class...

Actually, I think you need to think about what a Bank class is (or does). Your problem is that you've dived into coding without doing even that.
Programming is translating a problem into a sequence of actions that solve it.

Therefore, you need to think before you code.

Simple as this: If you don't understand the problem, you will never be able to write the code to solve it.
If you think you understand it, you will write:
(a) (best outcome) A solution that simply doesn't work (you were wrong).
(b) (worst outcome) A solution that works some - or even worse, most - of the time (you were right some of the time).

So, my advice:
1. Turn your computer off.
2. Sit down with a pencil and paper and concentrate on what a Bank does. Maybe list some actions.
3. When (and only when) you've got that list, think through how it might do those things. And again, write them down.

When you've done that (and it may take you a day or two), come back and see how close the code you've written is to what you now know.

Winston
 
Leesa Gabor
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay thanks I tried to clarify my questions.

So if I check the size of the arraylist, then if the given index is out of bounds I can make it return null.

Like this:

 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's better, although I'm not sure why you're subtracting 1. Actually, I'm thinking that finding the account by its position in the list is not what you want to do anyway. You call the parameter id, not index. Does that imply that you want to find an account with a particular account ID? In that case, you'd want to traverse the list and compare account IDs to the one you're looking for. And when you start studying maps, you'll change the data structure to a HashMap instead of a list.
 
Leesa Gabor
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in order to return the account, the get function has to take the index as a parameter, but the function we are making is supposed to take in and find the account based on its id. So that is why I am subtracting 1, because the first account created has id 1, which is at index 0, and they are given ids going up from one. I guess I could loop through all of the accounts in the list and find the one with the matching id, I think that is what you are saying. Would that be better to do than to use get?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leesa Gabor wrote:in order to return the account, the get function has to take the index as a parameter, but the function we are making is supposed to take in and find the account based on its id. So that is why I am subtracting 1, because the first account created has id 1, which is at index 0, and they are given ids going up from one. I guess I could loop through all of the accounts in the list and find the one with the matching id, I think that is what you are saying. Would that be better to do than to use get?

My suggestion would be to actually use a structure that allows you to find Accounts quickly. What you are doing is 'building logic' into the List index, which is usually not a good thing. What happens when you delete an Account? If you keep them in a List, then all the accounts to the right of the one you deleted will now have different indexes. You could possibly use your method to store them in an array, but I still think it's unnecessarily complicated.

Have a look at Maps (java.util.Map); and in your case probably a HashMap<Integer, Account>.

Winston
 
Leesa Gabor
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to be stubborn, I realize you think I am not doing this efficiently but this is how we are doing it for my class and I need to figure it out.

So I am confused because when I call accounts.add() and add an account in the test, it works, but calling bank.addAccount() does not.
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, last week you asked if it would be better to loop through the list of accounts for an account number, or just retrieve the account by index. That really depends on what your application is supposed to do. I'd say the first way is more flexible. If you miss an account number while adding accounts, or don't add them in strict numerical order, or later decide that you want to leave gaps in the account numbers, then your strategy of returning an account based on its index will break.

Be that as it may, your find method should work now, providing that you the first bank account you add has ID 1, the second has ID 2, etc. You say calling bank.addAccount() doesn't work now. Do you mean it fails, or a test fails, or the computer periodically bursts into flames? I have to go back on my earlier position, and ask for more code. What's the setup for testFindAccount()? I mean, where is "bank" instantiated and what accounts are set up into it?
 
Leesa Gabor
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my BankTest



Good news, it is working now. I realized the reason bank.addAccount was not "working" meaning, after I added accounts, I would check the array list to find the accounts and none of them would exist. That is because they were being added to the accounts within the bank that I constructed, and then when I was looking for the accounts, it was trying to find them in the accounts array that was created in the BankTest, which was empty. So I am thinking things are working, unless I should be making the accounts be added to the ArrayList accounts that is created in BankTest and not in the bank object. We are about to have our teacher run tests on them so I will see what happens. :/ Yikes!!!
 
Leesa Gabor
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and I realized I could change the number of accounts variable and just use size() so here is my revised Bank:

 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good for you getting those working! You might also look at the @Before and @BeforeClass annotations for JUnit tests. It's generally better form to use those to create initialization code rather than just having it done at construction time.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!