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

This is Java programing help. I basically have all the program done I just need

ID: 3817731 • Letter: T

Question

This is Java programing help. I basically have all the program done I just need help getting the data run through a scanner and into the program. I need to have the data ran in through a txt.file rather than having in read directly in the program i have most of program apart from this done. Also please attach the txt file as well. Thanks for the help.

Also I cannot seem to figure out my output is puting random commas in and also if you could fix the program from printing the array brackets i would appericate it

Person.java

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;
if (signedupDate.compareTo(p1.signedupDate) == 0) {
if (this.lastName.compareTo(p1.lastName) == 0) {
return this.firstName.compareTo(p1.firstName);
} else

{
return this.lastName.compareTo(p1.lastName);
}
} else {
return signedupDate.compareTo(p1.signedupDate);
}

}
public String toString() {
return "First Name: " + firstName + " " + "Last Name: " + lastName + " " + "Sign Up Date: " + signedupDate + " ";
}
}

MobilePurchase.java

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;

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 MobilePurchase {

public static void main(String arg[]) {

   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";
String date5 = "01-03-2017 23:00:00.000";
String date6 = "15-03-2017 23:00:00.000";
  
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS");
List < Person > peopleList = new ArrayList < Person > ();
Date date = new Date();

try {

date = formatter.parse(date1);
Person p1 = new Person("Bob", "Stevens", date);
peopleList.add(p1);
  
date = formatter.parse(date2);
Person p2 = new Person("Kerry", "Lens", date);
peopleList.add(p2);
  
date = formatter.parse(date3);
Person p3 = new Person("Steven", "Odell", date);
peopleList.add(p3);
  
date = formatter.parse(date4);
Person p4 = new Person("Brin", "Masters", date);
peopleList.add(p4);

date = formatter.parse(date5);
Person p5 = new Person("Drew", "Galloway", date);
peopleList.add(p5);

date = formatter.parse(date6);
Person p6 = new Person("Becky", "james", date);
peopleList.add(p6);
  
} 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(peopleList);
//Sorting list based on signedup date defined in person class
Collections.sort(peopleList);
System.out.println();
//Display list after sorted
System.out.println("Person detailed list after sorting based on signedup date........");
System.out.println(peopleList);

}
}

Here is the full requirments if you are wondering

home / study / engineering / computer science / questions and answers / need some help finshing a java problem i have most ...

Question: Need some help finshing a java problem I have most...

Bookmark

Need some help finshing a java problem I have most of the code done just cannot seem to get the last requirments of the program.

I am having issue sorting signups by date. My issue is if the have the same last name i cannot seem to get my arraylist in the correct order. Thanks for the help.

Code is belpw

Person.java

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 + " " + "Sign Up Date: " + signedupDate+ " ";
}

}

MobilePurchase.java


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 MobilePurchase {

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";
String date5 = "01-03-2017 23:00:00.000";
String date6 = "15-03-2017 23:00:00.000";
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS");
List < Person > peopleList = new ArrayList < Person > ();
Date date = new Date();

try {


date = formatter.parse(date1);
Person p1 = new Person("Bob", "Stevens", date);
peopleList.add(p1);

date = formatter.parse(date2);
Person p2 = new Person("Kerry", "Lens", date);
peopleList.add(p2);

date = formatter.parse(date3);
Person p3 = new Person("Steven", "Odell", date);
peopleList.add(p3);

date = formatter.parse(date4);
Person p4 = new Person("Brin", "Masters", date);
peopleList.add(p4);
  
date = formatter.parse(date5);
Person p5 = new Person("Drew", "Galloway", date);
peopleList.add(p5);
  
date = formatter.parse(date6);
Person p6 = new Person("Becky", "james", date);
peopleList.add(p6);

} 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(peopleList);

//Sorting list based on signedup date defined in person class
Collections.sort(peopleList);
System.out.println();
//Display list after sorted
System.out.println("Person detailed list after sorting based on signedup date........");
System.out.println(peopleList);


//Using Comparator if date is same then sorting based on last name then first name
PersonComparator comp = new PersonComparator();
Collections.sort(peopleList, 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(peopleList);
}

PersonComparator.java

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);
}

}

