Question

In: Computer Science

Question 2: Write a Java console application that will allow a user to add contacts to...

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.

Solutions

Expert Solution

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


Related Solutions

JAVA Write a Java console application that prompts the user to enter the radius of a...
JAVA Write a Java console application that prompts the user to enter the radius of a circle, then prints its radius, diameter, circumference, and area. The Console Output Enter the radius of the circle: 1.2 The radius is 1.2 The diameter is 2.4 The circumference is 7.5398223686155035 The area is 4.523893421169302 Programmer Notes Write and document your program per class coding conventions. Add an instance variable double radius. Generate its get/set methods. Manually type the methods getDiameter(), getCircumference(), and getArea()....
in java Write a contacts database program that presents the user with a menu that allows...
in java Write a contacts database program that presents the user with a menu that allows the user to select between the following options: Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program prompts...
Write a console application to total a set of numbers indicated and provided by a user,...
Write a console application to total a set of numbers indicated and provided by a user, using a while loop or a do…while loop according to the user’s menu choice as follows: C++programming Menu 1. To total numbers using a while loop 2. To total numbers using a do...while loop Enter your menu choice: 1 How many numbers do you want to add: 2 Enter a value for number 1: 4 Enter a value for number 2: 5 The total...
Write a Java console application that reads a string for a date in the U.S. format...
Write a Java console application that reads a string for a date in the U.S. format MM/DD/YYYY and displays it in the European format DD.MM.YYYY  For example, if the input is 02/08/2017, your program should output 08.02.2017
JAVA FILE PROGRAM Write a contacts database program that presents the user with a menu that...
JAVA FILE PROGRAM Write a contacts database program that presents the user with a menu that allows the user to select between the following options: Save a contact. Search for a contact. Print all contacts out to the screen. Quit If the user selects the first option, the user is prompted to enter a person's name and phone number which will get saved at the end of a file named contacts.txt. If the user selects the second option, the program...
Write a JAVA program that allow a user to enter 2 number, starting point and end...
Write a JAVA program that allow a user to enter 2 number, starting point and end point. For example a user me enter 1 and 100. Your program will Add numbers from 1 to 100, add all the even number, and add all the odd numbers Output: The sum of number 1 to 100 is: The sum of even number 1 to 100 is: The sum of odd number 1 to 100 is:
Write a C++ console application that allows your user to enter the total rainfall for each...
Write a C++ console application that allows your user to enter the total rainfall for each of 12 months into an array of doubles. The program should validate user input by guarding against rainfall values that are less than zero. After all 12 entries have been made, the program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts.
In this assessment, you will debug and fix a given Java console application that uses 2...
In this assessment, you will debug and fix a given Java console application that uses 2 dimensional arrays but the application does not compile nor execute. You can use either the Toolwire environment or your local Java development environment to complete this assignment.The application has four bugs. Your assignment is to find these bugs and fix them so that the application meets its stated requirements.The requirements of this application are as follows: The application is register students for courses in...
In this assessment, you will debug and fix a given Java console application that uses 2...
In this assessment, you will debug and fix a given Java console application that uses 2 dimensional arrays but the application does not compile nor execute. You can use either the Toolwire environment or your local Java development environment to complete this assignment.The application has four bugs. Your assignment is to find these bugs and fix them so that the application meets its stated requirements.The requirements of this application are as follows: The application is register students for courses in...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT