Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Merge the following two text files to another text file named \"allstudents.txt\

ID: 3844982 • Letter: M

Question

Merge the following two text files to another text file named "allstudents.txt". The new file should be sorted in terms of the students' graduation date from the nearest to furthest. (USING JAVA PROGRAMMING LANGUAGE)

Text file: boys.txt

NAME,ID,GRADUATION_DATE,AGE

1. John, 6, 2020-07-01 10:50:00 EST, 22

2. Mike, 9, 2020-07-07 10:40:00 EST, 23

3. Lucas, 10, 2020-07-07 10:42:00 EST, 23

Text file: girls.txt

NAME,ID,GRADUATION_DATE,AGE

1. Emma, 7, 2020-06-02 10:50:00 EST, 22

2. Olivia, 4, 2020-07-06 10:40:00 EST, 21

3. Sophia, 1, 2020-06-01 10:42:00 EST, 23

The output should be the following:

NAME,ID,GRADUATION_DATE,AGE

1. Sophia, 1, 2020-06-01 10:42:00 EST, 23

2. Emma, 7, 2020-06-02 10:50:00 EST, 22

3.  John, 6, 2020-07-01 10:50:00 EST, 22

4.  Olivia, 4, 2020-07-06 10:40:00 EST, 21

5. Mike, 9, 2020-07-07 10:40:00 EST, 23

6.  Lucas, 10, 2020-07-07 10:42:00 EST, 23

Explanation / Answer

below is your code: -

Person.java


import java.util.Date;

public class Person implements Comparable<Person> {
   private String name;
   private int ID;
   private Date gradDate;
   private int age;

   public Person(String name, int iD, Date gradDate, int age) {
       this.name = name;
       ID = iD;
       this.gradDate = gradDate;
       this.age = age;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public int getID() {
       return ID;
   }

   public void setID(int iD) {
       ID = iD;
   }

   public Date getGradDate() {
       return gradDate;
   }

   public void setGradDate(Date gradDate) {
       this.gradDate = gradDate;
   }

   public int getAge() {
       return age;
   }

   public void setAge(int age) {
       this.age = age;
   }

   @Override
   public int compareTo(Person o) {
       if (this.getGradDate().before(o.getGradDate())) {
           return -1;
       } else if (this.getGradDate().after(o.getGradDate())) {
           return 1;
       } else
           return 0;
   }

  
  
}

MergeFiles.java


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TimeZone;

public class MergeFiles {
   public static void main(String[] args) throws FileNotFoundException, ParseException {
       ArrayList<Person> persons = new ArrayList<>();
       Scanner scan1 = new Scanner(new File("boys.txt"));
       Scanner scan2 = new Scanner(new File("girls.txt"));

       scan1.nextLine();
       scan2.nextLine();
       String name, line;
       int id, age;
       Date d;
       String[] words;
       DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
       while (scan1.hasNextLine()) {
           line = scan1.nextLine();
           words = line.split(",");
           name = words[0];
           id = Integer.parseInt(words[1]);
           d = (Date) formatter.parse(words[2]);
           age = Integer.parseInt(words[3]);
           persons.add(new Person(name, id, d, age));
       }
       while (scan2.hasNextLine()) {
           line = scan2.nextLine();
           words = line.split(",");
           name = words[0];
           id = Integer.parseInt(words[1]);
           d = (Date) formatter.parse(words[2]);
           age = Integer.parseInt(words[3]);
           persons.add(new Person(name, id, d, age));
       }

       Collections.sort(persons);
       PrintWriter pw = new PrintWriter(new FileOutputStream("output.txt"));
       int c = 0;
       pw.println("NAME,ID,GRADUATION_DATE,AGE");
       for (Person p : persons)
           pw.println((++c) + ". " + p.getName() + " ," + p.getID() + " ," + formatDateToString(p.getGradDate(),"yyyy-MM-dd HH:mm:ss 'EST'","EST") + " ,"
                   + p.getAge());
       pw.close();
   }

   public static String formatDateToString(Date date, String format, String timeZone) {
       // null check
       if (date == null)
           return null;
       SimpleDateFormat sdf = new SimpleDateFormat(format);
       // default system timezone if passed null or empty
       if (timeZone == null || "".equalsIgnoreCase(timeZone.trim())) {
           timeZone = Calendar.getInstance().getTimeZone().getID();
       }
       sdf.setTimeZone(TimeZone.getTimeZone(timeZone));
       return sdf.format(date);
   }
}

boys.txt

NAME,ID,GRADUATION_DATE,AGE
John,6,2020-07-01 10:50:00 EST,22
Mike,9,2020-07-07 10:40:00 EST,23
Lucas,10,2020-07-07 10:42:00 EST,23

girls.txt

NAME,ID,GRADUATION_DATE,AGE
Emma,7,2020-06-02 10:50:00 EST,22
Olivia,4,2020-07-06 10:40:00 EST,21
Sophia,1,2020-06-01 10:42:00 EST,23

Output.txt

NAME,ID,GRADUATION_DATE,AGE
1. Sophia ,1 ,2020-06-01 10:42:00 EST ,23
2. Emma ,7 ,2020-06-02 10:50:00 EST ,22
3. John ,6 ,2020-07-01 10:50:00 EST ,22
4. Olivia ,4 ,2020-07-06 10:40:00 EST ,21
5. Mike ,9 ,2020-07-07 10:40:00 EST ,23
6. Lucas ,10 ,2020-07-07 10:42:00 EST ,23