I am currently working with an array list of a movie rental store. I am trying to make a parameter of movieID,renterID, and movieName. I would like to make all of these one method when I run the program, so the user can input 1 or 2 or all 3 of these parameters. Is this possible to do this from one method/if so, how? Also, can I make it where java accepts a blank as a null instead of having the user type null? The specific code I am working with is below.
public void methodOverloading(int MovieID, long RenterID)
{
System.out.println();
this.printMovieInforForMovieID(MovieID);
this.printMovieInforForRenterID(RenterID);
}
public void methodOverloading(int MovieID, String MovieName)
{
System.out.println();
this.printMovieInforForMovieID(MovieID);
this.printMovieInforForMovieNameContaining(MovieName);
}
public void methodOverloading(long RenterID)
{
System.out.println();
this.printMovieInforForRenterID(RenterID);
}
public void methodOverloading(long RenterID, String MovieName)
{
System.out.println();
this.printMovieInforForRenterID(RenterID);
this.printMovieInforForMovieNameContaining(MovieName);
}
public void methodOverloading(String MovieName)
{
System.out.println();
this.printMovieInforForMovieNameContaining(MovieName);
}
No, java does not allow default values for method arguments, such as inserting a null if no value is given. The way to do it is to create one master implementation such as:
public void methodOverloading(Integer MovieID, Long RenterID, String MovieName)
{
System.out.println();
if (MovieID != null) {
this.printMovieInforForMovieID(MovieID);
}
if (RenterID!= null) {
this.printMovieInforForRenterID(RenterID);
}
if (MovieName!= null) {
this.printMovieInforForMovieNameContaining(MovieName);
}
}
and then a bunch of short methods that just call out to the master:
public void methodOverloading(String MovieName)
{
methodOverloading(null, null, MovieName);
}
public void methodOverloading(Integer movieID, Long renterID, String movieName)
{
System.out.println();
if(MovieID != null) {
this.printMovieInforForMovieID(movieID);
}
if(RenterID != null) {
this.printMovieInforForRenterID(renterID);
}
if(MovieName != null) {
this.printMovieInforForMovieNameContaining(movieName);
}
}
This method accepts all 3 parameters and will only call the print method if their value is not null.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With