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

Java for Eclipse Project: The purpose of this project is to gain an understandin

ID: 3703019 • Letter: J

Question

Java for Eclipse Project: The purpose of this project is to gain an understanding of using a hash table, sorting and basic report generation techniques.

You will be provided an input file with each line containing a person’s first name, last name, and zip code separated by commas.

1. Create a hash map.

2. Read each line from the input file.

3. Parse the data into a Person class instance containing firstName, lastName, zipCode. If the parsing is not correct list that line as in error. Hint: there are standard Java library methods to help with the parsing.

4. Add the Person to the hash map. If a person by the same first and last name already exists, display an error and ignore this entry.

5. Sort the entries in the hash map by zip code, last name, and then first name. Hint: a HashMap does not support sorting. You will need to make a copy of all entries in the HashMap into another data structure.

6. Print a report header.

7. Print out the results sorted by zip code with a count of the numbers in each zip code.

8. Print a report summary.

A few sample lines in the input file might be:

Alan,Fontanella,55101

Bryan,Hills,40218

Andrew,Luck,55101

Joe,Smith,55102

Bill,Gates

Andrew,Luck,55102

For this input file, the generated report should look like:

Format error on line 5: Bill,Gates

Entry on line 6 already encountered: Andrew,Luck,55102

Company Zip Code Distribution – April 6, 2018 (Example Header)

Zip Code: 40218

Hills, Bryan

Zip Code 40218: 1 resident

Zip Code: 55101

Fontanella, Alan

Luck, Andrew

Zip Code 55101: 2 residents

Zip Code: 55102

Smith, Joe

Zip Code 55102: 1 resident

Report Summary:

2 Records in error

3 Zip Codes encountered

4 Records Processed

Explanation / Answer

Please find my implementation:

NOTE: Please make sure you place your input file in the correct folder. If using eclipse, the file should be in the project directly and NOT INSIDE src folder.

Person.java
==========


public class Person {
private String firstName;
private String lastName;
private int zipcode;

public Person(String fname, String lname, int zip)
{
firstName = fname;
lastName = lname;
zipcode = zip;
}

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 int getZipcode() {
return zipcode;
}

public void setZipcode(int zipcode) {
this.zipcode = zipcode;
}


}

SortPersons.java
==============
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class SortPersons {
public static void main(String[] args) {
String filename;
Scanner keyboard = new Scanner(System.in);

System.out.print("Enter input filename: ");
filename = keyboard.next();

try {
//load the file
int lineNo = 0;
int errors = 0;
String line;
Scanner file = new Scanner(new File(filename));
HashMap<String, Person> map = new HashMap<String, Person>();


while(file.hasNextLine())
{
line = file.nextLine();
lineNo++;

String[] tokens = line.split(",");
if(tokens.length != 3)
{
System.out.println("Format error on line " + lineNo + ": " + line);
errors++;
}
else
{
String key = tokens[0] + " " + tokens[1];
key = key.toUpperCase();
if(map.containsKey(key))
{
System.out.println("Entry on line " + lineNo + " already encountered: " + line);
errors++;
}

else
{
Person p = new Person(tokens[0], tokens[1], Integer.parseInt(tokens[2]));
map.put(key, p);
}
}
}
file.close();

ArrayList<Person> list = sortOnZip(map); //sort entries in map
int previousZip = 0;
int numZips = 0;
int count = 0;

//print report
System.out.println();
for(Person p : list){
if(p.getZipcode() != previousZip)
{
if(previousZip != 0)
System.out.println("Zip Code " + previousZip + ": " + count + " residents ");

numZips++;
count = 0;
previousZip = p.getZipcode();
System.out.println("Zip Code: " + previousZip);
}

System.out.println(p.getLastName()+", " + p.getFirstName());
count++;
}
System.out.println("Zip Code " + previousZip + ": " + count + " residents ");

System.out.println("Report Summary:");
System.out.println(errors + " Records in error");
System.out.println(numZips + " Zip Codes encountered");
System.out.println(list.size() + " Records Processed");


} catch (FileNotFoundException e) {

System.out.println(e.getMessage());
}

}

private static ArrayList<Person> sortOnZip(HashMap<String, Person> map)
{
ArrayList<Person> list = new ArrayList<Person>();
for(Person p : map.values())
list.add(p);

//selection sort on zipcode
for(int i = 0; i < list.size(); i++)
{
int minIdx = i;
for(int j = i + 1; j < list.size(); j++)
{
if(list.get(j).getZipcode() < list.get(minIdx).getZipcode())
minIdx = j;
}

if(minIdx != i)
{
Person temp = list.get(i);
list.set(i, list.get(minIdx));
list.set(minIdx, temp);
}
}

return list;
}
}

output
=====
Enter input filename: sample1.txt
Format error on line 5: Bill,Gates
Entry on line 6 already encountered: Andrew,Luck,55102

Zip Code: 40218
Hills, Bryan
Zip Code 40218: 1 residents

Zip Code: 55101
Luck, Andrew
Fontanella, Alan
Zip Code 55101: 2 residents

Zip Code: 55102
Smith, Joe
Zip Code 55102: 1 residents

Report Summary:
2 Records in error
3 Zip Codes encountered
4 Records Processed

Please DONT forgot to rate my answer. We are working hard for you Guys!!

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