3

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);
}
3
  • 1
    please take a look at the naming convetions for java. variables and parameters should start with a lower case letter Commented Oct 7, 2014 at 14:19
  • I have the parameters capitalized because the attributes in the field are lower case. I used capital letters so it would not mistake the instance for the global attribute. Is that not correct? Commented Oct 7, 2014 at 17:28
  • no. access fields with this Commented Oct 7, 2014 at 18:14

3 Answers 3

2

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

you cant check int and long for null. you msut use the wrapper classes. this code wont compile.
you can't compare int or long to null. Also, please follow Java conventions on variable names.
@PhilippSander True, thanks. Will edit it and rely on auto-boxing.
@Arkadiy I think that matching the style of the question is more important than following the industry convention.
1
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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.