Advertisement
i0x75

lap_pro

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