Java method overloading as combined user input

I am currently working with a list of arrays at a movie rental store. I am trying to make the parameter movieID, renterID and movieName. I would like to do all these methods when the program starts up, so the user can enter 1 or 2 or all 3 of these parameters. Can this be done with one method / if so, how? Also, can I do this when java accepts empty as null and not user type null? The specific code I'm 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);
}

      

+3


source to share


3 answers


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 takes all 3 parameters and will only call the print method if their value is not zero.

+1


source


No, java does not allow default values ​​for method arguments such as inserting null if no value is given. The way to do this is to create one main implementation, for example:

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 the master:

public void methodOverloading(String MovieName)
{
    methodOverloading(null, null, MovieName);
}

      

+2


source


The user of your program will not refer to any of these methods. Any input it provides will be collected by some GUI control or I / O method that you write, and calls to any of the methods you provide will be made by other code that you write.

Thus,

  • You can massage the custom inputs any way you like before passing them to the method that does the job. In particular, if the user provides an empty value, your code can pass the corresponding one null

    to the back end instead of the empty string.
  • If there are parameter values ​​that are guaranteed to be invalid, you can use them to signal missing user input. For example, if you can rely on all valid video and tenant IDs to be positive, forever, you can pass -1 for these parameters to signal that the user did not provide a value. Creating type object parameters will allow you to use null

    for this purpose.
  • Method overloading doesn't seem to do anything useful for you.
0


source







All Articles