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

Your system should first read the list of contacts from a text file. The file lo

ID: 3636373 • Letter: Y

Question

Your system should first read the list of contacts from a text file. The file looks like:
Ali 8601234
Bandar 053256678
Turky 8609101
Adel 8601121
Amr 055243141
Sami 053265161
Talal 056387718
Husam 8609202
Tareq 052498762
Once this information is read, the system should offer the user options to do with the
contact list. The program should display a menu of options that will be offered to the user
and prompt him to choose one of them. These options are:
1. Display contact list.
2. Add a new contact.
3. Delete a contact.
4. Search for a contact.
5. Save contacts in a file.
6. Exit.
The following paragraphs explain each option.
Option 1: Display contact list
When the user chooses this option, the program should print the entire contact list in the
screen.
Option 2: Add a new contact
When the user chooses this option, the program should prompt (read) the user to enter a
name and telephone number of the person. Then, it adds it in the list.
Option 3: Delete a contact
When the user chooses this option, the program should prompt the user to enter the name
of the person to be deleted it. If the person is in the list, it's deleted. If the person is not in
the list, a message is displayed saying that the contact does not exist.
Option 4: Search a contact
When the user chooses this option, the program should prompt the user to choose from
another two options.
4.1 Search by telephone number: if chosen, the program prompts the user to
enter the telephone number and search the list. If the contact is in the list, his name
should be printed. If not, a message is displayed saying that it does not exist.
4.2 Search by name: if chosen, the program prompts the user to enter the name
of the person and searches the list. If the name is in the list, his telephone number should
be printed. If not, a message is displayed saying that it does not exist.
Option 5: Save contacts in a file
When the user chooses this option, then the program should save the contact list in a text
file.
Option 6: Exit
The menu of options is displayed repeatedly until the user chooses this option. When he
chooses Exit, then the program should stop displaying the menu and ends execution.

Write a reply...

Explanation / Answer

please rate - thanks

message me if any problems before rating

import java.util.*;
import java.io.*;
public class FileIn
{public static void main(String[] args)throws FileNotFoundException,IOException

{String filename;
int entries=0,n;
String[]name=new String[50];
String []number=new String[50];
Scanner kb=new Scanner (System.in);
System.out.print("Enter name of file with contacts ");
filename=kb.nextLine();
Scanner in=new Scanner(new File(filename));
Scanner input=new Scanner(System.in);
while(in.hasNext())
   {name[entries]=in.next();
    number[entries]=in.next();
    entries++;
    }
n=menu(input);
while(n!=6)
   {switch(n)
    {case 1: display(name,number,entries);
               break;
       case 2: entries=add(name,number,entries,input);
               break;
      case 3: entries=delete(name,number,entries,input);
               break;
        case 4: search(name,number,entries,input,0);
               break;   
        case 5: save(name,number,entries,input);
               break;           
      }
    n=menu(input);
    }
}
public static int menu(Scanner in)
{int n=0;
while(n<1||n>6)
   {System.out.println("1. Display contact list.");
    System.out.println("2. Add a new contact.");
    System.out.println("3. Delete a contact.");
    System.out.println("4. Search for a contact.");
    System.out.println("5. Save contacts in a file.");
    System.out.println("6. Exit.");
    n=in.nextInt();
    }
    return n;
    }

public static void display(String[] name,String[] number,int entries)
            {System.out.println("Name Number");
            for(int i=0;i<entries;i++)
                  System.out.println(name[i]+" "+number[i]);
            System.out.println();   
            }
public static int add(String[] name,String[] number,int entries,Scanner in)
             {System.out.print("Enter persons name: ");
                name[entries]=in.next();
                System.out.print("Enter phone number: ");
                number[entries]=in.next();
             return entries+1;               
                }
public static int delete(String[] name,String[] number,int entries,Scanner in)
             {String n;
                int i,j;
                i=search(name,number,entries,in,1);
                if(i>=0)
                   if(i!=entries-1)
                       for(j=i;j<entries-1;j++)
                             {name[j]=name[j+1];
                            number[j]=number[j+1];
                            }
                return entries-1;
                }
public static int search(String[] name,String[] number,int entries,Scanner in,int from)
            {int type =0,n;
               if(from==0)
                  {System.out.print("Enter 1 to search by name, anything else to search by number: ");
                    if(in.nextInt()!=1)
                          type=1;
                    }
                if(type==1)
                   n=find(number,entries,"number",in);
                                    else
                   n=find(name,entries,"name",in);
                if(n<0)
                   System.out.println("Entry not found");
                else
                   if(from==0)
                       System.out.println(name[n]+" "+number[n]);
                return n;   
            }
public static void save(String[] name,String[] number,int entries,Scanner in)throws IOException

