In: Computer Science
(To be written in Java code)
A personal phone directory contains room for first names and phone numbers for 30 people. Assign names and phone numbers for the first 10 people. Prompt the user for a name, and if the name is found in the list, display the corresponding phone number. If the name is not found in the list, prompt the user for a phone number, and add the new name and phone number to the list.
Continue to prompt the user for names until the user enters quit. After the arrays are full (containing 30 names), do not allow the user to add new entries.
Use the following names and phone numbers:
Name | Phone # |
---|---|
Gina | (847) 341-0912 |
Marcia | (847) 341-2392 |
Rita | (847) 354-0654 |
Jennifer | (414) 234-0912 |
Fred | (414) 435-6567 |
Neil | (608) 123-0904 |
Judy | (608) 435-0434 |
Arlene | (608) 123-0312 |
LaWanda | (920) 787-9813 |
Deepak | (930) 412-0991 |
This code was provided to start with:
import java.util.*;
class PhoneNumbers
{
public static void main (String[] args)
{
// write code here
}
}
the above problem can be divided into following steps :
more details in the code .
text
code :
import java.util.*;
public class Main{
public static Map<String , Integer> phone_list = new
HashMap<String , Integer>(); //global map to store the name
and its number
static int count = 0; //count to keep track of number of
users.
public static void check_user(String user){ //function to check if
user is there
Scanner in = new Scanner(System.in);
if(phone_list.containsKey(user)){
System.out.println("Entry found");
System.out.println(user + " : " + phone_list.get(user));
}
else{
if(count >= 30){
System.out.println("Sorry! , the phone book can have only 30
contacts in it.");
System.exit(0);
}
System.out.println("User not found in the entry , please enter a
phone number for this user");
int p_no = in.nextInt();
phone_list.put(user , p_no);
count ++;
System.out.println("user addedin the phone book");
}
}
public static void add_user(){ //fucntion to add the user
Scanner in = new Scanner(System.in);
if(count >= 30){
System.out.println("Sorry! , the phone book can have only 30
contacts in it.");
System.exit(0);
}
System.out.println("user name : ");
String name = in.nextLine();
System.out.println("Phone no");
int p_no = in.nextInt();
phone_list.put(name , p_no);
count ++;
System.out.println("user addedin the phone book");
}
public static void main (String[] args){
Scanner in = new Scanner(System.in);
add_user();
add_user();
System.out.println("DO you want to add more users: ");
String choice = in.nextLine();
while(!choice.equals("quit")){
add_user();
System.out.println("DO you want to add more users: ");
choice = in.nextLine();
}
System.out.println("Enter the string to search in the phone_book
");
String s = in.nextLine();
check_user(s);
}
}