4 Replies Latest reply: Nov 26, 2008 1:54 PM by 843785 RSS

    How to design constructor with variable parameters?

    843785
      I'm working on a "project" where I need to make a class "book" that can store information about a book. It's name, isbn, stock, etc... What is throwing me off a little bit is the author. I'm allowed to enter up to 4 authors per book object. I'm coming at the problem with the assumption, that I should be able to load this author data within the constructor. Questions is how. At first I thought I'd make 4 different constructors, each with a different number of author parameters. Simple enough but it seems unnecessarily messy. Then I thought I'd make a variable length parameter array. e.g book( String ... author ). But what if someone wants to enter more than 4 authors or none at all?

      QUESTION:
      Is there a way I can set a minimum and maximum of inputs for variable length paramters?


      Vectors seem like they could be useful here instead of arrays. But again there is that problem of controlling constructor parameters.
        • 1. Re: How to design constructor with variable parameters?
          jschellSomeoneStoleMyAlias
          Then I thought I'd make a variable length parameter array. e.g book( String ... author ).
          (Otherstuff, String author, String ... otherAuthors)
          Is there a way I can set a minimum and maximum of inputs for variable length paramters?
          Validate it in the code itself

          Or create another class specifically for Authors

          No matter what you do you will still need validation code, for example what if the author is null? Or empty?
               class Authors
               {
                      public String Author1;
                      public String Author2;
                      public String Author3;
                      public String Author4;
          
                      public Authors(String auth)
                      public Authors(String auth1, String auth2)
                      ....
                 }
          • 2. Re: How to design constructor with variable parameters?
            843785
            Thanks, those seem like reasonable ways of doing things. I know there is going to be some checks in the code regardless. But is there a way to have the compiler return an error if say 5 authors are entered. Instead of having my code simply omit one, or send a message to the user saying "You have too many authors." Now that I'm thinking about it, that wouldn't be necessary, but I'd still like to know.
            • 3. Re: How to design constructor with variable parameters?
              843785
              The restriction of 4 authors max. looks rather arbitrary to me.

              I'd store the authors in a List and just add a check in the constructor:
              import java.util.*;
              
              public class Book {
                private List<String> authors;
              
                public Book(String... authors) {
                  if (authors.length > 4) {
                    throw new IllegalArgumentException("Too many cooks spoil the broth.");
                  }
                  this.authors = new ArrayList<String>(Arrays.asList(authors));
                }
              }
              • 4. Re: How to design constructor with variable parameters?
                jschellSomeoneStoleMyAlias
                yougene wrote:
                Thanks, those seem like reasonable ways of doing things. I know there is going to be some checks in the code regardless. But is there a way to have the compiler return an error if say 5 authors are entered. Instead of having my code simply omit one, or send a message to the user saying "You have too many authors." Now that I'm thinking about it, that wouldn't be necessary, but I'd still like to know.
                No. Because what you are describing can only occur at run time. A user might enter one author or nine. But that can't ever happen until run time.

                All your code can do is restrict the possibilities and drive towards a solution that restricts how it can be used. Thus if you implement the Authors class and then someone creates a GUI that uses it they will know that there is no point in allowing the user to enter more than four authors.