• Post Reply Bookmark Topic Watch Topic
  • New Topic

Boolean vlaue for a video store program - please help!!  RSS feed

 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Any idea how I can add a boolean method that will simply say whether a video is available or not. I have to admit I'm pretty useless with boolean statements and would appreciate any advice.

The program is a simple Video store management program. My current code is as follows,



Any help really appreciated!!
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
boolean is treated like any other type in this case, except for a naming convention on the "getter":
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply. I thought you had to use the conditional statements, if and else, to allow you to specify whether the video is available or not. Something like, 'if' the video is available return true, 'else' return false.

I don't quite see how your solution works.

Any help!!
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following are equivalent.

Programmers who tend to code in the second manner haven't realized that boolean
is can be treated like any type, because it is a type. If int foo was always 0 or 1,
which method would you write?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to first decide what it means for the video to be available. Jeff's solution assumes that a Video object has a boolean member where you mark each Video object as available or not (we're using "available" to mean the Video exists, but is currently lent out.)

But if, for example, "available" means that the store may or may not own a particular Video, then this implementation might not make sense. Maybe available() needs to be a member of the VideoStore class, and it might look like



where I've assumed your VideoStore class holds all Video objects in a HashMap using the titles as the keys.

So what I'm saying is that first you need to design this facility; then implementing the method is easy.
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Wow, this is all getting really complicated for me. I know what HashTables are, but I haven't yet learnt how to implement them in Java.

When I say 'available' for a video item, I mean in this case whether the video is available for rental or not. If it is not available this could mean either the store doesn't own it yet, or it is out on loan.

It's a pretty basic program at the moment!

Any help appreciated!

 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by celine scarlett:

When I say 'available' for a video item, I mean in this case whether the video is available for rental or not. If it is not available this could mean either the store doesn't own it yet, or it is out on loan.


OK. So you know that "available" actually is a combination of two different things. One is a property of a Video object (lent or not) while the other is really a property of the VideoStore object (presuming you have one of those.)

So you can ask a single Video object if it's rented or not using what Jeff said; that assumes, then, that when a Video goes out for rental, you're going to call setAvailable(false) and set that boolean member variable to false.

But if you want to know whether the store owns a Video or not, you probably need to ask the VideoStore, right? The VideoStore would need to search through the collection of Videos (I put them in a java.util.HashMap, but you could just use however you're currently doing it). If it doesn't find the Video it wants, then it returns false -- the Video isn't available. Then if the Video does exist, the VideoStore has to call its isAvailable() method so the Video can check its available flag. Make sense?

The coding is the easy part, really -- first you have to decide what you're going to do!
 
celine scarlett
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks for the reply and the explanation. The details of the videos, borrowers etc are to be stored in a text file/files.

I'll have to give this some real thought, and come back with the code I've implemented based upon your recommendations.

Thank you so much for your help. This might take me a while!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!