import PersonalProfileInterface; import java.util.Date; import SimpleOutput; /** * An implementation of PersonalProfile. * * @author Khong Lovan * @author Wanlin Liu * @author Elias Vafiadis * @author Anthony Nakaar * @version 1.1 of October 1999 */ public class PersonalProfile implements PersonalProfileInterface { // +--------+-------------------------------------------------------------- // | Fields | // +--------+ /** The user's name. */ Name userFullName; /** The user's address. */ StreetAddress address; /** The user's gender. */ String gender; /** The user's birthdate. */ Date birthDate; /** The user's phone number. */ String phoneNumber; //+--------------+---------------------------------------------------------- //| Constructors | //+--------------+ /** * Build an object of class PersonalProfile from the toString method of * the class. * @exception if the string is not convertible. */ public PersonalProfile(String str) throws Exception{ // Getting the user's name: First, get the index of the first new line. int line = str.indexOf("*"); // Throw an exception if the index is invalid. if (line == -1) { throw new Exception("Invalid"); } // if // Otherwise, get the elements of the string indexed between the beginning // and the first new line. else { this.userFullName = new Name(str.substring(0, line)); } // else // Getting the user's address: remove the user's name from the string. String anotherString = str.substring(line+1); // Get the index of the first new line. line = anotherString.indexOf("*"); // Throw an exception if the index is invalid. if (line == -1) { throw new Exception ("Invalid"); } // if // Otherwise, get the portion of the string that is the user's address. else { this.address = new StreetAddress(anotherString.substring(0, line)); } // else // Get the user's gender. // First, remove the address from the string. anotherString = anotherString.substring(line+1); // Then get the index of the first new line. line = anotherString.indexOf("*"); // If the index is invalid, throw an exception. if (line == -1) { throw new Exception("Invalid"); } // if // Otherwise, get the portion of the string that gives the gender. else { this.gender = anotherString.substring(0, line); } // else // Get the birthdate. // Get the phone number. anotherString = anotherString.substring(line+1); line = anotherString.indexOf("*"); if (line == -1) { throw new Exception("Invalid"); } // if else { this.phoneNumber = anotherString.substring(0, line); } // else } /** * This constructor takes no parameter and builds a personal profile. */ public PersonalProfile() { } /** * This constructor takes a name object and builds a personal profile. */ public PersonalProfile(Name name) { userFullName = name; } // +------------+---------------------------------------------------------- // | Extractors | // +------------+ /** * Get the user's name. * Pre: None. * Post: Returns the user's name. * Return type: Name. * Throws no exceptions. */ public Name getUserFullName() { return this.userFullName; } // getUserFullName() /** * Get the user's address. * Pre: None. * Post: Returns the user's address. * Return type: StreetAddress. * Throws no exceptions. */ public StreetAddress getAddress() { return this.address; } // getAddress() /** * Get the user's gender. * Pre: None. * Post: Returns the user's gender. * Return type: String. * Throws no exceptions. */ public String getGender() { return this.gender; } // getGender() /** * Get the user's date of birth. * Pre: None. * Post: Returns the user's date of birth. * Return type: Date. * Throws no exceptions. */ public Date getDateOfBirth() { return this.birthDate; } // getDateOfBirth() /** * Get the user's phone number. * Pre: None. * Post: Returns the user's phone number. * Return type: String. * Throws no exceptions. */ public String getPhoneNumber() { return this.phoneNumber; } // getPhoneNumber() // +-----------+---------------------------------------------------------- // | Modifiers | // +-----------+ /** * Set the user's name. * Pre: None. * Post: Changes the user's name according to the given parameter. */ public void setUserFullName(Name userName) { this.userFullName = userName; } // setUserFullName(Name userFullName) /** * Set the user's address. * Pre: None. * Post: Changes the user's address according to the given parameter. */ public void setAddress(StreetAddress address) { this.address = address; } // setAddress(StreetAddress address) /** * Set the user's gender. * Pre: None. * Post: Changes the user's gender according to the given parameter. */ public void setGender(String gender) { this.gender = gender; } // setGender(String gender) /** * Set the user's date of birth. * Pre: None. * Post: Changes the user's date of birth according to the given parameter. */ public void setDateOfBirth(Date DateOfBirth) { this.birthDate = DateOfBirth; } // setDateOfBirth(SimpleDate birthDate) /** * Set the user's phone number. * Pre: None. * Post: Changes the user's phone number according to the given parameter. */ public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } // setPhoneNumber(String phoneNumber) // +--------------+-------------------------------------------------------- // | Capabilities | // +--------------+ /** * Combines all information into one string. */ public String toString(){ String stuff = userFullName.toString() + "*" + address.toString() + "*" + gender + "*" + birthDate.toString() + "*" + phoneNumber + "*"; return stuff; } /** * Testing. */ /** public static void main(String[] args) { SimpleOutput out = new SimpleOutput(); // Create a profile. PersonalProfile profile = new PersonalProfile("Khong Lovan"); out.println(profile.toString()); } // main(String[])*/ } // class PersonalProfile