Final Activity in Data Structures and Algorithms
Members: Carl Jason T. Bernardino, Pearly Rose S. Braza
Course, Yr & Section: BSIT 1A
Contact List Program Implemented in Java
Program Description
The Contact List program is designed to manage a list of contacts,
allowing users to add new contacts, search for existing contacts by
name, and display all contacts stored in the list. The program is
implemented in Java and utilizes an ArrayList to dynamically manage
the collection of contacts.
Program
import java.util.ArrayList;
import java.util.Scanner;
public class ContactList {
static class Contact {
private String name;
private String phoneNumber;
private String email;
public Contact(String name, String phoneNumber, String email) {
this.name = name;
this.phoneNumber = phoneNumber;
this.email = email;
}
public String getName() {
return name;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getEmail() {
return email;
}
@Override
public String toString() {
return "Name: " + name + ", Phone: " + phoneNumber + ", Email: " +
email;
}
}
private ArrayList<Contact> contacts;
public ContactList() {
contacts = new ArrayList<>();
}
public void addContact(String name, String phoneNumber, String email)
{
Contact newContact = new Contact(name, phoneNumber, email);
contacts.add(newContact);
System.out.println("Contact added successfully.");
}
public void searchContacts(String searchName) {
String lowerSearch = searchName.toLowerCase();
boolean found = false;
System.out.println("Search results for \"" + searchName + "\":");
for (Contact contact : contacts) {
if (contact.getName().toLowerCase().contains(lowerSearch)) {
System.out.println(contact);
found = true;
}
}
if (!found) {
System.out.println("No contacts found with the given name.");
}
}
public void displayAllContacts() {
if (contacts.isEmpty()) {
System.out.println("Contact list is empty.");
return;
}
System.out.println("All contacts:");
for (Contact contact : contacts) {
System.out.println(contact);
}
}
public static void main(String[] args) {
ContactList contactList = new ContactList();
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the Contact List Manager!");
while (true) {
System.out.println("\nMenu:");
System.out.println("1. Add Contact");
System.out.println("2. Search Contact by Name");
System.out.println("3. Display All Contacts");
System.out.println("4. Exit");
System.out.print("Enter your choice (1-4): ");
String choice = scanner.nextLine().trim();
System.out.println();
switch (choice) {
case "1":
System.out.print("Enter name: ");
String name = scanner.nextLine().trim();
System.out.print("Enter phone number: ");
String phone = scanner.nextLine().trim();
System.out.print("Enter email: ");
String email = scanner.nextLine().trim();
contactList.addContact(name, phone, email);
break;
case "2":
System.out.print("Enter name to search: ");
String searchName = scanner.nextLine().trim();
System.out.println();
contactList.searchContacts(searchName);
break;
case "3":
contactList.displayAllContacts();
break;
case "4":
System.out.println("Exiting Contact List Manager.
Goodbye!");
scanner.close();
System.exit(0);
break;
default:
System.out.println("Invalid choice. Please enter a
number between 1 and 4.");
}
}
}
}
Functionalities
Add New Contacts: Users can input a contact's name, phone
number, and email to add to the list.
Search Contacts: Users can search for contacts by name, with
case insensitivity and partial matches supported.
Display All Contacts: Users can view all contacts currently
stored in the list.
Exit: Users can exit the program.
Data Structure Explanation
The program uses an ArrayList<Contact> to store contact objects.
The choice of ArrayList is due to its dynamic sizing capabilities, which
allow for easy addition and removal of contacts without the need to
manage the underlying array size manually. This makes it suitable for
applications where the number of contacts can vary significantly.
Key Code Snippets
1. Contact Class: Represents a contact with name, phone number,
and email.
static class Contact {
private String name;
private String phoneNumber;
private String email;
public Contact(String name, String phoneNumber, String email) {
this.name = name;
this.phoneNumber = phoneNumber;
this.email = email;
2. Adding a Contact:
public void addContact(String name, String phoneNumber, String email)
{
Contact newContact = new Contact(name, phoneNumber, email);
contacts.add(newContact);
System.out.println("Contact added successfully.");
}
3. Searching Contacts:
public void searchContacts(String searchName) {
String lowerSearch = searchName.toLowerCase();
// Search logic...
4. Displaying All Contacts:
public void displayAllContacts() {
if (contacts.isEmpty()) {
System.out.println("Contact list is empty.");
return;
}
// Display logic...
Sample Input and Output
Adding a Contact:
Enter name: Pearly Rose
Enter phone number: 09123456789
Enter email:
[email protected] Contact added successfully.
Searching for a Contact:
Enter name to search: Pearly
Search results for "Pearly":
Name: Pearly Rose, Phone: 09123456789, Email:
[email protected]Displaying All Contacts:
All contacts:
Name: Pearly Rose, Phone: 09123456789, Email:
[email protected]