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
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class ContactDTO {
private String name;
private String phoneNumber;
public ContactDTO(String name, String phoneNumber) {
this.name = name;
this.phoneNumber = phoneNumber;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
public class AddressBook {
private List<ContactDTO> contacts;
public AddressBook() {
contacts = new ArrayList<>();
}
public void addContact(String name, String phoneNumber) {
ContactDTO contact = new ContactDTO(name, phoneNumber);
contacts.add(contact);
System.out.println("Contact added: " + name + ", " + phoneNumber);
}
public void searchByName(String name) {
for (ContactDTO contact : contacts) {
if (contact.getName().equalsIgnoreCase(name)) {
System.out.println("Phone number for " + name + ": " + contact.getPhoneNumber());
return;
}
}
System.out.println("Contact not found: " + name);
}
public void deleteContact(String name) {
ContactDTO foundContact = null;
for (ContactDTO contact : contacts) {
if (contact.getName().equalsIgnoreCase(name)) {
foundContact = contact;
break;
}
}
if (foundContact != null) {
contacts.remove(foundContact);
System.out.println("Contact deleted: " + name);
} else {
System.out.println("Contact not found: " + name);
}
}
public static void main(String[] args) {
AddressBook addressBook = new AddressBook();
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();
addressBook.addContact(name, phoneNumber);
break;
case 2:
System.out.print("Enter name to search: ");
String searchName = scanner.nextLine();
addressBook.searchByName(searchName);
break;
case 3:
System.out.print("Enter name to delete: ");
String deleteName = scanner.nextLine();
addressBook.deleteContact(deleteName);
break;
case 4:
System.out.println("Exiting...");
System.exit(0);
break;
default:
System.out.println("Invalid choice!");
}
}
}
}
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