Building a Basic Contact Book is a fantastic project for Java beginners who want to practice handling data collections, classes and objects, user input, and simple search functionality. This project simulates managing contacts with names and phone numbers, all from the console.
What Will This Contact Book App Do?
- Allow users to add contacts (name and phone number).
- Display all saved contacts.
- Search contacts by name.
- Optionally, remove contacts.
- Exit the program via a menu.
Step 1: Concepts Practiced
This project will help you learn:
- How to create and use a Contact class (objects).
- Using an
ArrayList
to store multiple contacts. - Handling input/output using
Scanner
. - Implementing menu-driven programs with
loops
andswitch-case
. - Searching through collections.
- Simple data validation.
Step 2: Define the Contact Class
At its core, each contact will be an object with two properties:
java
public class Contact {
private String name;
private String phoneNumber;
// Constructor
public Contact(String name, String phoneNumber) {
this.name = name;
this.phoneNumber = phoneNumber;
}
// Getters
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
// For displaying a contact's details
@Override
public String toString() {
return "Name: " + name + ", Phone: " + phoneNumber;
}
}
Explanation:
- private fields follow encapsulation best practices.
- Constructor initializes contact data.
- Getter methods provide access to fields.
-
toString()
method helps print contact information nicely.
Step 3: Main Program — Menu and Logic
Here’s the main class with key functionalities:
java
import java.util.ArrayList;
import java.util.Scanner;
public class ContactBook {
public static void main(String[] args) {
ArrayList<Contact> contacts = new ArrayList<>();
Scanner scanner = new Scanner(System.in);
int choice;
do {
System.out.println("\n--- Contact Book Menu ---");
System.out.println("1. Add Contact");
System.out.println("2. View All Contacts");
System.out.println("3. Search Contact by Name");
System.out.println("4. Remove Contact");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine(); // Clear buffer
switch (choice) {
case 1:
addContact(contacts, scanner);
break;
case 2:
viewAllContacts(contacts);
break;
case 3:
searchContact(contacts, scanner);
break;
case 4:
removeContact(contacts, scanner);
break;
case 5:
System.out.println("Goodbye!");
break;
default:
System.out.println("Invalid choice! Please enter 1-5.");
}
} while (choice != 5);
scanner.close();
}
// Method to add a contact
public static void addContact(ArrayList<Contact> contacts, Scanner scanner) {
System.out.print("Enter contact name: ");
String name = scanner.nextLine();
System.out.print("Enter phone number: ");
String phoneNumber = scanner.nextLine();
Contact newContact = new Contact(name, phoneNumber);
contacts.add(newContact);
System.out.println("Contact added successfully!");
}
// Method to view all contacts
public static void viewAllContacts(ArrayList<Contact> contacts) {
if (contacts.isEmpty()) {
System.out.println("No contacts found.");
} else {
System.out.println("Contacts list:");
for (Contact c : contacts) {
System.out.println(c);
}
}
}
// Method to search contacts by name (case-insensitive)
public static void searchContact(ArrayList<Contact> contacts, Scanner scanner) {
System.out.print("Enter name to search: ");
String searchName = scanner.nextLine().toLowerCase();
boolean found = false;
for (Contact c : contacts) {
if (c.getName().toLowerCase().contains(searchName)) {
System.out.println(c);
found = true;
}
}
if (!found) {
System.out.println("No contacts found with name containing: " + searchName);
}
}
// Method to remove contact by exact name match
public static void removeContact(ArrayList<Contact> contacts, Scanner scanner) {
System.out.print("Enter contact name to remove: ");
String removeName = scanner.nextLine();
boolean removed = false;
for (int i = 0; i < contacts.size(); i++) {
if (contacts.get(i).getName().equalsIgnoreCase(removeName)) {
contacts.remove(i);
System.out.println("Contact removed successfully.");
removed = true;
break;
}
}
if (!removed) {
System.out.println("Contact not found: " + removeName);
}
}
}
Step 4: Explanation of Main Program
The Menu Loop
- Uses a do-while loop to keep showing options until the user exits.
- Reads user input for the choice and directs flow using a switch case.
Adding a Contact
- Prompts the user for name and phone number.
- Creates a new Contact object and adds it to the list.
Viewing Contacts
- Lists all saved contacts by calling their
toString()
method. - Shows a message if no contacts are present.
Searching Contacts
- Prompts the user for a search term.
- Performs case-insensitive matching on contact names.
- Prints matches or a "not found" message.
Removing Contacts
- Asks for the exact name of a contact to remove.
- Removes the first matching contact (ignoring case).
- Shows success or failure message accordingly.
How to Run the Project
1.Create two files in the same folder:
Contact.java
(Contact class)
ContactBook.java
(main program)
2.Compile both:
text
javac Contact.java ContactBook.java
3.Run the program:
text
java ContactBook
4.Use the menu options to add, view, search, and remove contacts!
Possible Enhancements
- Validate phone number format.
- Allow updating a contact’s details.
- Remove or view multiple contacts matching a search term.
- Save and load contacts from a file or database.
- Add sorting by name.
- Add email or other fields.
Why This Project Is Great for Beginners
- Object-Oriented Design: Introduction to creating and using classes.
-
Collections: Working with
ArrayList
to manage multiple data items. - User Interaction: Building a menu-driven program to control flow.
- Practical Use Case: Contact management is a common real-world task.
- Extendable: You can keep adding features as your skills grow.
Final Thoughts
Creating a Basic Contact Book combines many core Java skills into a single, manageable project. It encourages thinking about data modeling, interaction, and program structure. Start with this foundation, then challenge yourself by adding more features or converting it into a GUI app! Happy coding!
Check out the YouTube Playlist for great java developer content for basic to advanced topics.
Please Do Subscribe Our YouTube Channel for clearing programming concept and much more ... : CodenCloud
Top comments (0)