Create a Person class containing the following state and behavior as identified
ID: 3835114 • Letter: C
Question
Create a Person class containing the following state and behavior as identified below, and store at least five person in an Arraylist. Create two methods called writePerson(ArrayList<Person>) and readPerson returning ArrayList<Person>, which is to be included in your existing Person class.
In the writePerson method, create a file with Person objects where each object is represented on a separate line with each attribute separated by a comma, and this text file is to be saved to the C: emp folder, and the name is to be "PersonInfo.txt". In the readPerson method, retrieve each record in the file and store data in an ArrayList<Person>.
Once you called the methods from your PersonSortTest class, sort the object by last name/first name and display the last name/first name to the console. For example,
Ludgood, Michael
Ngo, Luan
Patton, Anthony
Salisbury, Robert
Thai, Kim
Object
State
Behavior
Person
First Name
Last Name
Age
Gender
SSN
getters/setters
Object
State
Behavior
Person
First Name
Last Name
Age
Gender
SSN
getters/setters
Explanation / Answer
PROGRAM CODE:
Person.java
package test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
public class Person {
private String firstName;
private String lastName;
private int age;
private String gender;
private String SSN;
public Person(String fname, String lname, int p_age, String p_gender, String p_ssn) {
firstName = fname;
lastName = lname;
age = p_age;
gender = p_gender;
SSN = p_ssn;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getSSN() {
return SSN;
}
public void setSSN(String sSN) {
SSN = sSN;
}
@Override
public String toString() {
return firstName + "," + lastName + "," + age + "," + gender + "," + SSN;
}
public static void writePerson(ArrayList<Person> persons)
{
PrintWriter writer;
try {
writer = new PrintWriter(new File("PersonInfo.txt"));
for(Person person: persons)
{
writer.write(person.toString() + " ");
}
writer.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void sort(ArrayList<Person> persons)
{
persons.sort(new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.getFirstName().compareTo(o2.getFirstName());
}
});
for(Person person: persons)
{
System.out.println(person.getLastName() + ", " + person.getFirstName());
}
}
public static ArrayList<Person> readPerson()
{
ArrayList<Person> persons = new ArrayList<>();
try {
Scanner fileReader = new Scanner(new File("PersonInfo.txt"));
while(fileReader.hasNextLine())
{
String line = fileReader.nextLine();
String tokens[] = line.split(",");
persons.add(new Person(tokens[0], tokens[1], Integer.valueOf(tokens[2]), tokens[3], tokens[4]));
}
fileReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return persons;
}
}
PersonSortTester.java
package test;
import java.util.ArrayList;
public class PersonSortTest {
public static void main(String args[])
{
ArrayList<Person> list = new ArrayList<>();
list.add(new Person("Michael", "Ludgood", 23, "Male", "78647824"));
list.add(new Person("Luan", "Ngo", 72, "Male", "3439289"));
list.add(new Person("Anthony", "Patton", 28, "Male", "8764234"));
list.add(new Person("Robert", "Salisbury", 43, "Male", "32983928"));
list.add(new Person("Kim", "Thai", 43, "Female", "198762"));
System.out.println("Before Sorting:");
for(Person person: list)
System.out.println(person);
System.out.println(" After sorting by first name: ");
Person.sort(list);
Person.writePerson(list);
ArrayList<Person> newList = new ArrayList<>();
newList = Person.readPerson();
System.out.println(" After reading from file: ");
for(Person person: newList)
System.out.println(person);
}
}
OUTPUT:
Before Sorting:
Michael,Ludgood,23,Male,78647824
Luan,Ngo,72,Male,3439289
Anthony,Patton,28,Male,8764234
Robert,Salisbury,43,Male,32983928
Kim,Thai,43,Female,198762
After sorting by first name:
Patton, Anthony
Thai, Kim
Ngo, Luan
Ludgood, Michael
Salisbury, Robert
After reading from file:
Anthony,Patton,28,Male,8764234
Kim,Thai,43,Female,198762
Luan,Ngo,72,Male,3439289
Michael,Ludgood,23,Male,78647824
Robert,Salisbury,43,Male,32983928