Thursday, 19 January 2017

Telephone...

A file contains a list of names and telephone numbers in the following form:-
AJIT                      281050
ANIL                     462890
RISHIKESH          524352

The names contains only one word and the names and telephone numbers are separated by white spaces. Write a interactive program to:-
·         Create the above file.
·         Display the content.
·         Search a telephone number for a given name( Assure No duplication  of name).
·         Exit the program.
The program must create the file for the first time.
Test your program for the following data:-
AJIT                      281050
ANIL                     462890
RISHIKESH           524352
JAVAGAL              345687
MONGIA                234561
RAHUL                  765433
ROBIN                             543221
SACHIN                 765433
SAURAV               8765908
VENKATESH                   7654322



Solution:-

import java.io.*;
import java.util.*;
class  writer
{
     String name;
     int number;
     void input()throws Exception
     {
           Scanner sc=new Scanner(System.in);
           System.out.print("Enter A Name:\t");
           name=sc.next();
           System.out.print("Enter Phone Number:\t");
           number=sc.nextInt();
     }
     void write()throws Exception//write data in file...
     {
           Scanner sc=new Scanner(System.in);
           char ch='y';
           while(ch=='y'||ch=='Y')
           {
                input();
                if(search(name)!=-404)
                {
                     System.out.println("Error 404: Contact Already Exists");
                }
                else
                     {
                           FileWriter fw = new FileWriter("phone.txt",true);//this will make file append...
                           PrintWriter pw = new PrintWriter(fw);
                           pw.println(name+"\t"+number);
                           pw.close();
                     }
                System.out.println("Do You Want To Continue Y/N ?");
                ch=sc.next().charAt(0);
           }
     }
     static int search(String str)throws Exception//Assure No duplication of name....
     {
           String match;
           Scanner sc=new Scanner(new File ("phone.txt"));
           while(sc.hasNext())
           {
                match=sc.next();
                if(match.equalsIgnoreCase(str))
                {
                     return sc.nextInt();
                }
           }
           return -404;
     }
}
class reader extends writer//Inheritence...
{
     String name;
     int number;    
     void output()throws Exception
     {
           System.out.println(name+"\t"+number);
     }
     void read()throws Exception//read data from file...
     {
           Scanner sn=new Scanner(new File ("phone.txt"));
           System.out.println("NAME\tPHONE");
           while(sn.hasNext())
           {
                name=sn.next();
                number=sn.nextInt();
                output();
           }
           sn.close();
     }
     static int search(String str)throws Exception//search for a contact...
     {
           String match;
           Scanner sr=new Scanner(new File("phone.txt"));
           while(sr.hasNext())
           {
                match=sr.next();
                if(match.equalsIgnoreCase(str))
                {
                     return sr.nextInt();
                }
           }
           return -404;
     }
     public static void main(String args[])throws Exception
     {
           reader obj=new reader();
           Scanner sc=new Scanner(System.in);
           System.out.println("1.Store Contact\n2.Read Contacts\n3.Search a contact");
           System.out.print("Enter Your Choice: ");
           int i=sc.nextInt();
           switch(i)
           {
                case 1:obj.write();
                break;
                case 2:obj.read();
                break;
                case 3:System.out.println("Enter a name to Search");
                String stri=sc.next();
                int no=search(stri);
                if(no==-404)
                {
                     System.out.println("No Search result was found");
                }
                else if(no!=404)
                {
                     System.out.println("Search results:\t"+stri+"\t"+no);
                }
                break;
                default:System.out.println("Enter A Valid Choice");
                break;
           }
     }

}





No comments:

Post a Comment

Goldbach Number ISC 2018

Question: A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes. Note: All even integer numbers ...