0

So I have a video inventory program,

Where the user creates a object of the class video by initializing the constructor

Video video=new Video(String name){
this.name=name
}

Now I have three more parameterized constructor

Video video=new Video(String name, int rating){
this.name=name;
this.rating=rating;
}

Video video=new Video(String name, int rating, boolean checkout){
this.name=name;
this.rating=rating;
this.checkout=checkout;
}

Video video=new Video(String name, boolean checkout){
this.name=name;
this.checkout=checkout;
}

Each video added is stored in a Video[] store array

First the user adds the video only by passing the name (eg.Matrix) and creates the object of the video, it gets added in the Video[] store

What I want is that whenever a another parameter is added to Matrix , suppose int rating, it first finds Matrix from the Video[] store array, then checks whether the boolean checkout parameter exists or not. if boolean checkout exists it passes the values to the constructor Video video=new Video(String name, int rating, boolean checkout) else it passes values to the constructor Video video=new Video(String name, int rating).

I want to know how to check if a parameter exists or not.

Another approach I wanted to take was to set one constructor Video(String name) and use null values to initialize the variables and later change them using setters, but you can't set int and boolean to null. So I have to use it like this (as I found on the internet)

      Video(String name){
       this.videoName=name;
       this.rating=(Integer) null;
       this.checkout=(Boolean) null;
    }
    

but this requires unboxing or something which I have no idea how to do. So any help would be appreciated. :)

  • 3
    Constructors are made to construct objects, not to complete existing ones. A better solution would be to have a method that does the check/completion and call the constructor only if it's necessary – jhamon Mar 10 '22 at 10:20
  • Rule number 1 in Object Oriented programming: model your things so that your abstractions are a good representation of reality AND useful. In your case: The name and rating of a movie ... is that going to change in the real world? Nope. Thus, and for many other reasons: strive to make your Video object **immutable**. Meaning: it should only have to (final!) fields name and rating. You set both fields using a constructor (the ctor checks that the incoming parameters are NOT null). And then your Video object NEVER needs to be updated again. – GhostCat Mar 10 '22 at 10:24
  • And then you could use another class that keeps a LIST of "existing movies" for example. And Another one that keeps a LIST of "existing movies that are currently rented". Believe me: allowing your Video object properties to be updated with setters is a BAD approach. Allowing properties to be NULL is also something you want to avoid. – GhostCat Mar 10 '22 at 10:25
  • And as said: keep concepts separated. A Video object doesn't know or care about its "rental" state. That is the responsibility of some sort of bookkeeper. And you also want to read a bit why/who you should implement/override the equals/hashCode methods in your Video class. – GhostCat Mar 10 '22 at 10:27
  • @GhostCat actually this a question given to me to solve for a college assignment, so as per requirements I can't make the variables final and I also need to include its rental state. The other suggestions, I will keep them in mind. Thank you. –  Mar 10 '22 at 10:41
  • If you dare to be a brave student, ask your instructor why they teach you bad OO practices ;-) – GhostCat Mar 10 '22 at 11:26

0 Answers0