Javatpoint Logo
Javatpoint Logo

Design Telephone Directory Java

A telephone directory is a fundamental application that allows users to store and manage their contacts' information efficiently. In this article, we will guide you through the process of designing a simple telephone directory application in Java. We'll provide you with complete code examples and explanations to help you understand each step of the development process.

Prerequisites:

Before we dive into the code, make sure you have the following prerequisites:

  1. Java Development Kit (JDK) installed on your computer.
  2. A basic understanding of Java programming concepts.
  3. An Integrated Development Environment (IDE) like Eclipse, IntelliJ IDEA, or Visual Studio Code.

Creating the Telephone Directory Class

Let's start by creating the TelephoneDirectory class, which will serve as the main structure for our telephone directory application. This class will contain a data structure (e.g., a HashMap) to store contacts.

TelephoneDirectory.java

Creating a Telephone Directory Application:

Now, let's create a simple Java application that uses the TelephoneDirectory class to interact with our telephone directory. Here's an example of a basic application:

TelephoneDirectoryApp.java

Output:

Telephone Directory Menu:
1. Add a contact
2. Find a contact
3. Remove a contact
4. List all contacts
5. Exit
Enter your choice: 1
Enter contact name: J HR
Enter phone number: 9876543219

Telephone Directory Menu:
1. Add a contact
2. Find a contact
3. Remove a contact
4. List all contacts
5. Exit
Enter your choice: 4
Contacts in the directory:
HR: 9876543219

Telephone Directory Menu:
1. Add a contact
2. Find a contact
3. Remove a contact
4. List all contacts
5. Exit
Enter your choice: 2
Enter contact name: HR
Phone number: 9876543219

Telephone Directory Menu:
1. Add a contact
2. Find a contact
3. Remove a contact
4. List all contacts
5. Exit
Enter your choice: 3
Enter contact name to remove: HR
Contact removed.

Telephone Directory Menu:
1. Add a contact
2. Find a contact
3. Remove a contact
4. List all contacts
5. Exit
Enter your choice: 4
Contacts in the directory:

Telephone Directory Menu:
1. Add a contact
2. Find a contact
3. Remove a contact
4. List all contacts
5. Exit
Enter your choice: 5
Exiting the Telephone Directory. Goodbye!

Explanation

We create a TelephoneDirectory object to store our contacts and a Scanner object to read user input.

  1. In the main loop, we display a menu with options to add, find, remove, list contacts, and exit the program.
  2. Depending on the user's choice, we call the appropriate methods of the TelephoneDirectory class.
  3. The program continues running until the user selects the "Exit" option.

Execution:

Compile and run the TelephoneDirectoryApp class using your preferred Java IDE or the command line. Follow the on-screen instructions to add, find, remove, list, and manage contacts in your telephone directory.

Conclusion:

In this article, we've designed a simple telephone directory application in Java using the TelephoneDirectory class. You've learned how to create a basic console-based user interface for managing contacts effectively. Feel free to enhance this application by adding features like data persistence, error handling, or a graphical user interface (GUI) for a more user-friendly experience.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA