In: Computer Science
Question 2:
Write a Java console application that will allow a user to add contacts to a contact list and view their current contact list sorted alphabetically. Your program should prompt the user to select an action (add or view). If the user chooses to add a contact, the contact information should be entered using the following format: Firstname Lastname, PhoneNumber If the user chooses to view their contact list, your program should display each contact on a separate line using the following format: Firstname Lastname: PhoneNumber Assume that both contact names and phone numbers will be unique (no two contacts can have the same name or phone number. A contact can only have one phone number, and a phone number can only belong to one contact). Create the appropriate objects based on the application description. Select the most suitable data structure(s) for this application.
Explanation:
Here Link List data structure is used.
Reason:
As the number of contacts have no any particular limit. So, here array can not be used.Because arrray has its limit of length. Beyond which no contact details can be added.
So, adding details is required, dynamic value allocation.So dynamic memory allocation is required.Link List is one of the best data structure for dynamic memory allocation system.
So, using link list here firstname, lastname and phone numbers are added.
Here sorting of names in alphabetical order is done with link list also.
Except that here string comparison function is added with link list.
/* program */
import java.io.*;
import java.util.Scanner;
public class contact_details
{
Node head;
/* node class with three data items */
static class Node {
int pno;
String fname;
String lname;
Node next;
/* Node constructor */
Node(String f,String l,int d)
{
pno = d;
fname=f;
lname=l;
next = null;
}
}
/* adding data in link list*/
public static contact_details add(contact_details list, String
fname,String lname,int pno)
{
Node current = list.head;
int p=0;
while (current != null)
{
/* check the name and contact details matching is
there or not */
/* string concat and comparison is used */
if((current.fname+current.lname).compareTo(fname+lname)!=0 ){
if(current.pno!=pno)
{
}
}
if((current.fname+current.lname).compareTo(fname+lname)==0 ||
current.pno==pno)
{
p++;
}
current = current.next;
}
if(p>0) { System.out.print("Contact Details Duplication
error:\n");}
if(p==0){
Node new_node = new
Node(fname,lname,pno);
new_node.next = null;
if (list.head == null) {
list.head = new_node;
}
else {
Node last = list.head;
while (last.next != null) {
last = last.next;
}
last.next = new_node;
}
}
return list;
}
/* print record in alphabetical sorting order */
public static void view(contact_details list)
{
Node current = list.head;
Node index=null;
String temp;
String temp1;
int d;
System.out.print("Contact Details in Sorted Alphabetical
Order:\n");
/* link list sorting */
while (current != null)
{
index = current.next;
while(index != null)
{
if(current.fname.compareTo(index.fname)>0 )
{
temp = current.fname;
current.fname = index.fname;
index.fname = temp;
temp1 =
current.lname;
current.lname = index.lname;
index.lname = temp1;
d =
current.pno;
current.pno = index.pno;
index.pno = d;
}
index = index.next;
}
current = current.next;
}
current = list.head;
/* print sorted data */
while (current != null)
{
System.out.print(current.fname + " ");
System.out.print(current.lname + " ");
System.out.print(current.pno + " ");
current = current.next;
System.out.println("\n");
}
}
/*main function */
public static void main(String[] args)
{
/* scanner object */
Scanner sc=new Scanner(System.in);
contact_details list = new contact_details ();
System.out.println("Contact add and display\n");
char ch1;
int pno;
String fname;
String lname;
do
{
System.out.println("\nMenu Details");
System.out.println("1.Add contact");
System.out.println("2.View Contact");
System.out.print("\nEnter the option:");
int ch = sc.nextInt();
switch(ch)
{
case 1:
System.out.print("\nEnter First Name : ");
sc.nextLine();
fname=sc.nextLine();
System.out.print("Enter Last Name : ");
lname=sc.nextLine();
System.out.print("Enter Phone no : ");
pno=sc.nextInt();
add(list,fname,lname,pno);
break;
case 2:
view(list);
break;
case 3:
break;
default:
System.out.println("Please enter correct option[1,2]\n");
break;
}
System.out.print("\nDo you want to continue [ press y/Y key for
continuing]: ");
ch1=sc.next().charAt(0);
} while (ch1=='Y'||ch1=='y');
}
}
SCREEN SHOT






OUTPUT



