package edu.hawaii.ics415.problem3.fieldcheck;

import java.util.StringTokenizer;
import java.util.Calendar;

/**
 * Provides the account object which contains the first name, last name, date of
 * birth, and social security number. The exception is thrown if an invalid
 * characters are passed as parameters.
 *
 * @author   Takuya Yamashita
 */
public class Account {

  /**
   * Provides the first name of a person
   */
  private String firstName;
  /**
   * Provides the last name of a person
   */
  private String lastName;
  /**
   * Provides the date of birth of a person
   */
  private String dateOfBirth;
  /**
   * Provides the social security number of a person
   */
  private String ssn;

  /**
   * Constructs an object with the first name, last name, date of birth, and
   * social security number. The accountException is thrown if an invalid
   * characters are passes as parameters.
   *
   * @param firstName    A first name of a person. Must be alphabetic or '-'
   *      characters.
   * @param lastName     A last name of a person. Must be alphabetic or '-'
   *      characters.
   * @param dateOfBirth  A date of birth of a person. Must be 10 length and
   *      numeric characters and the position 5 and 8 must be '-'.
   * @param ssn          A social security number of a person. Must be 11 length
   *      and numeric characters and theposition 4 and 7 must be '-'.
   */
  public Account(String firstName, String lastName, String dateOfBirth, String ssn) {
    String exceptionMessage = "";

    // if the value is invalid, the concatenates the error message into exceptionMessage.
    if (!checkValidName(firstName)) {
      exceptionMessage += "First name is invalid. 
"; } if (!checkValidName(lastName)) { exceptionMessage += "Last name is invalid.
"; } if (!checkValidSsn(ssn)) { exceptionMessage += "SSN is invalid.
"; } if (!checkValidDateOfBirth(dateOfBirth)) { exceptionMessage += "Date of birth is invalid.
"; } // if the exceptionMessage is not empty string, then throws AccountException with the message. if (exceptionMessage.length() != 0) { throw new AccountException(exceptionMessage); } this.firstName = firstName; this.lastName = lastName; this.dateOfBirth = dateOfBirth; this.ssn = ssn; } /** * Provides the away to check whether or not the name is valid. Returns false * if the vaue passed as a parameter is invalid. Otheriwse returns true * . * * @param name The name to be checked on whether or not the name is valid. * @return true if the name string is valid. false * if the name stirng is invalid. */ public boolean checkValidName(String name) { if (name == null || name.length() == 0) { return false; } // checks every digit. for (int i = 0; i < name.length(); i++) { char character = name.charAt(i); if ((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') || (character == '-')) { } else { return false; } } return true; } /** * Provides the away to check whether or not the date of birth is valid. * Returns false if the vaue passed as a parameter is invalid. * Otheriwse returns true. * * @param dateOfBirth The dateOfBirth to be checked on whether or not the * value is valid. * @return true if the dateOfBirth string is valid. * false if the dateOfBirth stirng is invalid. */ public boolean checkValidDateOfBirth(String dateOfBirth) { if (dateOfBirth == null || dateOfBirth.length() == 0) { return false; } for (int i = 0; i < dateOfBirth.length(); i++) { char character = dateOfBirth.charAt(i); // checks total length ex. 2000-04-01 if (dateOfBirth.length() != 10) { return false; } // checks whether or not it contains dash in the proper position. if (i == 4 || i == 7) { if (character != '-') { return false; } } // checks whether or not charactor is number else { if (character < '0' || character > '9') { return false; } } } // Splits stirg into year, month, date, and makes array. StringTokenizer birthToken = new StringTokenizer(dateOfBirth, "-"); String[] birthArray = new String[3]; int i = 0; while (birthToken.hasMoreElements()) { birthArray[i++] = birthToken.nextToken(); } int year = 0; int month = 0; int day = 0; try { year = Integer.parseInt(birthArray[0]); month = Integer.parseInt(birthArray[1]); day = Integer.parseInt(birthArray[2]); } catch (Exception e) { return false; } // gets date object, then convert current year, month, and day Calendar calendar = Calendar.getInstance(); int currentYear = calendar.get(Calendar.YEAR); int currentMonth = calendar.get(Calendar.MONTH) + 1; int currentDay = calendar.get(Calendar.DATE); int February = 0; if (year % 4 == 0) { February = 29; } else { February = 28; } int[] monthArray = {31, February, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month < 1 || month > 12) { return false; } // returns false if day of birth exceeds max day of a month. ex. 32 -> falase if (day < 1 || day > monthArray[currentMonth - 1]) { return false; } // checks whether or not the birth year exceeds the current year if (year > currentYear) { return false; } else if (year == currentYear) { // checks whether or not the month exceeds the current month given the year == currentYear if (month > currentMonth) { return false; } else if (month == currentMonth) { // checks whether or not the day exceeds the current day give the month == currentMonth if (day > currentDay) { return false; } } } return true; } /** * Provides the away to check whether or not the ssn is valid. Returns false * if the vaue passed as a parameter is invalid. Otheriwse returns true * . * * @param ssn The ssn to be checked on whether or not the value is valid. * @return true if the ssn string is valid. false * if the ssn stirng is invalid. */ public boolean checkValidSsn(String ssn) { if (ssn == null || ssn.length() == 0) { return false; } for (int i = 0; i < ssn.length(); i++) { char character = ssn.charAt(i); if (ssn.length() != 11) { return false; } if (i == 3 || i == 6) { if (character != '-') { return false; } } else { if (character < '0' || character > '9') { return false; } } } return true; } /** * Sets a first name. The accountException is thrown if the first name is * invalid. * * @param firstName The firstName to be passed as a parameter. */ public void setFirstName(String firstName) { if (!checkValidName(firstName)) { throw new AccountException("First name is invalid."); } this.firstName = firstName; } /** * Sets a lastName. The accountException is thrown if the lastName is invalid. * * @param lastName The lastName to be passed as a parameter. */ public void setLastName(String lastName) { if (!checkValidName(lastName)) { throw new AccountException("Last name is invalid."); } this.lastName = lastName; } /** * Sets a dateOfBirth. The accountException is thrown if the dateOfBirth is * invalid. * * @param dateOfBirth The dateOfBirth to be passed as a parameter. */ public void setDateOfBirth(String dateOfBirth) { if (!checkValidDateOfBirth(dateOfBirth)) { throw new AccountException("Date of birth is invalid."); } this.dateOfBirth = dateOfBirth; } /** * Sets the sss attribute of the Account object * * @param ssn The new sss value */ public void setSss(String ssn) { if (!checkValidSsn(ssn)) { throw new AccountException("SSN is invalid."); } this.ssn = ssn; } /** * Gets the firstName attribute of the Account object * * @return The firstName value */ public String getFirstName() { return this.firstName; } /** * Gets the lastName attribute of the Account object * * @return The lastName value */ public String getLastName() { return this.lastName; } /** * Gets the dateOfBirth attribute of the Account object * * @return The dateOfBirth value */ public String getDateOfBirth() { return this.dateOfBirth; } /** * Gets the ssn attribute of the Account object * * @return The ssn value */ public String getSsn() { return this.ssn; } }