• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Changing a boolean value within an arraylist with user input

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

First of all, I would like to say I'm a total beginner so please bear with me. And thanks for your help in advance!

I have set up a book object and library object. I have been able to accept user input using the scanner and add books to the arraylist:

That's the adding part not including the scanner stuff.

This is my arraylist:

Basically I need the user to select the book they want to loan out and then change the onLoan boolean to true (I have used a menu system for the user to choose different options).
I have created a loanBook() method but am not really sure where to start.

This is what my menu looks like:


And this is the code I used for it (I am unsure whether to use if statements or switch cases):

All help is appreciated, thanks again!
 
Marshal
Posts: 79225
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

That sort of menu looks to me like a good candidate for a switch‑case.
You can get at any element in a List if you know its index, simply by calling myList.get(index). Remember the first index is 0. Once you get your hands on the book reference from the List, you can simply call its lend method. And later you can call its return method. I presume you have such methods (or similar) in your Book class.

Why have you got your methods marked static? You should always have a good explanation for writing static. Otherwise it is probably a serious mistake.
 
Samuel Knight
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the warm welcome!

Would this be better? Or could it be more simple?


The thing I can't get my head around is that I have all of these different states like BookId, Title etc. but how do I tell it that I only want to change the onLoan variable. Surely if I do something like it won't work.
Honestly I've never heard of the lend or return methods . I have getters and setters/ accessors and mutators. Is that the same thing?

In terms of the static reference, I'm not sure really, I tried public void but couldn't get it to work. This is my first semi-difficult program, for me at least .
Hopefully I don't sound too dumb, but I'm quite lost.

Thanks.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect you are using static methods because you have a lot of code in main(). Read MainIsAPain (that's a link).

Essentially, for all but the most trivial programs, you should have one or two lines in main(). Something like this:

 
Samuel Knight
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my main():


It seems a bit useless to me.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason checkInput has to be static is because it's in main, which is static. Get your code out of main and then you can get rid of the "static" in the declaration of checkInput.

Honestly I've never heard of the lend or return methods


These would be method you write. If you have a Book class, you can write a lend and return method.
 
Campbell Ritchie
Marshal
Posts: 79225
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might have written those methods already but with different names; you said something about loanBook(). Maybe you should read loanBook for lend. Otherwise you should add lend and return methods (or similar) to your Book class.
I would suggest you move the code out of the switch into separate methods (the same sort of thing others have told you). Your switch would probably be in a method to run a Library. That would entail a Library class. The switch would look something like this:-To get that repeated it should be inside a loop. You can have a test for the option entered being not 7 to continue the loop. All the code you want to run should be inside those methods. The loan and return methods would probably each call findBook().
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Samuel Knight wrote:

I've gone back to the beginning and noticed this line. It seems no one mentioned that yet.

1 argument is fine; 2 - ok; 3 - well, could be, but starts indicating bad things; 4, 5 and 6 - proves your fears that something is wrong with your program design.

typeOfBook probably should not be an argument, but rather you should have special type book class, which could be a subclass of some superclass. That superclass maybe as abstract class could define a certain type of books.

onLoan at worst likely could be a default value of false and contain setter method in case state changes. Of course getter too.
numOfLoans not sure what is that, but most likely should not be an argument of constructor.

Anyway, have you been taught about inheritance?

[edited] sentence about typeOfBook as it was not clear.
 
Campbell Ritchie
Marshal
Posts: 79225
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:. . . onLoan at worst likely could be a default value of false and contain setter method in case state changes. Of course getter too. . . .

That is what the lend and return methods are about.

If you have getXXX methods which return booleans, it is customary to name them differently. Not getOnLoan() but isOnLoan(). That name works nicely in this instance.

You would probably have to find another way to set the ID than using the constructor parameters.
 
Samuel Knight
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys for the responses! I've been gone for a while working on some of your suggestions and stuff. Am in a much better position now and things have got sorted, so thanks again! I'm sure I'll post for help in the future
 
Campbell Ritchie
Marshal
Posts: 79225
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done Do show us what you have got.
 
Samuel Knight
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once I tidy it up and am happy with it I shall post all the code It should be within the next few days. Thanks again.
reply
    Bookmark Topic Watch Topic
  • New Topic