• Post Reply Bookmark Topic Watch Topic
  • New Topic

Jave Bank Account Class - Satisfying Test Expectations  RSS feed

 
Joffrey Williams
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to rewrite my code to get a majority of my Test to pass. I am having a difficult time getting these final to tests to pass (listed at Bottom)   .

(1st test) When a withdrawel is is too large the WIthdrawal fails. I have tried different runtime exceptions, negative balances etc, nothing satisfies this test.

(2nd test)When my account is opened out of range (<1.00  and > 10000.00) its suppose to throw a Runtime Exception. None of my runtime exceptions satisfy this test.

What am I doing wrong?

I set up a variable for min and max and indicated that account is outOf Range(does nothing).



 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What sort of exception are you testing for and what sort of exception are you throwing? You would usually throw an IllegalArgumentException if the wrong amount is supplied as an argument. If you have something like an account with minimum balance, you sh‍ould consider a custom exception (maybe EmptyAccountException). You would have to test for that exception.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I'll skip for a while your main questions.

Joffrey Williams wrote:What am I doing wrong?
1. First, making no help for yourself by inconsistently formatting your code, whish makes your code difficult to read and finally look messy.

2. Try to follow such formatting:
Have you noticed how formatting is consistent? Fields declarations start right below the class declaration, telling that fields belong to class. Later goes constructor which initializes fields. Methods, one by one, each of them separated with an empty line. No spaces in between each line of code, as it makes difficult to spot where one method ends and other begins.

3. Another thing. Variable name 'accountOutOfRange' I think is a very poor name in this context. Out of range goes usually indices, not the account balance state. You really need to spend a bit more time on that and find better name which would be obvious to everybody and no questions would arise.

4. Check this your method:
Joffrey Williams wrote:

What you basically do here is next, you set account as out of range when the amount supplied is valid and could be withdrawn. Is it really what you want to do? Probably not. I presume that happened because of wrong indentation and formatting as well. It just confirms my very first point.

5. Reading your multiple comments such as: 'nothing satisfieds this test', 'non of my runtime exceptions satisfy this test', suggests that you just throwing random code snippets in and expecting to pass. However, exceptions you're expecting to being thrown you need to specificy more expicitly. In your case would be IndexOutOfBoundsException (that is of course wrong one to throw in the first place). Common practice is to throw an IllegalArgumentException when the argument doesn't satisfy or in different words violates your rule if(amount > getAccountBalance()). Over here instead getAccountBalance() probably you can use your field directly as you're operating with the same class's field.

6. Here one more thing:
Joffrey Williams wrote:

What you do here, on line 13, you give balance a value of initialAmount and calling a method setBalance(balance) to do the same thing once again. Think how to fix that.


Anyway, I'd say start fixing your code from the formatting and indentation issues first and then move towards another.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!