I have this long java code. I want to remove methods without damaging the progra
ID: 3701025 • Letter: I
Question
I have this long java code. I want to remove methods without damaging the program. I want to keep the methods that help me read the text file, add new contact, and to print the list. thank you in advance..
this is my text file:
Winston, Maikayla, 123-456-7890, maikaylarw@yahoo.com, 06/26/1998
Jones, Robert, 123-456-7890, robertjones@yahoo.com, 12/11/87
Williams, Marie, 123-456-7890, mariew@yahoo.com, 02/12/78
Walker, Fred, 123-456-7890, fwalker@yahoo.com, 12/31//97
Sinclar, Taylor, 123-456-7890, tsinclair@yahoo.com, 10/04/98
My program..:
import java.util.*;
import java.io.*;
public class ContactDLL {
public static void main(String[] args) throws NumberFormatException, IOException {
ContactDoublyLinkedListImpl<Contact> contact_list = new ContactDoublyLinkedListImpl<Contact>();
File file = new File("contacts.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st = br.readLine()) != null){
String contact[] = st.split(",");
contact_list.addLast(new Contact(contact[0].trim(),contact[1].trim(),contact[2].trim(),contact[3].trim(),contact[4].trim()));
}
//adding new contact
contact_list.addLast(new Contact("Jenny","Singer","334-556-3343","jenny@yahoo.com","12/11/89"));
br.close();
String last_name ="Singer";
FileWriter writer = new FileWriter(last_name+"Contacts.txt");
BufferedWriter bw = new BufferedWriter(writer);
Iterator<Contact> it = contact_list.iterator();
while (it.hasNext()) {
Contact contact = (Contact) it.next();
bw.write(contact.toString());
bw.newLine();
}
bw.close();
}
}
class ContactDoublyLinkedListImpl<E> implements Iterable<E> {
private Node head;
private Node tail;
private int size;
public ContactDoublyLinkedListImpl() {
size = 0;
}
/**
* this class keeps track of each element information
* @author java2novice
*
*/
private class Node {
E element;
Node next;
Node prev;
public Node(E element, Node next, Node prev) {
this.element = element;
this.next = next;
this.prev = prev;
}
}
@Override
public Iterator<E> iterator() {
// TODO Auto-generated method stub
return new Iterator<E>() {
Node current = head;
@Override
public boolean hasNext() {
return current != null;
}
@Override
public E next() {
if (hasNext()) {
E data = current.element;
current = current.next;
return data;
}
return null;
}
@Override
public void remove() {
throw new UnsupportedOperationException("Remove not implemented.");
}
};
}
}
class Contact{
String first_name;
String last_name;
String phone_num;
String email;
String dob;
public Contact(){
}
public Contact(String first_name, String last_name, String phone_num,
String email, String dob) {
super();
this.first_name = first_name;
this.last_name = last_name;
this.phone_num = phone_num;
this.email = email;
this.dob = dob;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return this.first_name+", "+this.last_name+", "+this.phone_num+", "+this.email+", "+this.dob;
}
}
Explanation / Answer
You can remove the whole
class instead use ArrayList for your use case.
Here is the final code