Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: Telephone Directory ~ ISC COMPUTER SCIENCE PRACTICAL SOLLVED 2000

Thursday, August 7, 2014

Telephone Directory ~ ISC COMPUTER SCIENCE PRACTICAL SOLLVED 2000

ISC COMPUTER SCIENCE PRACTICAL SOLLVED 2000, ISC JAVA PROGRAMS
A file contains a list of names and telephone numbers in the following form
AJIT 281050
ANIL 462890
HRISHIKESH 524352
.
.
.
.
.
.
The names contain only one word and the names and telephone numbers
are separated by white spaces. Write an interactive program to :
(i) Create the above file.
(ii) Display the contents in two columns.
(iii) Search a telephone number for a given name (Assure no duplication of names).
(iv) Exit the program.


Test your program using the following data :

AJAY 281050
AJIT 425382
ANIL 525122
AJHAR 528422
HRISHIKESH 524352
JAVAGAL 532673
MONGIA 691383
RAHUL 698114
ROBIN 524899
SACHIN 628266
SAURAV 708294
VENKATESH 492642

Test the (iii) option by searching the telephone numbers of :
(a) AJHAR
(b) SACHIN       

import java.util.*;
import java.io.*;
public class Telephone
    {
    public static void main(String[] args) throws IOException
        {
        String fileName = "Tele.dat";
        String nameCust,temp;
        String teleNumber,S;
        boolean flag;
        Scanner in = new Scanner(System.in);
        FileOutputStream fout = new FileOutputStream(fileName);
        while(true) {
            temp = "";
            flag = false;
            System.out.println("Enter Customer name:");
            nameCust = in.next();
            System.out.println("Enter telephone number");
            teleNumber = in.next();
            temp = nameCust+" "+teleNumber;
            temp = temp.toUpperCase();
            FileReader fr = new FileReader(fileName);   
            BufferedReader br = new BufferedReader(fr);
            while(( S=br.readLine() ) != null) {
                String check ="";
                int j = 0;
                while(S.charAt(j++) != ' ')
                    check += S.charAt(j-1); 
                if (check.equals(nameCust)) {
                    System.out.println("Name already exists, enter a different name:");
                    flag = true;
                    }
                }
            br.close();   
            if(flag == true)
                continue;   
            for( int i = 0; i < temp.length();i++)
                fout.write(temp.charAt(i));
            fout.write('\n');   
            System.out.println("Want to continue? (Y/N)");   
            String a = in.next();
            if ( a.toUpperCase().equals("N"))
                break;
            }
        fout.close();   
       
        String srchTele;
        System.out.println("Enter the customer name to search for telephone number:");
        srchTele = in.next();
        srchTele = srchTele.toUpperCase();
        FileReader fr = new FileReader(fileName);   
        BufferedReader br = new BufferedReader(fr);
        flag = false;
            while(( S=br.readLine() ) != null) {
                String check ="";
                int j = 0;
                while(S.charAt(j++) != ' ')
                    check += S.charAt(j-1); 
                if (check.equals(srchTele)) {                    
                    System.out.println(S.substring(j));
                    flag = true;
                    }
                }
        if( flag == false)
            System.out.println("Customer does not exists:");       
        br.close();       
        }
    }   

No comments:

Post a Comment