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

Please write this program in java In this program, you are to create a new progr

ID: 3816352 • Letter: P

Question

Please write this program in java

In this program, you are to create a new program that allows people to purchase the new Galax S8 or S8+. To do this, you are to read in a file (that you create – please have a minimum of 5 people in your file) that contains the following fields: First Name, Last Name, Date Signed up. You are to use the following format for the date in your file. *Hint* use the formatter below to create a date object.

String test = "31-12-1969 23:00:00.000";

DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS");

Date date = formatter.parse(test);

You are to use an ArrayList to store your people.

You are going to create a system that will sort these people using either InsertionSort or SelectionSort (your choice) based on the following criteria:

a. Sort by date signed up. You must sort using the Date Object.

b. If they have the same sign up date, they should be sorted by last name and then first name. (Please have an example of this).

c. This is a console application d. You must implement the Comparable interface in your Person class.

e. You cannot have more than one compareTo method.

f. The sorted list should appear in the console.

Explanation / Answer


//Person Class code is here.....
package com.purchage;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Person implements Comparable{

   public String firstName;
   public String lastName;
   public Date signedupDate;
  
   Person(){}
   Person(String firstName,String lastName,Date date){
       this.firstName=firstName;
       this.lastName=lastName;
       this.signedupDate=date;
   }
  
  
   @Override
   public int compareTo(Object p) {
       Person p1 = (Person)p;
       return this.signedupDate.compareTo(p1.signedupDate);
   }
  
   public String toString(){
       return "{First Name: "+firstName+","+"Last Name: "+lastName+","+"Signedup Date: "+signedupDate+"}";
   }
  
}


// MobilePurchage class code is here.....

package com.purchage;

package com.purchage;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class MobilePurchage {
  
   public static void main(String[] args) {
      
       String date1 = "31-03-2017 23:00:00.000";
       String date2 = "10-03-2017 23:00:00.000";
       String date3 = "15-03-2017 23:00:00.000";
       String date4 = "01-03-2017 23:00:00.000";
       DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS");
       List<Person> pepoleList=new ArrayList<Person>();
       Date date=new Date();
  
           try {
              
              
               date=formatter.parse(date1);
               Person p1=new Person("Niraj","Kumar",date);
               pepoleList.add(p1);
              
               date=formatter.parse(date2);
               Person p2=new Person("Rahul","Kumar",date);
               pepoleList.add(p2);
              
               date=formatter.parse(date3);
               Person p3=new Person("Santosh","Kumar",date);
               pepoleList.add(p3);
              
               date=formatter.parse(date4);
               Person p4=new Person("Alok","Kumar",date);
               pepoleList.add(p4);
              
           } catch (ParseException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }

      
       //Display list before sorted
       System.out.println("Person detailed list before sorting........");
       System.out.println(pepoleList);
      
       //Sorting list based on signedup date defined in person class
       Collections.sort(pepoleList);
       System.out.println();
       //Display list after sorted
       System.out.println("Person detailed list after sorting based on signedup date........");
       System.out.println(pepoleList);
      
      
       //Using Comparator if date is same then sorting based on last name then first name
       PersonComparator comp=new PersonComparator();
       Collections.sort(pepoleList,comp);
       System.out.println();
       System.out.println(" Person detailed list after sorting based on last name followed by first name if date is same....");
       System.out.println(pepoleList);
   }

}


// Find here for comparator class code...

package com.purchage;

import java.util.Comparator;

public class PersonComparator implements Comparator<Person>{

   @Override
   public int compare(Person p1, Person p2) {
       return p1.lastName.compareTo(p2.lastName)-p1.firstName.compareTo(p2.firstName);
   }

}

// Please find here for Console output is here....

Person detailed list before sorting........
[{First Name: Niraj,Last Name: Kumar,Signedup Date: Fri Mar 31 23:00:00 IST 2017}, {First Name: Rahul,Last Name: Kumar,Signedup Date: Fri Mar 10 23:00:00 IST 2017}, {First Name: Santosh,Last Name: Kumar,Signedup Date: Wed Mar 15 23:00:00 IST 2017}, {First Name: Alok,Last Name: Kumar,Signedup Date: Wed Mar 01 23:00:00 IST 2017}]

Person detailed list after sorting based on signedup date........
[{First Name: Alok,Last Name: Kumar,Signedup Date: Wed Mar 01 23:00:00 IST 2017}, {First Name: Rahul,Last Name: Kumar,Signedup Date: Fri Mar 10 23:00:00 IST 2017}, {First Name: Santosh,Last Name: Kumar,Signedup Date: Wed Mar 15 23:00:00 IST 2017}, {First Name: Niraj,Last Name: Kumar,Signedup Date: Fri Mar 31 23:00:00 IST 2017}]

Person detailed list after sorting based on last name followed by first name if date is same....
[{First Name: Santosh,Last Name: Kumar,Signedup Date: Wed Mar 15 23:00:00 IST 2017}, {First Name: Rahul,Last Name: Kumar,Signedup Date: Fri Mar 10 23:00:00 IST 2017}, {First Name: Niraj,Last Name: Kumar,Signedup Date: Fri Mar 31 23:00:00 IST 2017}, {First Name: Alok,Last Name: Kumar,Signedup Date: Wed Mar 01 23:00:00 IST 2017}]