JAVA. Personal phone directory app. A personal phone directory contains room for
ID: 3813258 • Letter: J
Question
JAVA. Personal phone directory app.
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. Save the file as PhoneNumbers.java.Explanation / Answer
import java.util.*;
public class PhoneNumbers
{
// This function fills the phonebook with some random first anme and number
// for 10 contacts
private static void initialize(PhoneBook[] addressList)
{
addressList[0] = new PhoneBook("George", "752-019-4502");
addressList[1] = new PhoneBook("Oliver", "973-018-5562");
addressList[2] = new PhoneBook("Alice", "369-201-1098");
addressList[3] = new PhoneBook("Dennis", "019-432-1010");
addressList[4]= new PhoneBook("Brad", "850-439-7647");
addressList[5] = new PhoneBook("Ender", "102-441-9745");
addressList[6] = new PhoneBook("Nin", "782-321-2192");
addressList[7] = new PhoneBook("Garrus", "207-371-8872");
addressList[8] = new PhoneBook("Miranda", "908-210-8458");
addressList[9] = new PhoneBook("Lila", "249-492-1192");
}
// Function to search for a number using first name. It do a case insenstive match
private static String findContactInfoByName(PhoneBook[] addressList, String name, int size)
{
String number = "";
for(int i = 0; i < size; i++)
{
if (addressList[i].getName().toLowerCase().equals(name.toLowerCase()))
{
return addressList[i].getPhoneNumber();
}
}
return number;
}
public static void main(String[] args)
{
// capacity defined in question
final int cap = 30;
// Array to contain name and number list
PhoneBook[] addressList = new PhoneBook[cap];
int size = 10;
initialize(addressList);
// To take input from user
Scanner sc = new Scanner(System.in);
// Loop until user enter quit
while(true)
{
// Ask user for input
System.out.println("Enter first name to search phonebook for number. Enter 'quit' to quit. ");
String name = sc.next();
// if input is quit then quit
if (name.toLowerCase().equals("quit"))
{
break;
}
// find a contact, if not exist prompt user for a number to add it to list.
// Do not prompt if list size is already more than capacity.
String number = findContactInfoByName(addressList, name, size);
if (! number.equals(""))
{
System.out.println("Phone number: " + number);
}
else
{
System.out.println("Name doesn't exist in phonebook.");
if (size < cap)
{
System.out.print("Enter number to add this name to list: ");
number = sc.next();
addressList[size++] = new PhoneBook(name, number);
}
}
}
sc.close();
}
}
//Class to store a contact's first name and number information with appropriate
// getters, setters and constructors.
class PhoneBook
{
PhoneBook(String name, String phoneNumber)
{
this.name = name;
this.phoneNumber = phoneNumber;
}
public String getName()
{
return name;
}
public String getPhoneNumber()
{
return phoneNumber;
}
String phoneNumber;
String name;
}
Sample run
Enter first name to search phonebook for number.
Enter 'quit' to quit.
Miranda
Phone number: 908-210-8458
Enter first name to search phonebook for number.
Enter 'quit' to quit.
John
Enter a number to add to list: 850-439-7648
Enter first name to search phonebook for number.
Enter 'quit' to quit.
John
Phone number: 850-439-7648
Enter first name to search phonebook for number.
Enter 'quit' to quit.
quit
Please rate positively if it answer your question.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.