Here are the full system requirments if interested

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

Please find the updated solution: /* Updated Person class */ import java.util.Date; public class Person{ private String firstName; private String lastName; private Date signedupDate; // argument contructor Person(String firstName, String lastName, Date date) { this.firstName = firstName; this.lastName = lastName; this.signedupDate = date; } //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 Date getSignedupDate() { return signedupDate; } public void setSignedupDate( Date signedupDate ) { this.signedupDate = signedupDate; } //formatted toString method of person public String toString() { return "First Name: " + firstName + " " + "Last Name: " + lastName + " " + "Sign Up Date: " + signedupDate + " "; } } ----------------------------------- import java.util.Comparator; /** * Updated PersonComparator to compare based on signupdate then last name and then first name */ public class PersonComparator implements Comparator { @Override public int compare( Person o1, Person o2 ) { int res = o1.getSignedupDate().compareTo(o2.getSignedupDate()); if( res == 0 ) { res = o1.getLastName().compareTo(o2.getLastName()); if( res == 0 ) return o1.getFirstName().compareTo(o2.getFirstName()); return res; } return res; } } ----------------------------------- import java.io.FileNotFoundException; import java.io.FileReader; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; // Update test class public class MobilePurchase { public static void main( String[] args ) throws FileNotFoundException, ParseException { // Declare formatter DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS"); List peopleList = new ArrayList(); // Update scanner to read from file Scanner fileScanner = new Scanner(new FileReader("purchase.txt")); // iterate through each line, create Person object, add to List while( fileScanner.hasNext() ) { String line = fileScanner.nextLine(); String[] details = line.split(","); peopleList.add(new Person(details[0], details[1], formatter.parse(details[2]))); } // Print the result before sorting System.out.println("Person detailed list before sorting........"); for(Person person : peopleList) System.out.println(person); // sort the list with comparator peopleList.sort(new PersonComparator()); // Print the result after sorting System.out.println("Person detailed list after sorting as required.............."); for(Person person : peopleList) System.out.println(person); fileScanner.close(); } } ----------------------------------------- Input text file: purchase.txt Bob, Stevens, 31-03-2017 23:00:00.000 Kerry, Lens, 10-03-2017 23:00:00.000 Steven, Odell, 15-03-2017 23:00:00.000 Brin, Masters, 01-03-2017 23:00:00.000 Drew, Galloway, 01-03-2017 23:00:00.000 Becky, james, 15-03-2017 23:00:00.000 Sample output Person detailed list before sorting........ First Name: Bob Last Name: Stevens Sign Up Date: Fri Mar 31 23:00:00 IST 2017 First Name: Kerry Last Name: Lens Sign Up Date: Fri Mar 10 23:00:00 IST 2017 First Name: Steven Last Name: Odell Sign Up Date: Wed Mar 15 23:00:00 IST 2017 First Name: Brin Last Name: Masters Sign Up Date: Wed Mar 01 23:00:00 IST 2017 First Name: Drew Last Name: Galloway Sign Up Date: Wed Mar 01 23:00:00 IST 2017 First Name: Becky Last Name: james Sign Up Date: Wed Mar 15 23:00:00 IST 2017 Person detailed list after sorting as required.............. First Name: Drew Last Name: Galloway Sign Up Date: Wed Mar 01 23:00:00 IST 2017 First Name: Brin Last Name: Masters Sign Up Date: Wed Mar 01 23:00:00 IST 2017 First Name: Kerry Last Name: Lens Sign Up Date: Fri Mar 10 23:00:00 IST 2017 First Name: Steven Last Name: Odell Sign Up Date: Wed Mar 15 23:00:00 IST 2017 First Name: Becky Last Name: james Sign Up Date: Wed Mar 15 23:00:00 IST 2017 First Name: Bob Last Name: Stevens Sign Up Date: Fri Mar 31 23:00:00 IST 2017
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote