Address book

Design a simple address book application using HashMap or TreeMap. Implement features such as adding contacts, searching by name, and deleting contacts.

Approach 1

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    private Map<String, String> contacts;

    public Main() {
        contacts = new HashMap<>();
    }

    public void addContact(String name, String phoneNumber) {
        contacts.put(name, phoneNumber);
        System.out.println("Contact added: " + name + ", " + phoneNumber);
    }

    public void searchByName(String name) {
        String phoneNumber = contacts.get(name);
        if (phoneNumber != null) {
            System.out.println("Phone number for " + name + ": " + phoneNumber);
        } else {
            System.out.println("Contact not found: " + name);
        }
    }

    public void deleteContact(String name) {
        String phoneNumber = contacts.remove(name);
        if (phoneNumber != null) {
            System.out.println("Contact deleted: " + name);
        } else {
            System.out.println("Contact not found: " + name);
        }
    }

    public static void main(String[] args) {
        Main Main = new Main();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("\nAddress Book Menu:");
            System.out.println("1. Add Contact");
            System.out.println("2. Search by Name");
            System.out.println("3. Delete Contact");
            System.out.println("4. Exit");
            System.out.print("Enter your choice: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // Consume newline
            switch (choice) {
                case 1:
                    System.out.print("Enter name: ");
                    String name = scanner.nextLine();
                    System.out.print("Enter phone number: ");
                    String phoneNumber = scanner.nextLine();
                    Main.addContact(name, phoneNumber);
                    break;
                case 2:
                    System.out.print("Enter name to search: ");
                    String searchName = scanner.nextLine();
                    Main.searchByName(searchName);
                    break;
                case 3:
                    System.out.print("Enter name to delete: ");
                    String deleteName = scanner.nextLine();
                    Main.deleteContact(deleteName);
                    break;
                case 4:
                    System.out.println("Exiting...");
                    System.exit(0);
                    break;
                default:
                    System.out.println("Invalid choice!");
            }
        }
    }
}

This program allows users to add contacts, search for contacts by name, and delete contacts. It uses a HashMap to store contacts, where the name is the key and the phone number is the value. The main method provides a simple text-based menu for users to interact with the address book.

Approach 2

In this implementation:

  • ContactDTO class represents the data transfer object for a contact, encapsulating the contact's name and phone number.

  • AddressBook class manages the list of contacts and provides methods to add, search, and delete contacts.

  • The main method provides a simple text-based menu for users to interact with the address book. Users can add contacts, search for contacts by name, and delete contacts.

Last updated

Was this helpful?