Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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..

import java.util.*;

import java.io.*;

public class ContactLinkList {

public static void main(String[] args) throws NumberFormatException, IOException {

// TODO Auto-generated method stub

ContactDoublyLinkedListImpl contact_list = new ContactDoublyLinkedListImpl();

File file = new File("C:\\Users\\Admin\\Desktop\\data.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("C:\\Users\\Admin\\Desktop\\"+last_name+"Contacts.txt");

BufferedWriter bw = new BufferedWriter(writer);

Iterator it = contact_list.iterator();

while (it.hasNext()) {

Contact contact = (Contact) it.next();

bw.write(contact.toString());

bw.newLine();

}

bw.close();

}

}

class ContactDoublyLinkedListImpl implements Iterable {

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;

}

}

/**

* returns the size of the linked list

* @return

*/

public int size() { return size; }

/**

* return whether the list is empty or not

* @return

*/

public boolean isEmpty() { return size == 0; }

/**

* adds element at the starting of the linked list

* @param element

*/

public void addFirst(E element) {

Node tmp = new Node(element, head, null);

if(head != null ) {head.prev = tmp;}

head = tmp;

if(tail == null) { tail = tmp;}

size++;

System.out.println("adding: "+element);

}

/**

* adds element at the end of the linked list

* @param element

*/

public void addLast(E element) {

Node tmp = new Node(element, null, tail);

if(tail != null) {tail.next = tmp;}

tail = tmp;

if(head == null) { head = tmp;}

size++;

System.out.println("adding: "+element);

}

/**

* this method walks forward through the linked list

*/

public void iterateForward(){

System.out.println("iterating forward..");

Node tmp = head;

while(tmp != null){

System.out.println(tmp.element);

tmp = tmp.next;

}

}

/**

* this method walks backward through the linked list

*/

public void iterateBackward(){

System.out.println("iterating backword..");

Node tmp = tail;

while(tmp != null){

System.out.println(tmp.element);

tmp = tmp.prev;

}

}

/**

* this method removes element from the start of the linked list

* @return

*/

public E removeFirst() {

if (size == 0) throw new NoSuchElementException();

Node tmp = head;

head = head.next;

head.prev = null;

size--;

System.out.println("deleted: "+tmp.element);

return tmp.element;

}

/**

* this method removes element from the end of the linked list

* @return

*/

public E removeLast() {

if (size == 0) throw new NoSuchElementException();

Node tmp = tail;

tail = tail.prev;

tail.next = null;

size--;

System.out.println("deleted: "+tmp.element);

return tmp.element;

}

@Override

public Iterator iterator() {

// TODO Auto-generated method stub

return new Iterator() {

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;

}

}

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

Database Management An Organizational Perspective

Authors: Richard T. Watson

1st Edition

0471305340, 978-0471305347

More Books

Students also viewed these Databases questions