            {System.out.print("Enter the name of file: ");
            String filename=in.next();
            PrintWriter f=new PrintWriter(new FileWriter(filename));
                for(int i=0;i<entries;i++)
                     f.println(name[i]+" "+number[i]);
                f.close();
            }
public static int find(String data[],int n,String mess,Scanner in)
{int i;
String val;
System.out.print("Enter "+mess+" to search: ");
val=in.next();
for(i=0;i<n;i++)
    if(data[i].compareToIgnoreCase(val)==0)
          return i;
return -1;
}
}

sample run

----jGRASP exec: java FileIn

Enter name of file with contacts input.txt
1. Display contact list.
2. Add a new contact.
3. Delete a contact.
4. Search for a contact.
5. Save contacts in a file.
6. Exit.
1
Name    Number
Ali    8601234
Bandar    053256678
Turky    8609101
Adel    8601121
Amr    055243141
Sami    053265161
Talal    056387718
Husam    8609202
Tareq    052498762

1. Display contact list.
2. Add a new contact.
3. Delete a contact.
4. Search for a contact.
5. Save contacts in a file.
6. Exit.
2
Enter persons name: jj
Enter phone number: 123456
1. Display contact list.
2. Add a new contact.
3. Delete a contact.
4. Search for a contact.
5. Save contacts in a file.
6. Exit.
1
Name    Number
Ali    8601234
Bandar    053256678
Turky    8609101
Adel    8601121
Amr    055243141
Sami    053265161
Talal    056387718
Husam    8609202
Tareq    052498762
jj    123456

1. Display contact list.
2. Add a new contact.
3. Delete a contact.
4. Search for a contact.
5. Save contacts in a file.
6. Exit.
3
Enter name to search: Ali
1. Display contact list.
2. Add a new contact.
3. Delete a contact.
4. Search for a contact.
5. Save contacts in a file.
6. Exit.
1
Name    Number
Bandar    053256678
Turky    8609101
Adel    8601121
Amr    055243141
Sami    053265161
Talal    056387718
Husam    8609202
Tareq    052498762
jj    123456

1. Display contact list.
2. Add a new contact.
3. Delete a contact.
4. Search for a contact.
5. Save contacts in a file.
6. Exit.
4
Enter 1 to search by name, anything else to search by number: 1
Enter name to search: Sami
Sami 053265161
1. Display contact list.
2. Add a new contact.
3. Delete a contact.
4. Search for a contact.
5. Save contacts in a file.
6. Exit.
4
Enter 1 to search by name, anything else to search by number: 1
Enter name to search: kk
Entry not found
1. Display contact list.
2. Add a new contact.
3. Delete a contact.
4. Search for a contact.
5. Save contacts in a file.
6. Exit.
4
Enter 1 to search by name, anything else to search by number: 2
Enter number to search: 123456
jj 123456
1. Display contact list.
2. Add a new contact.
3. Delete a contact.
4. Search for a contact.
5. Save contacts in a file.
6. Exit.
5
Enter the name of file: output.txt
1. Display contact list.
2. Add a new contact.
3. Delete a contact.
4. Search for a contact.
5. Save contacts in a file.
6. Exit.
6

----jGRASP: operation complete.