Create a Person class containing the following state and behavior as identified
ID: 3830927 • Letter: C
Question
Create a Person class containing the following state and behavior as identified below. You must also create a PersonTest class to store at least three persons in an ArrayList. Create a method called WriteFile() that creates a text file that contains 3 Person objects where each object is represented on a separate line with each attribute seperated by a comma, and this text file is saved in the c: emp folder and the filename is PersonInfo.txt.
Create a method called ReadFile() that reads from the file, creates one Person object per line, and stores the object in an ArrayList object. Write the stored sorted objects by last name and display last name and first name to the console. For example, Salisbury, Robert.
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
Person.java
import java.util.Comparator;
public class Person implements Comparator<Person>{
private String firstName;
private String lastName;
private int age;
private String gender;
private String ssn;
public Person(){
}
//Constructor with parameters to create person objects
public Person(String firstName, String lastName, int age, String gender, String ssn){
this.firstName=firstName;
this.lastName=lastName;
this.age=age;
this.gender=gender;
this.ssn=ssn;
}
//Getters and setters
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) {
this.ssn = ssn;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
//to print the details of Person
public String toString(){
return firstName+" , "+lastName+" , "+age+" , "+gender+" , "+ssn;
}
//To compare using last name
/* (non-Javadoc)
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
*/
@Override
public int compare(Person p1, Person p2) {
return p1.getLastName().compareTo(p2.getLastName());
}
}
PersonTest.java
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class PersonTest {
public static void main(String[] args) {
//Arraylist to store person objects
ArrayList<Person> people=new ArrayList<Person>();
//Create new Person objects and add to ArrayList
Person p1=new Person("Salisbury","Robert",20,"Male","123-2345-123");
people.add(p1);
Person p2=new Person("James","Pearson",21,"Male","568-2345-123");
people.add(p2);
Person p3=new Person("Marie","Curie",21,"Female","880-2345-123");
people.add(p3);
//Sort using last name
Collections.sort(people, new Person());
writeFile(people, "C:/temp/PersonInfo.txt");
ArrayList<Person> sorted= readFile("C:/temp/PersonInfo.txt");
System.out.println("............Sorted..............");
System.out.println("Last Name First Name");
for (Person person : sorted) {
System.out.println(person.getLastName()+" "+person.getFirstName());
}
}
/**
* @param list of persons-people and outfile path
* @param outFile writes persons to outfile
*/
public static void writeFile(ArrayList<Person> people, String outFile) {
// write to file
BufferedWriter writer;
try {
writer = new BufferedWriter(new FileWriter(outFile));
for (Person person : people) {
writer.write(person.toString());
writer.write(System.getProperty("line.separator"));
}
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @param fileName
* @return list of person details available in the input file
*/
public static ArrayList<Person> readFile(String fileName) {
ArrayList<Person> sortedList=new ArrayList<Person>();
Scanner in;
try {
in = new Scanner(new File(fileName));
String line;
// while file has lines
while (in.hasNextLine()) {
line=in.nextLine();
//split each line with comma ","
String tokens[] = line.split(",");
String firstName=tokens[0].trim();
String lastName=tokens[1].trim();
int age=Integer.parseInt(tokens[2].trim());
String gender=tokens[3].trim();
String ssn=tokens[4].trim();
Person person=new Person(firstName, lastName, age, gender, ssn);
sortedList.add(person);
}
// close scanner
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return sortedList;
}
}
Sample Output
PersonInfo.txt:
Salisbury , Robert , 20 , Male , 123-2345-123
James , Pearson , 21 , Male , 568-2345-123
Marie , Curie , 21 , Female , 880-2345-123
Console output:
............Sorted..............
Last Name First Name
Curie Marie
Pearson James
Robert Salisbury