Hello I need help meeting a requirment for a java file. I need to have the data
ID: 3817523 • Letter: H
Question
Hello I need help meeting a requirment for a java file. 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
Note : Just change the file path in the following code to the path where you save "data.txt" file
------------------------------------------------------------------------------------------------------------------------------
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
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[]) throws IOException, ParseException {
String filePath = "/home/zubair/Desktop/data.txt";
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS");
/* Read the data in from data.txt file */
List<Person> peopleList = readDataFromFile(filePath, formatter);
/* Display list before sorted */
System.out.println("List of Persons before sorting");
printList(peopleList);
/* Sorting list based on signedup date defined in person class */
Collections.sort(peopleList);
System.out.println(" List of Persons after sorting");
printList(peopleList);
}
public static void printList(List<Person> peopleList) {
for (Person person : peopleList) {
System.out.println(person.toString());
}
}
public static List<Person> readDataFromFile(String filePath, DateFormat formatter)
throws IOException, ParseException {
List<Person> resultList = new ArrayList<Person>();
/* open the file for reading */
BufferedReader bufferedInput = new BufferedReader(new FileReader(filePath));
/*
* read each line from file and add it to the List.
*/
String inputLine;
while ((inputLine = bufferedInput.readLine()) != null) {
String attributes[] = inputLine.trim().split(",");
String firstName = attributes[0].trim();
String lastName = attributes[1].trim();
Date date = formatter.parse(attributes[2].trim());
Person person = new Person(firstName, lastName, date);
resultList.add(person);
}
/* Close the buffered reader and return the list */
bufferedInput.close();
return resultList;
}
}
------------------------------------------------------------------------------------------------------------------------------------
import java.util.Date;
public class Person implements Comparable<Object> {
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.toLowerCase().compareTo(p1.lastName.toLowerCase()) == 0) {
return this.firstName.toLowerCase().compareTo(p1.firstName.toLowerCase());
} else
{
return this.lastName.toLowerCase().compareTo(p1.lastName.toLowerCase());
}
} else {
return signedupDate.compareTo(p1.signedupDate);
}
}
public String toString() {
return "First Name: " + firstName + " " + "Last Name: " + lastName + " " + "Sign Up Date: " + signedupDate + " ";
}
}
------------------------------------------------------------------------------------------------------------------------------
filename is data.txt
------------------------------------------------------------------------------------------------------------------------------
Drew,Galloway,01-03-2017 23:00:00.000
Brin,Masters,01-03-2017 23:00:00.000
Kerry,Lens,10-03-2017 23:00:00.000
Steven,Odell,15-03-2017 23:00:00.000
Becky,james,15-03-2017 23:00:00.000
Bob,Stevens,31-03-2017 23:00:00.000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.