0% found this document useful (0 votes)
12 views

Java Collections

Uploaded by

varshinibl8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Java Collections

Uploaded by

varshinibl8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CODING

package collections;

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

public class Phonebook {


private Map<String, String> contacts;

public Phonebook() {
this.contacts = new HashMap<>();
}

public void addContact(String name, String phoneNumber) {


if (!contacts.containsKey(name)) {
contacts.put(name, phoneNumber);
System.out.println("Contact added: " + name + " - " + phoneNumber);
} else {
System.out.println("Contact already exists: " + name);
}
}

public void removeContact(String name) {


if (contacts.containsKey(name)) {
String removedPhoneNumber = contacts.remove(name);
System.out.println("Contact removed: " + name + " - " +
removedPhoneNumber);
} else {
System.out.println("Contact not found: " + name);
}
}

public void searchContact(String name) {


if (contacts.containsKey(name)) {
System.out.println("Contact found: " + name + " - " + contacts.get(name));
} else {
System.out.println("Contact not found: " + name);
}
}

public void displayContacts() {


if (contacts.isEmpty()) {
System.out.println("Phonebook is empty.");
} else {
System.out.println("Contacts in the phonebook:");
for (Map.Entry<String, String> entry : contacts.entrySet()) {
System.out.println(entry.getKey() + " - " + entry.getValue());
}
}
}

public static void main(String[] args) {


Phonebook phonebook = new Phonebook();
Scanner scanner = new Scanner(System.in);
int choice;

do {
System.out.println("\nPhonebook Application");
System.out.println("1. Add Contact");
System.out.println("2. Remove Contact");
System.out.println("3. Search Contact");
System.out.println("4. Display Contacts");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
choice = scanner.nextInt();
scanner.nextLine();

switch (choice) {
case 1:
System.out.print("Enter contact name: ");
String name = scanner.nextLine();
System.out.print("Enter contact phone number: ");
String phoneNumber = scanner.nextLine();
phonebook.addContact(name, phoneNumber);
break;
case 2:
System.out.print("Enter contact name to remove: ");
String removeName = scanner.nextLine();
phonebook.removeContact(removeName);
break;
case 3:
System.out.print("Enter contact name to search: ");
String searchName = scanner.nextLine();
phonebook.searchContact(searchName);
break;
case 4:
phonebook.displayContacts();
break;
case 5:
System.out.println("Exiting program.");
break;
default:
System.out.println("Invalid choice. Please enter a number between 1
and 5.");
}
} while (choice != 5);

scanner.close();
}
}
OUTPUT

You might also like