Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Entry
- {
- String[] name = new String[2];
- String[] homeAddress = new String[4];
- int telNum;
- Entry(String names[],String homeAddresses[],int telNums){
- this.name[0]=names[0];
- this.name[1]=names[1];
- this.homeAddress[0]=homeAddresses[0];
- this.homeAddress[1]=homeAddresses[1];
- this.homeAddress[2]=homeAddresses[2];
- this.homeAddress[3]=homeAddresses[3];
- this.telNum=telNums;
- }
- }
- class Driver
- {
- public static void main(String args[])
- {
- Directory dir = new Directory(10);
- while(true){
- System.out.println("\nEnter Choice-: ");
- System.out.println("1. Add person details into the telephone book");
- System.out.println("2. Display the entire telephone book");
- System.out.println("3. Search a telephone number based on person first name");
- System.out.println("4. Remove a person from the telephone book");
- System.out.println("5. Exit");
- Scanner sc = new Scanner(System.in);
- int choice = sc.nextInt();
- String[] name = new String[2];
- String[] homeAddresses = new String[5];
- int telNum;
- sc.nextLine();
- switch(choice)
- {
- case 1:{
- System.out.println("Enter Last name: ");
- name[0] = sc.nextLine();
- System.out.println("Enter First Name: ");
- name[1] = sc.nextLine();
- System.out.println("Enter street address: ");
- homeAddresses[0] = sc.nextLine();
- System.out.println("Enter city: ");
- homeAddresses[1] = sc.nextLine();
- System.out.println("Enter state: ");
- homeAddresses[2] = sc.nextLine();
- System.out.println("Enter zipcode: ");
- homeAddresses[3] = sc.nextLine();
- System.out.println("Enter telephone number: ");
- telNum = sc.nextInt();
- Entry e = new Entry(name,homeAddresses,telNum);
- dir.insert(e);
- dir.sort();
- break;
- }
- case 2:{
- dir.display();
- break;
- }
- case 3:{
- System.out.println("Enter Last Name : -");
- String lastName = sc.nextLine();
- System.out.println("Enter first Name :-");
- String firstName = sc.nextLine();
- dir.search( lastName, firstName );
- break;
- }
- case 4:{
- System.out.println("Enter Last Name : -");
- String lastName = sc.nextLine();
- System.out.println("Enter first Name :-");
- String firstName = sc.nextLine();
- dir.delete( lastName, firstName );
- break;
- }
- case 5:{
- System.exit(0);
- break;
- }
- }
- }
- }
- }
- class Directory{
- int max;
- Entry[] dir;
- int index = 0;
- Directory(int max){
- this.max = max;
- this.dir = new Entry[max];
- }
- public void insert(Entry newentry){
- dir[index] = newentry;
- index++;
- }
- public void delete(String last, String first){
- int i;
- for(i=0; i< index;i++){
- Entry tempEntry = dir[i];
- if(tempEntry.name[0].equals(last) && tempEntry.name[1].equals(first) ) {
- break;
- }
- }
- if( i < index ){
- if(i == 0){
- dir[0] = null;
- }
- for( int j=i; j <= index-1; j++ ) {
- dir[j] = dir[j + 1];
- }
- index = index - 1;
- }
- else{
- System.out.println("not found");
- }
- }
- public void display(){
- if(index ==0){
- System.out.println("Directory Empty !!");
- System.exit(0);
- }
- for(int i=0;i<index;i++){
- Entry tempEntry = dir[i];
- System.out.println("Last Name :\t" + tempEntry.name[0]);
- System.out.println("First Name :\t" + tempEntry.name[1]);
- System.out.println("street address :\t" + tempEntry.homeAddress[0]);
- System.out.println("city :\t" + tempEntry.homeAddress[1]);
- System.out.println("state :\t" + tempEntry.homeAddress[2]);
- System.out.println("zip code :\t" + tempEntry.homeAddress[3]);
- System.out.println("tel number :\t" + tempEntry.telNum);
- }
- }
- public void sort(){
- Entry tempi,tempj;
- for(int i=0; i < index; i++){
- tempi = dir[i];
- for(int j = 0; j < index; j++){
- tempj = dir[j];
- int choice = (tempi.name[0]).compareTo(tempj.name[0]);
- if(choice == 1 ){
- dir[i] = tempj;
- dir[j] = tempi;
- }
- }
- }
- }
- public int search(String last, String first){
- Entry tempEntry;
- for(int i=0; i < index; i++){
- tempEntry = dir[i];
- if((tempEntry.name[0]).equals(last) && (tempEntry.name[1]).equals(first)){
- System.out.println("\n Element found !!!\n\n");
- System.out.println("Last Name :\t" + tempEntry.name[0]);
- System.out.println("First Name :\t" + tempEntry.name[1]);
- System.out.println("street address :\t" + tempEntry.homeAddress[0]);
- System.out.println("city :\t" + tempEntry.homeAddress[1]);
- System.out.println("state :\t" + tempEntry.homeAddress[2]);
- System.out.println("zip code :\t" + tempEntry.homeAddress[3]);
- System.out.println("tel number :\t" + tempEntry.telNum);
- return 0;
- }
- }
- System.out.println("\n\n not found \n\n ");
- return 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement