In: Computer Science
*******FOR A BIG THUMBS UP********
------INSTRUCTIONS---------
Coding standards note: The coding standards are in a document in a D2L module titled Coding Standards. See Grading Criteria for points lost if not followed.
Competencies being graded:
Problem Statement:
Create a program that reads in the file people.dat from Homework 2. (Yes, you can use the code from Homework 1). Please make sure to include Person.java in your submission.
Implement your own HashMap implementation. You may use the HashMap example provided in the notes, but you MUST modify it to handle Person objects only and bring it completely up to coding standards to receive all points. You MAY NOT use java.util.HashMap or java.util.TreeMap in this problem.
After reading in the Person objects, store the Personobjects into your HashMap implementation using the unique ID as the key to the map rather than the ArrayList used in Homework 2. Then print the Person objects from the HashMap in a user friendly fashion. You may use iterator or for-each loop-> DO NOT JUST System.out.println THE HASHMAP- ITERATE THROUGH IT. Hint: Modify your toString in Person to get a nice looking output.
------people.dat---------
¬í sr PersonM”fò(Ÿ# I idNumL cityt Ljava/lang/String;L
firstNameq ~ L lastNameq ~ xp t Bufordt Nanat
Agyemansq ~ t Dulutht Josepht Andersonsq ~ t Lawrencevillet Kylet
Brookssq ~ t Daculat Joshuat Broughtonsq ~ t Lilburnt
Demetrit Clarksq ~ t
Snellvillet Davidt Edwardssq ~ t Atlantat Jonit Elshanisq ~ t
Decaturt Jacobt Fagan
------peopleFile.txt-------
Nana
Agyeman
Buford
1
Joseph
Anderson
Duluth
2
Kyle
Brooks
Lawrenceville
3
Joshua
Broughton
Dacula
4
Demetri
Clark
Lilburn
5
David
Edwards
Snellville
6
Joni
Elshani
Atlanta
7
Jacob
Fagan
Decatur
8
--------Person.java-----------
import java.io.Serializable;
public class Person implements Serializable
{
public String firstName;
public String lastName;
public int idNum;
public String city;
/**
* Default constructor used to create empty
attributes
*/
public Person()
{
firstName = "";
lastName = "";
idNum = 0;
city = "";
}
/**
* @param firstName
* @param lastName
* @param idNum
* @param city
*/
public Person(String firstName, String lastName, int
idNum, String city)
{
this.firstName = firstName;
this.lastName = lastName;
this.idNum = idNum;
this.city = city;
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString()
{
return firstName + " " + lastName
;
}
/**
* @return the firstName
*/
public String getFirstName()
{
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName()
{
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName)
{
this.lastName = lastName;
}
/**
* @return the idNum
*/
public int getIdNum()
{
return idNum;
}
/**
* @param idNum the idNum to set
*/
public void setIdNum(int idNum)
{
this.idNum = idNum;
}
/**
* @return the city
*/
public String getCity()
{
return city;
}
/**
* @param city the city to set
*/
public void setCity(String city)
{
this.city = city;
}
}
----------GeneratePeopleFile.java-----------
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;
public class GeneratePeopleFile
{
public static void main(String[] args)
{
// Starting point of program
// open a file of Person class and
read them into an ArrayList of Persons
ArrayList<Person> people =
new ArrayList<Person>();
File peopleFile = new
File("peopleFile.txt");
// open a Scanner to read data from
File
Scanner peopleReader = null;
try
{
peopleReader =
new Scanner(peopleFile);
} catch (FileNotFoundException
e)
{
// TODO
Auto-generated catch block
System.out.println("File not found - terminating program");
System.exit(0);
e.printStackTrace();
}
// read one person at a
time
while
(peopleReader.hasNext())
{
// read first
name
String firstName
= peopleReader.next();
String lastName
= peopleReader.next();
String city =
peopleReader.next();
int id =
peopleReader.nextInt();
// create new
Person instance and add to ArrayList
Person temp =
new Person(firstName, lastName, id, city);
people.add(temp);
}
// print info to user
System.out.println("The people from
the file are:");
System.out.println(people);
// write people to another
file
File secondPeopleFile = new
File("people.dat");
ObjectOutputStream peopleWrite =
null;
try
{
peopleWrite =
new ObjectOutputStream(new FileOutputStream(secondPeopleFile));
for (Person
temp : people)
{
peopleWrite.writeObject(temp);
}
peopleWrite.close();
} catch (IOException e)
{
System.out.println("Problems writing to file");
// TODO
Auto-generated catch block
e.printStackTrace();
}
}
}
Short Summary:
**************Please do upvote to appreciate our time.
Thank you!******************
Source Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class GeneratePeopleFile
{
public static void main(String[] args)
{
// Starting point of program
// open a file of Person class and read them into an ArrayList of
Persons
// ArrayList<Person> people = new
ArrayList<Person>();
//Declare hashmap with unique id as key and Person
object as Value
HashMap<Integer, Person> personMap=new
HashMap<>();
File peopleFile = new
File("C:\\Users\\asus\\peopleFile.txt");
// open a Scanner to read data from File
Scanner peopleReader = null;
try
{
peopleReader = new Scanner(peopleFile);
} catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
System.out.println("File not found - terminating program");
System.exit(0);
e.printStackTrace();
}
// read one person at a time
while (peopleReader.hasNext())
{
// read first name
String firstName = peopleReader.next();
String lastName = peopleReader.next();
String city = peopleReader.next();
int id = peopleReader.nextInt();
// create new Person instance and add to ArrayList
Person temp = new Person(firstName, lastName, id, city);
personMap.put(id, temp);
// people.add(temp);
}
// print info to user
System.out.println("The people from the file are:");
// using for-each loop for iteration over Map.entrySet()
for (Map.Entry<Integer,Person> entry :
personMap.entrySet())
System.out.println( entry.getValue().toString());
// personMap.forEach((k,v) ->
System.out.println(v.toString()));
// write people to another file
File secondPeopleFile = new
File("C:\\Users\\asus\\people.dat");
ObjectOutputStream peopleWrite = null;
try
{
peopleWrite = new ObjectOutputStream(new
FileOutputStream(secondPeopleFile));
// using for-each loop for iteration over Map.entrySet()
for (Map.Entry<Integer,Person> entry :
personMap.entrySet())
peopleWrite.writeObject(entry.getValue());
/* for (Person temp : people)
{
peopleWrite.writeObject(temp);
}*/
peopleWrite.close();
} catch (IOException e)
{
System.out.println("Problems writing to file");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Person.java
import java.io.Serializable;
public class Person implements Serializable
{
public String firstName;
public String lastName;
public int idNum;
public String city;
/**
* Default constructor used to create empty
attributes
*/
public Person()
{
firstName = "";
lastName = "";
idNum = 0;
city = "";
}
/**
* @param firstName
* @param lastName
* @param idNum
* @param city
*/
public Person(String firstName, String lastName, int
idNum, String city)
{
this.firstName = firstName;
this.lastName = lastName;
this.idNum = idNum;
this.city = city;
}
@Override
public String toString() {
return "[firstName=" + firstName +
", lastName=" + lastName + ", idNum=" + idNum + ", city=" + city +
"]";
}
/**
* @return the firstName
*/
public String getFirstName()
{
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName()
{
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName)
{
this.lastName = lastName;
}
/**
* @return the idNum
*/
public int getIdNum()
{
return idNum;
}
/**
* @param idNum the idNum to set
*/
public void setIdNum(int idNum)
{
this.idNum = idNum;
}
/**
* @return the city
*/
public String getCity()
{
return city;
}
/**
* @param city the city to set
*/
public void setCity(String city)
{
this.city = city;
}
}
Code Screenshot:
Person.java
Output:
**************Please do upvote to appreciate our time.
Thank you!******************