CODING
package collections;
import [Link];
import [Link];
import [Link];
public class Phonebook {
private Map<String, String> contacts;
public Phonebook() {
[Link] = new HashMap<>();
}
public void addContact(String name, String phoneNumber) {
if () {
[Link](name, phoneNumber);
[Link]("Contact added: " + name + " - " + phoneNumber);
} else {
[Link]("Contact already exists: " + name);
}
}
public void removeContact(String name) {
if ([Link](name)) {
String removedPhoneNumber = [Link](name);
[Link]("Contact removed: " + name + " - " +
removedPhoneNumber);
} else {
[Link]("Contact not found: " + name);
}
}
public void searchContact(String name) {
if ([Link](name)) {
[Link]("Contact found: " + name + " - " + [Link](name));
} else {
[Link]("Contact not found: " + name);
}
}
public void displayContacts() {
if ([Link]()) {
[Link]("Phonebook is empty.");
} else {
[Link]("Contacts in the phonebook:");
for ([Link]<String, String> entry : [Link]()) {
[Link]([Link]() + " - " + [Link]());
}
}
}
public static void main(String[] args) {
Phonebook phonebook = new Phonebook();
Scanner scanner = new Scanner([Link]);
int choice;
do {
[Link]("\nPhonebook Application");
[Link]("1. Add Contact");
[Link]("2. Remove Contact");
[Link]("3. Search Contact");
[Link]("4. Display Contacts");
[Link]("5. Exit");
[Link]("Enter your choice: ");
choice = [Link]();
[Link]();
switch (choice) {
case 1:
[Link]("Enter contact name: ");
String name = [Link]();
[Link]("Enter contact phone number: ");
String phoneNumber = [Link]();
[Link](name, phoneNumber);
break;
case 2:
[Link]("Enter contact name to remove: ");
String removeName = [Link]();
[Link](removeName);
break;
case 3:
[Link]("Enter contact name to search: ");
String searchName = [Link]();
[Link](searchName);
break;
case 4:
[Link]();
break;
case 5:
[Link]("Exiting program.");
break;
default:
[Link]("Invalid choice. Please enter a number between 1
and 5.");
}
} while (choice != 5);
[Link]();
}
}
OUTPUT