In Java, create a program that reads a file containing records for people. Then sort the list of people in ascending order (last name, first name) and then write the output to a new file.
** Here's the majority of the code for main method ** (Add in the necessary code and create the person class) import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Comparator; import java.util.Scanner; import java.io.File; import java.util.ArrayList; public class Main { public static ArrayList getFromFile(String filename) { // open the file //for each line //serialize into a person object //store into array list // return array list return(new ArrayList()); } public static void writeToFile(String filename, ArrayList persons) { // open file //for each person in persons // serialize to specified format //close file } public static void main(String[] args) { // String filename = "" ArrayList persons = getFromFile("10-01.person .csv"); persons.sort(new Comparator() { @Override public int compare(Person person1, Person person2) { //todo int comparison = person1.getLastName() .compareToIgnoreCase(person2.getLastName)()); if (comparison ==0){ comparison = person1.getFirstName() .compareToIgnoreCase(person2.getFirstName()); } // if (person1 > person2); return(1); // if (person1 writeToFile("10-01.person .csv", persons);
** Here's part of the contents of the 10-01.person.csv file ** (Create your own text file and implement it into the code)
id,first_name,last_name 0000000001,Lucienne,Harpur 0000000002,Karolina,Echallier 0000000003,Zaccaria,Grinstead 0000000004,Alyss,Lockey 0000000005,Gus,Brownsea 0000000006,Braden,McTerry 0000000007,Remington,Bilbee 0000000008,Elka,Robottom 0000000009,Modesta,Watterson 0000000010,Martie,Boreham 0000000011,Juan,Tysack 0000000012,Ilaire,Burdass 0000000013,Valentine,Brunon 0000000014,Willy,Dayce 0000000015,Arnuad,Readings 0000000016,Margarette,Janusik 0000000017,Walther,Brummitt 0000000018,Garwood,Detoile 0000000019,Perren,Senogles 0000000020,Eric,Matthiesen 0000000021,Kent,Pietron 0000000022,Darcey,Burkman 0000000023,Clarence,Boag 0000000024,Collete,Orpin 0000000025,Pembroke,McVanamy 0000000026,Curtice,Harmes 0000000027,Cristie,Raden 0000000028,Dorette,Swigg 0000000029,Lisette,Benes 0000000030,Horatius,Slee 0000000031,Robbert,MacAlister 0000000032,Gaby,Pfleger 0000000033,Glyn,Longshaw 0000000034,Alla,Essame 0000000035,Bee,Sebastian 0000000036,Gabriel,Lathan 0000000037,Thatch,Verheyden 0000000038,Janina,Benka 0000000039,Frederigo,Easbie 0000000040,Coralyn,Caldera 0000000041,Brewer,Schneidau 0000000042,Bobbye,Djurevic 0000000043,Cally,Giacobelli 0000000044,Pamela,Newborn 0000000045,Camel,Alvey 0000000046,Con,Gayden 0000000047,Manfred,Masurel 0000000048,Gianina,Feirn 0000000049,Fern,Farry
To get started, you will need to create a Person class. The Plass will have three fields or attributes associated with it. ld-Contains the unique id for the person. Eg. a student id, a social security number, etc. LastName Contains the person's surname FirstName Contains the person's given name You will need to write the necessary constructors, getters and setters for the Person class fields. The constructors, getters and setter methods have NOT been explicitly specified but are still required. What you include or don't include is up to you. In addition, you will need to write the following methods (at a minimum): .getName0 Returns the name as a String value in "
, first> format. So for example my name would be output as "Heeres, George" without the quotes. equals)- Takes an instance of Person as a parameter and compares the lds of the Person objects for equality. We ONLY want to compare the id values. In most modern database systems, when we do equality checks, we are only checking the id values to determine equality. (eg. social security number, etc.) toString0 Returns the id and name in for following format. "[id> last>, first>" For example, if my id had been 11122565, the output would be: 11122565] Heeres, George Note: You probably want to leverage the getName) method to avoid writing duplicate code A file (10.01-person.ccontaining people has been provided for you. The file is in the standard CSV (comma separated values) format. The following is a small example from the file: id,first_name,last_name 0000080001,Lucienne, Harpur 0000080882, Karolina, Echallier 0000080803,Zaccaria, Grinstead 0000080004,Alyss, Lockey 0000080005, Gus, Brownsea 0000080006, Braden,McTerry 0000000007, Remington,Bilbee 0000080008, Elka, Robottom 0000880809, Modesta,Watterson You will notice that the first line of the file contains the column headers for the file. When reading the file, you will need to ignore this first line For each line read from the file, you will need to serialize the data into a Person object. Hint: Look at the String.split method. You will likely want to store this information as it's being read into an ArrayList of some sort. After the contents of the file have been sorted, you will need to sort the records in Last name followed by First name format. Assuming you have used an ArrayList of Person objects, you can use the .sort(0) method of the ArrayList class which requires an anonymous delegate. You will need to implement the compare) method of the Comparator interface. The compare method returns A negative integer if the first argument (person1) is less than the second argument (person2) A zero if the first argument (person1) is equal to the second argument (person2) A positive integer if the first argument (person1) greater than the second argument (person2) An example is provided below. You will need to implement the logic for the compare) method. Optionally, you can write your own sorting algorithm. A number of them have been provided in the book. ArrayList people - ...; people.sort(new Comparator) @0verride public int compare(Person person1, Person person2) //todo // f (person1 // f (person! // f (personi return 0 > person2); return(1); ,", You will need to write a header value to the file. A snippet of the expected output file is included below: id,name 0000000949,Whittock Abdul 0000000828,MacIlhargy Abigael 0000000984, Ambroix Abner 0000000285,Claxton Abran 0000000519,MacFayden Addison 0000000867,Mc Mechan Adelaide 0000000626,Burnie Adelheid Instructions 1. Create your Person class as per the above requirements. 1. Add JavaDoc for your class summary as well as for each of your public methods (including constructors) for your Person class. 2. Write a class and/or method to serialize (i.e. read) the data from the input file (10.01-person.csy ). 1. Add JavaDoc to all of your public methods and/ or class definitions 3. Sort the collection of people read from the file in last name followed by first name order. 4 Write a class and/or method to serialize (i.e. write) the data to the output file (10.01-person-sorted.csv). 1. Add JavaDoc to all of your public methods and/ or class definition:s To get started, you will need to create a Person class. The Plass will have three fields or attributes associated with it. ld-Contains the unique id for the person. Eg. a student id, a social security number, etc. LastName Contains the person's surname FirstName Contains the person's given name You will need to write the necessary constructors, getters and setters for the Person class fields. The constructors, getters and setter methods have NOT been explicitly specified but are still required. What you include or don't include is up to you. In addition, you will need to write the following methods (at a minimum): .getName0 Returns the name as a String value in ", first> format. So for example my name would be output as "Heeres, George" without the quotes. equals)- Takes an instance of Person as a parameter and compares the lds of the Person objects for equality. We ONLY want to compare the id values. In most modern database systems, when we do equality checks, we are only checking the id values to determine equality. (eg. social security number, etc.) toString0 Returns the id and name in for following format. "[id> last>, first>" For example, if my id had been 11122565, the output would be: 11122565] Heeres, George Note: You probably want to leverage the getName) method to avoid writing duplicate code A file (10.01-person.ccontaining people has been provided for you. The file is in the standard CSV (comma separated values) format. The following is a small example from the file: id,first_name,last_name 0000080001,Lucienne, Harpur 0000080882, Karolina, Echallier 0000080803,Zaccaria, Grinstead 0000080004,Alyss, Lockey 0000080005, Gus, Brownsea 0000080006, Braden,McTerry 0000000007, Remington,Bilbee 0000080008, Elka, Robottom 0000880809, Modesta,Watterson You will notice that the first line of the file contains the column headers for the file. When reading the file, you will need to ignore this first line For each line read from the file, you will need to serialize the data into a Person object. Hint: Look at the String.split method. You will likely want to store this information as it's being read into an ArrayList of some sort. After the contents of the file have been sorted, you will need to sort the records in Last name followed by First name format. Assuming you have used an ArrayList of Person objects, you can use the .sort(0) method of the ArrayList class which requires an anonymous delegate. You will need to implement the compare) method of the Comparator interface. The compare method returns A negative integer if the first argument (person1) is less than the second argument (person2) A zero if the first argument (person1) is equal to the second argument (person2) A positive integer if the first argument (person1) greater than the second argument (person2) An example is provided below. You will need to implement the logic for the compare) method. Optionally, you can write your own sorting algorithm. A number of them have been provided in the book. ArrayList people - ...; people.sort(new Comparator) @0verride public int compare(Person person1, Person person2) //todo // f (person1 // f (person! // f (personi return 0 > person2); return(1); ,", You will need to write a header value to the file. A snippet of the expected output file is included below: id,name 0000000949,Whittock Abdul 0000000828,MacIlhargy Abigael 0000000984, Ambroix Abner 0000000285,Claxton Abran 0000000519,MacFayden Addison 0000000867,Mc Mechan Adelaide 0000000626,Burnie Adelheid Instructions 1. Create your Person class as per the above requirements. 1. Add JavaDoc for your class summary as well as for each of your public methods (including constructors) for your Person class. 2. Write a class and/or method to serialize (i.e. read) the data from the input file (10.01-person.csy ). 1. Add JavaDoc to all of your public methods and/ or class definitions 3. Sort the collection of people read from the file in last name followed by first name order. 4 Write a class and/or method to serialize (i.e. write) the data to the output file (10.01-person-sorted.csv). 1. Add JavaDoc to all of your public methods and/ or class definition:s