Phonebook Using Hashtable

Phonebook Using Hashtable

In most interviews or assignment cases ask you to implement a phone book using a proper data structure. This article shows the Implementing Phone book Using Hashtable. The case is as below.

Assume you are given to task implement a database for existing telephone company database which contains phone number and name of customers. But there are considerable records in the database and it is very slow to search, update or delete the data. Assume you are going to use Hashtable in Java for that scenario and implement methods to accomplish below actions,

1. Insert new record
2. Find record
3. Delete record
4. Search all records

This article shows you how to implement the above scenario. I’m using NetBeans as my IDE and using JDK 8.

Firs create an Interface for basic operations. However it helps to manage the code rather than writing all in one Java class.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package HashtableExample;

/**
 *
 * @author Bhanuka
 */
public interface PhoneBook {

    public void add(String number, String name);

    public void find(String Number);

    public void remove(String Number);

    public void showall();
}

Next step is to create a java class for implement above operations. For example I used Java inheritance here this will emphasis your code more readable.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package HashtableExample;

import java.util.Hashtable;

/**
 *
 * @author Bhanuka
 */
public class PhonebookMaker implements PhoneBook {

    private Hashtable<String, String> phoneBook;

    public PhonebookMaker() {
        phoneBook = new Hashtable<String, String>();
    }

    @Override
    public void add(String number, String name) {
        if (phoneBook.containsKey(number)) {
            System.out.println(number + " already exists");
        } else {
            phoneBook.put(number, name);
            System.out.println("Contact was added successfully.");
        }
    }

    @Override
    public void find(String Number) {
        if (phoneBook.containsKey(Number)) {
            String Name = phoneBook.get(Number);
            System.out.println(Number + "'s owner is " + Name + ".");
        } else {
            System.out.println("No person named exists for this " + Number);
        }
    }

    @Override
    public void remove(String Number) {
        if (phoneBook.containsKey(Number)) {
            phoneBook.remove(Number);
            System.out.println(Number + " was removed successfully.");
        } else {
            System.out.println("No person named exists for this " + Number);
        }
    }

    @Override
    public void showall() {
        if (phoneBook.size() > 0) {
            System.out.println(phoneBook.size() + " Contact Details ara available");
            for (String key : phoneBook.keySet()) {
                System.out.println(key + " : " + phoneBook.get(key));
            }
        } else {
            System.out.println("No available Contacts");
        }

    }
}

Finally, creating a Java class to demonstration the simple application.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package HashtableExample;

import java.util.Hashtable;
import java.util.Scanner;

/**
 *
 * @author Bhanuka
 */
public class PhonebookDemo {

    public static void main(String[] args) {

        System.out.println("Perfome any action [add , find, remove, show_all]");
        Scanner scanner = new Scanner(System.in);
        PhonebookMaker phonebookMaker = new PhonebookMaker();

        while (true) {

            System.out.println("Type action");
            String action = scanner.next();

            if (action.equalsIgnoreCase("add")) {
                System.out.println("Enter Phone Number");
                String number = scanner.next();
                System.out.println("Enter Name");
                String name = scanner.next();
                phonebookMaker.add(number, name);
            } else if (action.equalsIgnoreCase("find")) {
                System.out.println("Enter Number to Find");
                String number = scanner.next();
                phonebookMaker.find(number);
            } else if (action.equalsIgnoreCase("remove")) {
                System.out.println("Enter Number to Remove");
                String number = scanner.next();
                phonebookMaker.remove(number);
            } else if (action.equalsIgnoreCase("show_all") || action.equalsIgnoreCase("showall")) {
                phonebookMaker.showall();
            } else if (action.equalsIgnoreCase("exit")) {
                break;
            }
        }
        scanner.close();
    }
}

This is the code example for implementing a Phone book using Java Hashtable.

You can find more Java interview question examples from here.
You can find more details on new technologies from here.