Menu-Driven program for Phone Directory
Problem Statement: Write a menu-driven program for using switch-case with following features:
- Store Contact numbers of people
- Search for the Contact numbers using their names
- Search for the Contact numbers using their number
- Delete a Contact number
- Update a Contact number
Examples:
Approach: The idea is to use switch case for switching the case for menu-driven program for the phonebook directory and store the contact number in the hash-map and search the contact in the hash-map in O(1) time.
Below is the implementation of the above approach:
C++14
//C++14 implementation of menu driven //Phone Book Directory #include <iostream> #include <string> #include <map> using namespace std; void GFG(); // Global variable for phone book directory map<string, string> phoneBook; // List of Inputs string inputList[] = { "1" , "cool 123456789" , "y" , "2" , "cool" , "n" }; int index = -1; // Function to provide sample inputs string getInput() { index++; return inputList[index]; } // Function to delete a contact void deleteContact() { string name = getInput(); cout<< "\nEnter the contact name to be deleted:\n" ; cout<<name<<endl; if (phoneBook.count(name) > 0) { phoneBook.erase(name); cout << "Contact Deleted!" << endl; } else { cout << "Contact not found!" << endl; } cout<< "Do you want to perform more operations? (y / n)\n" ; char choice = getInput()[0]; cout<<choice<<endl; if (choice == 'y' || choice == 'Y' ) { GFG(); } } // Function to update a contact number void updateContact() { cout<< "\nEnter the contact name to be updated:\n" ; string name = getInput(); if (phoneBook.count(name) > 0) { cout<< "\nEnter the new contact number:" ; string phone = getInput(); phoneBook[name] = phone; cout << "Contact updated!" << endl; } else { cout << "Contact not found!" << endl; } cout<< "Do you want to perform more operations? (y / n)\n" ; char choice = getInput()[0]; cout<<choice<<endl; if (choice == 'y' || choice == 'Y' ) { GFG(); } } // Function to search a contact void searchContact() { cout<< "\nEnter the name to be searched:\n" ; string name = getInput(); cout<<name<<endl; if (phoneBook.count(name) > 0) { cout << "Contact Found! \n" << name << " " << phoneBook[name] << endl; } else { cout << "Contact not found!" << endl; } cout<< "Do you want to perform more operations? (y / n)\n" ; char choice = getInput()[0]; cout<<choice<<endl; if (choice == 'y' || choice == 'Y' ) { GFG(); } } // Function to store a contact void storeContact() { cout<< "\nEnter the name and phone number separated by space - \n" ; string namePhone = getInput(); cout<<namePhone<<endl; int spacePos = namePhone.find( ' ' ); string name = namePhone.substr(0, spacePos); string phone = namePhone.substr(spacePos + 1); if (phoneBook.count(name) > 0) { cout << "Contact Already exists!" << endl; } else { phoneBook[name] = phone; cout << "Contact Stored!" << endl; } cout<< "\nDo you want to perform more operations? (y / n)\n" ; char choice = getInput()[0]; cout<<choice<<endl; if (choice == 'y' || choice == 'Y' ) { GFG(); } } // Main Function for Menu-Driven void GFG() { cout << "Please choose any choice from below -" << endl; cout << "Store Contact number (1)" << endl; cout << "Search Contact number (2)" << endl; cout << "Update Contact number (3)" << endl; cout << "Delete Contact number (4)" << endl; int choice = stoi(getInput()); cout<<choice<<endl; switch (choice) { case 1: storeContact(); break ; case 2: searchContact(); break ; case 3: updateContact(); break ; case 4: deleteContact(); break ; default : cout << "Invalid choice!" << endl; } } int main() { cout << "----------------------Welcome to GeeksforGeeks Phonebook----------------------" << endl; GFG(); return 0; } |
Java
//Java implementation of menu driven //Phone Book Directory import java.util.*; public class PhoneBook { public static HashMap<String, Integer> contact = new HashMap<String, Integer>(); //List of Inputs public static List<String> inputLis = Arrays.asList( "1" , "cool 123456789" , "y" , "2" , "cool" , "n" ); public static int indi = - 1 ; // Function to provide sample inputs // Remove this function to run on // Custom Inputs public static String input() { indi += 1 ; System.out.println(inputLis.get(indi)); return inputLis.get(indi); } // Function to delete a contact public static void delete() { System.out.println( "Enter the contact" + " name to be deleted" ); String name = input().strip(); if (contact.containsKey(name)) { contact.remove(name); System.out.println( "Contact Deleted !\n" ); } else { System.out.println( "Contact not found !\n" ); } System.out.println( "Do you want to perform more" + " operations? (y / n)" ); String choice = input().strip(); if (choice.equals( "y" )) { main(); } } // Function to update a contact number public static void update() { System.out.println( "Enter the contact name" + " to be updated - " ); String name = input().strip(); if (contact.containsKey(name)) { System.out.println( "Enter the new" + " contact number - " ); int phone = Integer.parseInt(input()); contact.put(name, phone); System.out.println( "Contact updated\n" ); } else { System.out.println( "Contact not found !\n" ); } System.out.println( "Do you want to perform " + "more operations? (y / n)" ); String choice = input().strip(); if (choice.equals( "y" )) { main(); } } // Function to search a contact public static void search() { System.out.println( "Enter the name to be searched - " ); String name = input().strip(); if (contact.containsKey(name)) { System.out.println( "Contact Found !" ); System.out.println(name + " " + contact.get(name)); } else { System.out.println( "Contact not found !\n" ); } System.out.println( "Do you want to perform more" + " operations? (y / n)" ); String choice = input().strip(); if (choice.equals( "y" )) { main(); } } // Function to store a contact public static void store() { System.out.println( "\n\nEnter the name" + " and phone number" + " separated by space - " ); String[] namePhone = input().strip() .split( " " ); String name = namePhone[ 0 ]; int phone = Integer.parseInt(namePhone[ 1 ]); if (contact.containsKey(name)) { System.out.println( "Contact Already exists !\n" ); } else { contact.put(name, phone); System.out.println( "Contact Stored !" ); } System.out.println( "Do you want to perform more" + " operations? (y / n)" ); String choice = input().strip(); if (choice.equals( "y" )) { main(); } } // Main Function for Menu-Driven public static void main() { System.out.println( "Please choose any choice" + " from below -\n\n\n" ); System.out.println( "Store Contact number (1)" ); System.out.println( "Search Contact number (2)" ); System.out.println( "Update Contact number (3)" ); System.out.println( "Delete Contact number (4)" ); int choice = Integer.parseInt(input()); switch (choice) { case 1 : store(); break ; case 2 : search(); break ; case 3 : update(); break ; case 4 : delete(); break ; default : break ; } } public static void main(String[] args) { System.out.println( "----------------------" + "Welcome to GeeksforGeeks Phonebook" + "----------------------" ); main(); } } |
Python3
# Python implementation of menu driven # Phone Book Directory contact = {} # List of Inputs inputLis = [ "1" , "cool 123456789" , "y" , "2" , "cool" , "n" ] indi = - 1 # Function to provide sample inputs # Remove this function to run on # Custom Inputs def input (): global indi indi + = 1 print (inputLis[indi]) return inputLis[indi] # Function to delete a contact def delete(): global contact print ( "Enter the contact" \ " name to be deleted" ) name = input ().strip() if name in contact: del (contact[name]) print ( "Contact Deleted !\n" ) else : print ( "Contact not found !\n" ) print ( "Do you want to perform more" \ " operations? (y / n)" ) choice = input ().strip() if choice = = "y" : main() # Function to update a contact number def update(): global contact print ( "Enter the contact name" \ " to be updated - " ) name = input ().strip() if name in contact: print ( "Enter the new" \ " contact number - " ) phone = int ( input ()) contact[name] = phone print ( "Contact updated\n" ) else : print ( "Contact not found !\n" ) print ( "Do you want to perform " \ "more operations? (y / n)" ) choice = input ().strip() if choice = = "y" : main() # Function to search a contact def search(): global contact print ( "Enter the name to be searched - " ) name = input ().strip() if name in contact: print ( "Contact Found !" ) print (name, contact[name]) else : print ( "Contact not found !\n" ) print ( "Do you want to perform more" \ " operations? (y / n)" ) choice = input ().strip() if choice = = "y" : main() # Function to store a contact def store(): print ( "\n\nEnter the name" \ " and phone number" + \ " separated by space - " ) name, phone = map ( str , \ input ().strip()\ .split( " " )) global contact if name in contact: print ( "Contact Already exists !\n" ) else : contact[name] = phone print ( "Contact Stored !" ) print ( "Do you want to perform more" \ " operations? (y / n)" ) choice = input ().strip() if choice = = "y" : main() # Main Function for Menu-Driven def main(): print ( "Please choose any choice" \ " from below -\n\n\n" ) print ( "Store Contact number (1)" ) print ( "Search Contact number (2)" ) print ( "Update Contact number (3)" ) print ( "Delete Contact number (4)" ) choice = int ( input ()) choice_dict = { 1 : store, 2 : search, 3 : update, 4 : delete } choice_dict[choice]() if __name__ = = "__main__" : print ( "----------------------" + \ "Welcome to GeeksforGeeks Phonebook" + \ "----------------------" ) main() |
C#
//C# implementation of menu driven //Phone Book Directory using System; using System.Collections.Generic; public class PhoneBook { public static Dictionary< string , int > contact = new Dictionary< string , int >(); //List of Inputs public static List< string > inputLis = new List< string >(){ "1" , "cool 123456789" , "y" , "2" , "cool" , "n" }; public static int indi = -1; // Function to provide sample inputs // Remove this function to run on // Custom Inputs public static string input() { indi += 1; Console.WriteLine(inputLis[indi]); return inputLis[indi]; } // Function to delete a contact public static void delete() { Console.WriteLine( "Enter the contact" + " name to be deleted" ); string name = input().Trim(); if (contact.ContainsKey(name)) { contact.Remove(name); Console.WriteLine( "Contact Deleted !\n" ); } else { Console.WriteLine( "Contact not found !\n" ); } Console.WriteLine( "Do you want to perform more" + " operations? (y / n)" ); string choice = input().Trim(); if (choice.Equals( "y" )) { main(); } } // Function to update a contact number public static void update() { Console.WriteLine( "Enter the contact name" + " to be updated - " ); string name = input().Trim(); if (contact.ContainsKey(name)) { Console.WriteLine( "Enter the new" + " contact number - " ); int phone = Int32.Parse(input()); contact[name] = phone; Console.WriteLine( "Contact updated\n" ); } else { Console.WriteLine( "Contact not found !\n" ); } Console.WriteLine( "Do you want to perform " + "more operations? (y / n)" ); string choice = input().Trim(); if (choice.Equals( "y" )) { main(); } } // Function to search a contact public static void search() { Console.WriteLine( "Enter the name to be searched - " ); string name = input().Trim(); if (contact.ContainsKey(name)) { Console.WriteLine( "Contact Found !" ); Console.WriteLine(name + " " + contact[name]); } else { Console.WriteLine( "Contact not found !\n" ); } Console.WriteLine( "Do you want to perform more" + " operations? (y / n)" ); string choice = input().Trim(); if (choice.Equals( "y" )) { main(); } } // Function to store a contact public static void store() { Console.WriteLine( "\n\nEnter the name" + " and phone number" + " separated by space - " ); string [] namePhone = input().Trim().Split( " " ); string name = namePhone[0]; int phone = Int32.Parse(namePhone[1]); if (contact.ContainsKey(name)) { Console.WriteLine( "Contact Already exists !\n" ); } else { contact.Add(name, phone); Console.WriteLine( "Contact Stored !" ); } Console.WriteLine( "Do you want to perform more" + " operations? (y / n)" ); string choice = input().Trim(); if (choice.Equals( "y" )) { main(); } } // Main Function for Menu-Driven public static void main() { Console.WriteLine( "Please choose any choice" + " from below -\n\n\n" ); Console.WriteLine( "Store Contact number (1)" ); Console.WriteLine( "Search Contact number (2)" ); Console.WriteLine( "Update Contact number (3)" ); Console.WriteLine( "Delete Contact number (4)" ); int choice = Int32.Parse(input()); switch (choice) { case 1: store(); break ; case 2: search(); break ; case 3: update(); break ; case 4: delete(); break ; default : break ; } } public static void Main( string [] args) { Console.WriteLine( "----------------------" + "Welcome to GeeksforGeeks Phonebook" + "----------------------" ); main(); } } |
Output:
Please Login to comment...