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.