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

: Create java code to track a contacts information. Each contact type has at lea

ID: 3714719 • Letter: #

Question

: Create java code to track a contacts information. Each contact type has at least five different fields. Read contact information from a file and write a method display Polymorphic ally all contacts information. Your code must satisfy the following requirements 1. Use Inheritance, Polymorphism, Abstract Classes and Interfaces. 2. Each subtype is coded as a separate class. 3. Each contact type has at least 5 fields in Employment and Friend contacts with at least one field different from the other 4. Store data in text files 5. Include a minimum of 4 different objects of each contact type.

Explanation / Answer

Code:

Contact.java

import java.io.Serializable;

//Using Serializable interface

public class Contact implements Serializable {

/**

*

*/

private static final long serialVersionUID = 1L;

private String name;

private int contactNumber;

private String address;

private Contact friend;

private Employment employer;

@Override

public String toString() {

return "Contact [name=" + name + ", contactNumber=" + contactNumber + ", address=" + address + ", friend="

+ friend + ", employer=" + employer + "]";

}

public Contact(String name, int contactNumber, String address, Contact friend, Employment employer) {

super();

this.name = name;

this.contactNumber = contactNumber;

this.address = address;

this.friend = friend;

this.employer = employer;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getContactNumber() {

return contactNumber;

}

public void setContactNumber(int contactNumber) {

this.contactNumber = contactNumber;

}

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

public Contact getFriend() {

return friend;

}

public void setFriend(Contact friend) {

this.friend = friend;

}

public Employment getEmployer() {

return employer;

}

public void setEmployer(Employment employer) {

this.employer = employer;

}

}

ReadWriteMain.java

import java.util.ArrayList;

import java.util.List;

public class ReadWriteMain {

public static void main(String args[]) {

ReaderWriter rw = new FileReaderWriter();

String fileName = "src/data.txt";

List<Contact> contacts = new ArrayList<Contact>();

contacts.add(new Contact("test1", 1232432, "address1", null, new Employment("employer1", "staff1")));

contacts.add(new Contact("test2", 1232432, "address2", null, new Employment("employer2", "staff2")));

contacts.add(new Contact("test3", 1232432, "address3", null, new Employment("employer3", "staff3")));

contacts.add(new Contact("test4", 1232432, "address4", null, new Employment("employer4", "staff4")));

contacts.add(new Contact("test5", 1232432, "address5", null, new Employment("employer5", "staff5")));

rw.writeDataToFile(contacts, fileName);

List<Contact> readContacts = rw.readDataFromFile(fileName);

for (Contact contact : readContacts) {

System.out.println(contact);

}

}

}

Employment.java

import java.io.Serializable;

public class Employment implements Serializable{

/**

*

*/

private static final long serialVersionUID = 1L;

private String employer;

private String designation;

public Employment(String employer, String designation) {

super();

this.employer = employer;

this.designation = designation;

}

public String getEmployer() {

return employer;

}

@Override

public String toString() {

return "Employment [employer=" + employer + ", designation=" + designation + "]";

}

public void setEmployer(String employer) {

this.employer = employer;

}

public String getDesignation() {

return designation;

}

public void setDesignation(String designation) {

this.designation = designation;

}

}

Abstract class ReaderWriter.java

import java.util.List;

public abstract class ReaderWriter {

abstract List<Contact> readDataFromFile(String fileName);

abstract void writeDataToFile(List<Contact> contacts, String fileName);

}

FileReaderWriter.java with polymorphism

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.util.List;

//Using Abstract class

public class FileReaderWriter extends ReaderWriter {

List<Contact> readDataFromFile(String fileName) {

List<Contact> contacts = null;

try {

FileInputStream fileIs = new FileInputStream(fileName);

ObjectInputStream objIs = new ObjectInputStream(fileIs);

contacts = (List<Contact>) objIs.readObject();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return contacts;

}

// Polymorphism

void printFile(String fileName) {

List<Contact> contacts = null;

try {

FileInputStream fileIs = new FileInputStream(fileName);

ObjectInputStream objIs = new ObjectInputStream(fileIs);

contacts = (List<Contact>) objIs.readObject();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

for (Contact contact : contacts) {

System.out.println(contact);

}

}

// Polymorphism

void printFile(File file) {

List<Contact> contacts = null;

try {

FileInputStream fileIs = new FileInputStream(file);

ObjectInputStream objIs = new ObjectInputStream(fileIs);

contacts = (List<Contact>) objIs.readObject();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

for (Contact contact : contacts) {

System.out.println(contact);

}

}

void writeDataToFile(List<Contact> contacts, String fileName) {

File f = new File(fileName);

// String f = "src/data.txt";

try {

FileOutputStream ops = new FileOutputStream(fileName);

ObjectOutputStream objOps = new ObjectOutputStream(ops);

// for (Contact c : contacts) {

objOps.writeObject(contacts);

// objOps.writeByte(" ");

// }

objOps.flush();

objOps.close();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

Comment: All the best, FileReaderWriter have the polymorphic method printFile. Interface used Serializable, abstract class used ReaderWriter.

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