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

(Java Program) Create a java program that would let you remove/add contact info

ID: 3666915 • Letter: #

Question

(Java Program) Create a java program that would let you remove/add contact info to ur phonebook. Use "Doubly Linked List" to implement the phonebook. Contact infos includes name, surname, phone no. and email. These infos are saved in the txt file called "phonebook.txt" and it should be read by program & inserted in the link list. When contact info is added to linked list, it should be added in right place in terms of alphabetical order(Surname).

(Hint) : Contact information should be defined as a class. Therefore each time you are adding a node, you should create an object of contact information class.

(Input) : Do not input any data input from the console, instead read the input from a file.

Exampe : (from my phonebook.txt file)

John Polvo (random email) 387664310
Peter Smith (random email) 1434767850
Carson Dell (random email) 7901234564

(Output) : The corresponding output for given input is as follows:

Carson Dell (random email) 7901234564
John Polvo (random email) 387664310
Peter Smith (random email) 8901234567

Above is the contents in my .txt phonebook file so make use of those three entries and deliver a working java code, much appreciated, Thanks

Explanation / Answer


/*
* Java Program to Implement Sorted Doubly Linked List
*/

import java.util.Scanner;

import cgegg.AllPositive.ContactInfo;

/* Class Node */
class Node
{
protected ContactInfo data;
protected Node next, prev;

/* Constructor */
public Node()
{
next = null;
prev = null;
data = null;
}
/* Constructor */
public Node(ContactInfo d, Node n, Node p)
{
data = d;
next = n;
prev = p;
}
/* Function to set link to next node */
public void setLinkNext(Node n)
{
next = n;
}
/* Function to set link to previous node */
public void setLinkPrev(Node p)
{
prev = p;
}
/* Funtion to get link to next node */
public Node getLinkNext()
{
return next;
}
/* Function to get link to previous node */
public Node getLinkPrev()
{
return prev;
}
/* Function to set data to node */
public void setData(ContactInfo d)
{
data = d;
}
/* Function to get data from node */
public ContactInfo getData()
{
return data;
}
}

/* Class linkedList */
class linkedList
{
protected Node start;
public int size;

/* Constructor */
public linkedList()
{
start = null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return start == null;
}
/* Function to get size of list */
public int getSize()
{
return size;
}
/* Function to insert element */
public void insert(ContactInfo val)
{
Node nptr = new Node(val, null, null);
Node tmp, ptr;
boolean ins = false;
if(start == null)
start = nptr;
else if (val.compareTo( start.getData()) <= 0)
{
nptr.setLinkNext(start);
start.setLinkPrev(nptr);
start = nptr;
}
else
{
tmp = start;
ptr = start.getLinkNext();
while(ptr != null)
{
if((val.compareTo(tmp.getData())>=0) && (val.compareTo(ptr.getData())<=0))
{
tmp.setLinkNext(nptr);
nptr.setLinkPrev(tmp);
nptr.setLinkNext(ptr);
ptr.setLinkPrev(nptr);
ins = true;
break;
}
else
{
tmp = ptr;
ptr = ptr.getLinkNext();
}
}
if(!ins)
{
tmp.setLinkNext(nptr);
nptr.setLinkPrev(tmp);

}
}
size++;
}
/* Function to delete node at position */
public void deleteAtPos(int pos)
{
if (pos == 1)
{
if (size == 1)
{
start = null;
size = 0;
return;
}
start = start.getLinkNext();
start.setLinkPrev(null);
size--;
return ;
}
if (pos == size)
{
Node ptr = start;

for (int i = 1; i < size - 1; i++)
ptr = ptr.getLinkNext();
ptr.setLinkNext(null);
size --;
return;
}
Node ptr = start.getLinkNext();
for (int i = 2; i <= size; i++)
{
if (i == pos)
{
Node p = ptr.getLinkPrev();
Node n = ptr.getLinkNext();

p.setLinkNext(n);
n.setLinkPrev(p);
size-- ;
return;
}
ptr = ptr.getLinkNext();
}
}
/* Function to display status of list */
public void display()
{
System.out.print("Doubly Linked List = ");
if (size == 0)
{
System.out.print("empty ");
return;
}
if (start.getLinkNext() == null)
{
System.out.println(start.getData() );
return;
}
Node ptr = start;
System.out.print(start.getData()+ " <-> ");
ptr = start.getLinkNext();
while (ptr.getLinkNext() != null)
{
System.out.print(ptr.getData()+ " <-> ");
ptr = ptr.getLinkNext();
}
System.out.print(ptr.getData()+ " ");
}
}

// Contact Class
   class ContactInfo implements Comparable<ContactInfo>{

       String name;
       String email;
       String phone;
      
       public ContactInfo(String name, String email, String phone) {
           this.name = name;
           this.email = email;
           this.phone = phone;
       }

       @Override
       public int compareTo(ContactInfo o) {
           return this.name.compareTo(o.name);
       }
      
   }

//PhoneBook.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import cgegg.AllPositive.ContactInfo;

public class PhoneBook {
  
   public static void main(String[] args) throws IOException {
       linkedList list = new linkedList();
       File f = new File("phonebook.txt");
       FileReader fis = new FileReader(f);
       BufferedReader br = new BufferedReader(fis);
       String line = br.readLine();
       ContactInfo c=null;
       while(line !=null){
           line= line.trim();
           String info[] = line.split(" ");
           c = new ContactInfo(info[0], info[1], info[2]);
           list.insert(c);
             
       }
   }

}