Java

 

A simple Java application to manage and store contacts with features to add, display, and search for contacts.

Introduction

A Contact Book is a digital version of your traditional address book. It allows users to store
important contact details such as names, phone numbers, and emails. This Java application
is designed to manage contacts by enabling users to add new contacts, view existing ones, and
search for specific contacts.

Objective

The objective of this Java program is to create a simple and effective contact management system.
With this, you will learn how to use basic object-oriented programming concepts, such as classes
and objects, as well as how to store and manipulate data within a simple application.

Code Implementation

import java.util.*;

class Contact {
    String name;
    String phone;
    String email;

    // Constructor to initialize the contact details
    public Contact(String name, String phone, String email) {
        this.name = name;
        this.phone = phone;
        this.email = email;
    }

    // Display the contact information
    public void displayContact() {
        System.out.println("Name: " + name);
        System.out.println("Phone: " + phone);
        System.out.println("Email: " + email);
        System.out.println("------------------------------");
    }
}

public class ContactBook {
    private static List contacts = new ArrayList<>();

    // Method to add a new contact
    public static void addContact(String name, String phone, String email) {
        Contact newContact = new Contact(name, phone, email);
        contacts.add(newContact);
        System.out.println("Contact added successfully.");
    }

    // Method to display all contacts
    public static void displayAllContacts() {
        if (contacts.isEmpty()) {
            System.out.println("No contacts to display.");
        } else {
            for (Contact contact : contacts) {
                contact.displayContact();
            }
        }
    }

    // Method to search for a contact by name
    public static void searchContact(String name) {
        boolean found = false;
        for (Contact contact : contacts) {
            if (contact.name.equalsIgnoreCase(name)) {
                contact.displayContact();
                found = true;
                break;
            }
        }
        if (!found) {
            System.out.println("Contact not found.");
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int choice;

        do {
            System.out.println("\nContact Book Menu:");
            System.out.println("1. Add New Contact");
            System.out.println("2. Display All Contacts");
            System.out.println("3. Search Contact by Name");
            System.out.println("4. Exit");
            System.out.print("Enter your choice: ");
            choice = sc.nextInt();
            sc.nextLine();  // consume newline

            switch (choice) {
                case 1:
                    System.out.print("Enter Name: ");
                    String name = sc.nextLine();
                    System.out.print("Enter Phone: ");
                    String phone = sc.nextLine();
                    System.out.print("Enter Email: ");
                    String email = sc.nextLine();
                    addContact(name, phone, email);
                    break;

                case 2:
                    displayAllContacts();
                    break;

                case 3:
                    System.out.print("Enter Name to Search: ");
                    String searchName = sc.nextLine();
                    searchContact(searchName);
                    break;

                case 4:
                    System.out.println("Exiting the Contact Book.");
                    break;

                default:
                    System.out.println("Invalid choice. Please try again.");
            }
        } while (choice != 4);
        
        sc.close();
    }
}

Explanation of the Program Structure

The program consists of two main components: the Contact class and the ContactBook class. The Contact class is used to represent a contact with attributes like name, phone, and email. It also has a method to display the contact’s details.

The ContactBook class is where the main application logic is implemented. It contains a list of contacts and offers methods to add a new contact, display all contacts, and search for a contact by name. The program runs in a loop, offering the user options until they choose to exit.

The main method serves as the entry point of the application, where it interacts with the user through the console to manage contacts.

How to Run the Program

1. Copy the provided Java code into a file named ContactBook.java.

2. Compile the Java code using the following command:

javac ContactBook.java

3. Run the compiled program with this command:

java ContactBook

4. Follow the on-screen instructions to add, display, or search for contacts.

© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)