Reputation: 27
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);
}
Upvotes: 3
Views: 517
Reputation: 181149
The user of your program is not going to invoke any of these methods. Any input he provides will be collected by some GUI control or I/O method that you write, and calls to any of the methods you present will be made by other code you write.
As such,
null
to the back end instead of the blank string.null
for that purpose.Upvotes: 0
Reputation: 2800
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);
}
Upvotes: 2
Reputation: 10249
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.
Upvotes: 1