mvb
mvb

Reputation: 27

Java method overloading as a combined user input

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

Answers (3)

John Bollinger
John Bollinger

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,

  1. You can massage the user's inputs any way you like before handing them off to the method that does the work. In particular, if the user provides a blank value then your code can pass a corresponding null to the back end instead of the blank string.
  2. If there are parameter values that are guaranteed invalid then you can use them to signal missing user input. For example, if you can rely on all valid movie and renter IDs to be positive, forever, then you can pass -1 for those parameters to signal that the user did not provide a value. Making the parameters be of object types would allow you to use null for that purpose.
  3. Method overloading doesn't appear to be doing anything useful for you.

Upvotes: 0

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

Philipp Sander
Philipp Sander

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

Related Questions