User bean.

Enterprise Java
Architecting and Designing Scalable, Multitier Systems
by Michael Minh Nguyen
Listing 6. User bean.


package com.scalablemultitier;



public class User

{

  private static final int MAX_FIRST_NAME_LENGTH_ALLOWED = 10;

  private static final int MAX_LAST_NAME_LENGTH_ALLOWED = 10;

  private static final String FIRST_NAME_ERROR_MSG =

      "First name needs to be between 1 and 10 characters.";

  private static final String LAST_NAME_ERROR_MSG =

     "Last name needs to be between 1 and 10 characters.";

  private static final String MIDDLE_INITIAL_ERROR_MSG =

     "Middle initial is optional and can have only 1 character.";

  private static final String EMAIL_ERROR_MSG =

    "Email specified is either blank or not in the correct format.";

  private static final char APPERSEND = '@';

  private static final char PERIOD = '.';

  private static final char HYPHEN = '-';

  private static final char UNDERSCORE = '_';

  String mFirstName, mLastName, mEmail, mMiddleInitial;



  public void setFirstName (String firstName)

  {

    int length;

    if (firstName == null)

    {

      // Normally throw a application-specific exception such as BusinessRuleException.

      // Use RuntimeException keep code listing simple.

      throw new RuntimeException (FIRST_NAME_ERROR_MSG);

    }

    length = firstName.trim().length();

    if (length < 1 || length > MAX_FIRST_NAME_LENGTH_ALLOWED)

    {

      throw new RuntimeException (FIRST_NAME_ERROR_MSG);

    }

    mFirstName = firstName.trim ();

  }



  public String getFirstName ()

  {

    return mFirstName;

  }



  public void setLastName (String lastName)

  {

    int length;

    if (lastName == null)

    {

      throw new RuntimeException (LAST_NAME_ERROR_MSG);

    }

    length = lastName.trim().length();

    if (length < 1 || length > MAX_FIRST_NAME_LENGTH_ALLOWED)

    {

      throw new RuntimeException (LAST_NAME_ERROR_MSG);

    }

    mLastName = lastName.trim ();

  }



  public String getLastName ()

  {

    return mLastName;

  }



  public void setEmail (String email)

  {

    int appersendIndex, periodIndex;

    if (email == null || email.trim ().length () == 0)

    {

      throw new RuntimeException (EMAIL_ERROR_MSG);

    }

    appersendIndex = email.indexOf (APPERSEND);

    if (appersendIndex <= 0)

    {

      throw new RuntimeException(EMAIL_ERROR_MSG);

    }

    periodIndex = email.indexOf (PERIOD);

    if (periodIndex <= 0)

    {

      throw new RuntimeException(EMAIL_ERROR_MSG);

    }

    // make sure that there is one or more characters after the period

    if (periodIndex == (email.length () - 1))

    {

      throw new RuntimeException(EMAIL_ERROR_MSG);

    }

    // check for only character or numbers

    for (int i = 0, length = email.length (); i < length; i++)

    {

      if (! Character.isLetterOrDigit  (email.charAt (i)) &&

            email.charAt (i) != APPERSEND && email.charAt (i) != PERIOD &&

            email.charAt (i) != HYPHEN && email.charAt (i) != UNDERSCORE)

      {

        throw new RuntimeException (EMAIL_ERROR_MSG);

      }

    }

  }



  public String getEmail ()

  {

    return mEmail;

  }



  public void setMiddleInitial (String middleInitial)

  {

    int length;

    if (middleInitial == null)

    {

      mMiddleInitial = null;

      return;

    }

    length = middleInitial.trim().length();

    if (length > 1)

    {

      throw new RuntimeException (MIDDLE_INITIAL_ERROR_MSG);

    }

    mMiddleInitial = middleInitial.trim ();

  }



  public String getMiddleInitial ()

  {

    return mMiddleInitial;

  }

}

About the Author

Michael Minh Nguyen is a senior software engineer at eBuilt Inc. in Irvine, CA. He may be contacted at mnguyen@ebuilt.com.

Featured

Most   Popular
Upcoming Events

AppTrends

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.