Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases In Networked Information Systems 6th International Workshop Dnis 2010 Aizu Wakamatsu Japan March 2010 Proceedings Lncs 5999

Authors: Shinji Kikuchi ,Shelly Sachdeva ,Subhash Bhalla

2010th Edition

3642120377, 978-3642120374

More Books

Students also viewed these Databases questions