Advertisement
i0x75

last

Apr 7th, 2020
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.04 KB | None | 0 0
  1. import java.util.*;
  2. class Entry
  3. {
  4.    String[] name = new String[2];
  5.    String[] homeAddress = new String[4];
  6.    int telNum;
  7.    Entry(String names[],String homeAddresses[],int telNums){
  8.        this.name[0]=names[0];
  9.        this.name[1]=names[1];
  10.        this.homeAddress[0]=homeAddresses[0];
  11.        this.homeAddress[1]=homeAddresses[1];
  12.        this.homeAddress[2]=homeAddresses[2];
  13.        this.homeAddress[3]=homeAddresses[3];
  14.        this.telNum=telNums;
  15.    }
  16.  
  17. }
  18.  
  19. class Driver
  20. {
  21.    public static void main(String args[])
  22.    {
  23.        Directory dir = new Directory(10);
  24.        while(true){
  25.        System.out.println("\nEnter Choice-: ");
  26.        System.out.println("1. Add person details into the telephone book");
  27.        System.out.println("2. Display the entire telephone book");
  28.        System.out.println("3. Search a telephone number based on person first name");
  29.        System.out.println("4. Remove a person from the telephone book");
  30.        System.out.println("5. Exit");
  31.        Scanner sc = new Scanner(System.in);
  32.        int choice = sc.nextInt();
  33.        String[] name = new String[2];
  34.        String[] homeAddresses = new String[5];
  35.        int telNum;
  36.        sc.nextLine();
  37.        switch(choice)
  38.        {
  39.            case 1:{
  40.                        System.out.println("Enter Last name: ");
  41.                        name[0] = sc.nextLine();
  42.                        System.out.println("Enter First Name: ");
  43.                        name[1] = sc.nextLine();
  44.                        System.out.println("Enter street address: ");
  45.                        homeAddresses[0] = sc.nextLine();
  46.                        System.out.println("Enter city: ");
  47.                        homeAddresses[1] = sc.nextLine();
  48.                        System.out.println("Enter state: ");
  49.                        homeAddresses[2] = sc.nextLine();
  50.                        System.out.println("Enter zipcode: ");
  51.                        homeAddresses[3] = sc.nextLine();
  52.                        System.out.println("Enter telephone number: ");
  53.                        telNum = sc.nextInt();
  54.                        Entry e = new Entry(name,homeAddresses,telNum);
  55.                        dir.insert(e);
  56.                        dir.sort();
  57.                        break;
  58.                     }
  59.            case 2:{
  60.                    dir.display();
  61.                    break;
  62.  
  63.                     }
  64.            case 3:{
  65.                    System.out.println("Enter Last Name : -");
  66.                    String lastName = sc.nextLine();
  67.                    System.out.println("Enter first Name :-");
  68.                    String firstName = sc.nextLine();
  69.                    dir.search( lastName, firstName );
  70.                    break;  
  71.                    }
  72.             case 4:{
  73.                    System.out.println("Enter Last Name : -");
  74.                    String lastName = sc.nextLine();
  75.                    System.out.println("Enter first Name :-");
  76.                    String firstName = sc.nextLine();
  77.                    dir.delete( lastName, firstName );
  78.                    break;
  79.             }
  80.             case 5:{
  81.                    System.exit(0);
  82.                    break;
  83.                    }  
  84.             }
  85.        }
  86.    }
  87. }
  88. class Directory{
  89.    int max;
  90.    Entry[] dir;
  91.    int index = 0;
  92.    Directory(int max){
  93.        this.max = max;
  94.        this.dir = new Entry[max];
  95.    }
  96.    public void insert(Entry newentry){
  97.        dir[index] = newentry;
  98.        index++;
  99.    }
  100.    public void delete(String last, String first){
  101.        int i;
  102.        for(i=0; i< index;i++){
  103.            Entry tempEntry = dir[i];
  104.            if(tempEntry.name[0].equals(last) && tempEntry.name[1].equals(first) ) {
  105.                break;
  106.            }
  107.        }
  108.        if( i < index ){
  109.            if(i == 0){
  110.                dir[0] = null;
  111.            }
  112.            for( int j=i; j <= index-1; j++ ) {
  113.            dir[j] = dir[j + 1];
  114.            }
  115.            index = index - 1;
  116.        }
  117.        else{
  118.            System.out.println("not found");
  119.        }
  120.      
  121.    }
  122.    public void display(){
  123.        if(index ==0){
  124.            System.out.println("Directory Empty !!");
  125.            System.exit(0);
  126.        }
  127.        for(int i=0;i<index;i++){
  128.            Entry tempEntry = dir[i];
  129.            System.out.println("Last Name :\t" + tempEntry.name[0]);
  130.            System.out.println("First Name :\t" + tempEntry.name[1]);
  131.            System.out.println("street address :\t" + tempEntry.homeAddress[0]);
  132.            System.out.println("city :\t" + tempEntry.homeAddress[1]);
  133.            System.out.println("state :\t" + tempEntry.homeAddress[2]);
  134.            System.out.println("zip code :\t" + tempEntry.homeAddress[3]);
  135.            System.out.println("tel number :\t" + tempEntry.telNum);
  136.        }
  137.    }
  138.    public void sort(){
  139.        Entry tempi,tempj;
  140.        for(int i=0; i < index; i++){
  141.            tempi = dir[i];
  142.            for(int j = 0; j < index; j++){
  143.                tempj = dir[j];
  144.                int choice = (tempi.name[0]).compareTo(tempj.name[0]);
  145.                if(choice == 1 ){
  146.                    dir[i] = tempj;
  147.                    dir[j] = tempi;
  148.                }
  149.            }
  150.        }
  151.    }
  152.    public int search(String last, String first){
  153.        Entry tempEntry;
  154.        for(int i=0; i < index; i++){
  155.            tempEntry = dir[i];
  156.            if((tempEntry.name[0]).equals(last) && (tempEntry.name[1]).equals(first)){
  157.                System.out.println("\n Element found !!!\n\n");
  158.            System.out.println("Last Name :\t" + tempEntry.name[0]);
  159.            System.out.println("First Name :\t" + tempEntry.name[1]);
  160.            System.out.println("street address :\t" + tempEntry.homeAddress[0]);
  161.            System.out.println("city :\t" + tempEntry.homeAddress[1]);
  162.            System.out.println("state :\t" + tempEntry.homeAddress[2]);
  163.            System.out.println("zip code :\t" + tempEntry.homeAddress[3]);
  164.            System.out.println("tel number :\t" + tempEntry.telNum);
  165.            return 0;  
  166.            }
  167.        }
  168.        System.out.println("\n\n not found \n\n ");
  169.        return 1;
  170.    }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement