Results 1 to 5 of 5

Thread: please help me

  1. #1
    javanew is offline Member
    Join Date
    Mar 2010
    Posts
    49
    Rep Power
    0

    Default please help me

    i have this constructor,, if i want to restrict it to create objects if idNum < 8 digits howw ??? do not use exceptions please

    Java Code:
    	public Payk(String checkNum, String id ) {
    		this.id = id; //--> how to make it to not accept less than 8 digits
    		this.checkNum = checkNum;
    		
    	}
    Last edited by javanew; 03-29-2010 at 12:38 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You can check if a number has fewer than 8 digits with:

    Java Code:
    if(num < 10000000 && num > -10000000) {
        // whatever
    }

    If you don't throw an exception then invoking the Paycheck constructor will return a reference to a newly constructed object, regardless of the arguments and what you do with them.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I've just noticed that idNum is a String. You can get the length of the string easily with the .length() method.

    (You may have a problem now that idNum is not a valid integer so the length of the string may not make sense interpreted as a number of digits. And again if there is a restriction on whether an object gets created then the caller must deal with this: either have the constructor throw an exception that the caller does something with, or have the caller check the condition before calling the constructor.)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'm thinking that Exceptions are the best solution here. One kludge though could be to give the class a default ID String, or truncate a too-long string or pad a too-short String, but I don't like these "solutions" at all.

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    You need to use the Factory pattern. A factory is a class that produces instances of another class. The factory can be implemented using static methods, a singleton, or you can instantiate multiple factories. The static method approach is the most simple, especially if you simply add a static method to the existing class.

    Add a public static method, such as
    Java Code:
    public static Payk createPayk(final String checkNum, final String id) {}
    Change the constructor from public to private, to keep others from using it.

    In the static method, validate the parameters. If they are not valid, return null. Otherwise, call the constructor and return the result.
    The Java Tutorial. Read it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •