Vector Exercise
instance variables:
- a Vector called personVect using the no-argument constructor.
methods:
- a utility method called locatePerson that searches the list of people (personVect) for a particular social security number (passed to the routine as a String parameter) and returns the index of that element.
- a public method called addPerson that accepts name, SSN, and address as parameters and adds a new Person to personVect
- a public method called removePerson that accepts a SSN as an argument and locates a Person in the personVect (using locatePerson) and removes that element (if the vector is not empty and if the index returned is valid)
- a public method called showPersonList that prints a Person object at the specified index (parameter) from personVect.
// locate Person in a vector and return
the index
private int locatePerson ( String searchSSN
)
{
int index = -1;
// initialize to one less than smallest index
boolean found = false;
while ( !found && ++index <
personVect.size( )
)
{
found = searchSSN.equals ( ((Person) personVect.elementAt(index)).getSSN ( ) );
}
if (found) return index;
else return -1;
} // end locatePerson
public class Person
{
private String name;
private String SSN;
private String address;
public Person (
)
{
name = " ";
SSN = "000-00-0000";
address = " ";
}
public Person ( String name, String SSN, String address )
{
this.name = name;
this.SSN =
SSN;
this.address = address;
}
public String getSSN ( )
{
return
SSN;
}
// Return String with values of instance variables
public String showPerson( )
{
return "" + name + " with SSN " + SSN + " lives at " + address;
} // end showPerson
}