• Post Reply Bookmark Topic Watch Topic
  • New Topic

Library program - Method to return a book  RSS feed

 
Chris Green
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, currently I a following night school for Java programming. Yesterday we got an assignment to create a small library app. Created 3 classes (Book, Author, Loan). I have already written the constructors for all classes. Next we had to write methods for setting the loan on a book. This method I have written...However, I also need to write a method that resets the loaned state of a book. So when somebody picks up a book, it is loaned out, when the person returns the book, the book isn't loaned out anymore. I don't know if the way I am doing this is the correct one... I just set loan = null; Is this correct ?

Here is my code for the Book.java class.


Here my code for the author.java class:


And here is the code for the loan.java class
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll go by small bits depending on free minutes I find.


What happens when method gets invoked on null? So let's assume bookName is null. Please explain what would happen?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Didn't IntelliJ advice you on that?

It should tell you: Condition always false when reached
 
Chris Green
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:I'll go by small bits depending on free minutes I find.


What happens when method gets invoked on null? So let's assume bookName is null. Please explain what would happen?


Throw the IllegalArgumentException ?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us the test case you wrote in order to be assured this is indeed happens.

Test case could be either temporary main() method within the same class you're testing or JUnit test. Both should give similar output.
 
Chris Green
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Please show us the test case you wrote in order to be assured this is indeed happens.

Test case could be either temporary main() method within the same class you're testing or JUnit test. Both should give similar output.


Sorry about that... Here you go:

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you're testing ideal case scenarios. You need to test a bit more than that, in fact mainly the cases when the things might not go well.
So, pass in an empty string instead of actual bookName, then pass in null, then instead author pass in null and see if your test passes.

And fix accordingly, run tests again. Don't write different scenarios to one test case. They should be short, no more than 5 lines.
There should be at least 3 test cases for what I described and only for constructor of Book class: [1] null passed, [2] empty string, [3] valid input
 
Chris Green
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:But you're testing ideal case scenarios. You need to test a bit more than that, in fact mainly the cases when the things might not go well.
So, pass in an empty string instead of actual bookName, then pass in null, then instead author pass in null and see if your test passes.

And fix accordingly, run tests again. Don't write different scenarios to one test case. They should be short, no more than 5 lines.
There should be at least 3 test cases for what I described and only for constructor of Book class: [1] null passed, [2] empty string, [3] valid input


Ok, but my initial question in fact was if the way I used to tell the program that a book has been return method resetLoan() was the correct way to do this ?!
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Resetting means putting in it's initial or previous state before you did something. Now what state you had before you putted book on loan? Null, right? So that seems to be correct in this design to reset to null.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!