Categories

JAVA DATEBASE
Technology Network Community
Oracle Database
Fusion Middleware
Development Tools
Java
Desktop
Server & Storage Systems
Enterprise Management
Berkeley DB Family
Cloud Computing
Big Data
Business Intelligence
Architecture
Migration and Modernization
E-Business Suite
Siebel
PeopleSoft Enterprise
JD Edwards World
Industries
JD Edwards EnterpriseOne
User Productivity Kit Pro (UPK) and Tutor
Governance, Risk & Compliance (GRC)
Master Data Management (MDM)
Oracle CRM On Demand
On Demand: SaaS and Managed Applications
AutoVue Enterprise Visualization
Primavera
ATG
Agile PLM
Endeca Experience Management
Fusion Applications
Archived Forums

 



Tags

New To Java


How to design constructor with variable parameters?


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.

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)             ....        } 

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.

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));   } } 

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.


Related Links

can't get scroll panes to scroll
Problem creating service jboss.security:name=XMLLoginConfig
Is there an easier way to do this?
How can I download Pointbase Database?
please help can we implement datasource for bluetooth connections
Convert an Object into a String
Change ireport Report Query On Runtime?
Problem with package
Building Lift(elevator)
Jdbc Error
How to reterive the image on Servlet
Need help to install Java installer
Process still exists in memory
I want to start learning Java
display chinese words in textbox
Java Service Wrapper