Question

In: Computer Science

(To be written in Java code) A personal phone directory contains room for first names and...

(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

}

}

Solutions

Expert Solution

the above problem can be divided into following steps :

  1. create a global map and count variable .
  2. create a function to add the user . asks for username and phone number , check if count < 30 and add if the yes.
  3. create a function to check if user is present in the phone book . take string name as parameter , check if the map contains the name , if yes then print the name and number , else , ask for number and add the user name and number if count < 30 and increment the count.

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);
}
}


Related Solutions

Language: Java I have written this code but not all methods are running. First method is...
Language: Java I have written this code but not all methods are running. First method is running fine but when I enter the file path, it is not reading it. Directions The input file must be read into an input array and data validated from the array. Input file format (500 records maximum per file): comma delimited text, should contain 6 fields: any row containing exactly 6 fields is considered to be invalid Purpose of this code is to :...
Given the below Java code snippet, assuming the code is correct, and the names are meaningful,...
Given the below Java code snippet, assuming the code is correct, and the names are meaningful, select the best answer for the below questions: public static void main(String[] args) { int numStations = 10; int enter = 0; int exit = 0; Train train = new Train(numStations); for (int station = 0; station < numStations; station++) {      enter = enter + train.enterPassengers(station);      exit = exit + train.exitPassengers(station); } System.out.println("Number of passengers entered the train: " + enter); System.out.println("Number...
This code must be written in Java. This code also needs to include a package and...
This code must be written in Java. This code also needs to include a package and a public static void main(String[] args) Write a program named DayOfWeek that computes the day of the week for any date entered by the user. The user will be prompted to enter a month, day, and year. The program will then display the day of the week (Sunday..Saturday). The following example shows what the user will see on the screen: This program calculates the...
Java Programming: spellcheck Write code to read in a dictionary from the current directory called "dict.txt"...
Java Programming: spellcheck Write code to read in a dictionary from the current directory called "dict.txt" (you can find this in the current directory/data) Add each word to a hash map after splitting words and throwing away punctuation (see the demo). open a file called "spell.txt" to check in the current directory. Print out every word in spell.txt that is NOT in the dictionary. Example: if spell.txt contains: hello, this is a test. 2152189u5 Misspelled! cApitalized The output should be:...
Write a java code snippet to prompt the user for the number of names they’d like...
Write a java code snippet to prompt the user for the number of names they’d like to enter. Create a new array of the size chosen by the user and prompt the user for each of the names. Output the list of names in reverse order.
The solution should be written in Java. Your POSmain program should take three file names from...
The solution should be written in Java. Your POSmain program should take three file names from command line arguments. The first file contains a list of products and their prices; the second and third files are lists of items in two shopping carts of two customers. The POSmain program should first read the price file, then read each of the cart files to load a list of items in a shopping cart and store them in a ShoppingCart objects. The...
This code works fine. In java, can you: change the variable and method names to make...
This code works fine. In java, can you: change the variable and method names to make logical sense add method header comments to describe what the method does and what the params/returns are add a class header comment to describe what the code does import java.util.Arrays; import java.util.Scanner; public class MethodLibrary { public static void main(String args[]) { System.out.println("MethodLibrary"); System.out.println("Example use of methods"); int[] smallNums1 = {2, 1, 7, 4, 3}; display("display:", smallNums1); char [] characters = {'a','b','c','d','e','f'}; first( characters,...
Write a java code to (A) Enter the number of children, (B) Insert names from user,...
Write a java code to (A) Enter the number of children, (B) Insert names from user, (C) Print these names. upload source code with a screenshotfor output in one file with cover page
I need code written in java for one of my projects the instructions are Write a...
I need code written in java for one of my projects the instructions are Write a program that interacts with the user via the console and lets them choose options from a food menu by using the associated item number. It is expected that your program builds an <orderString> representing the food order to be displayed at the end. (See Sample Run Below). Please note: Each student is required to develop their own custom menus, with unique food categories, items...
Write a Personal Ethics Statement includes the following: 1- The first part of your personal code...
Write a Personal Ethics Statement includes the following: 1- The first part of your personal code of ethics is where you will develop the philosophy (why) behind your code of ethics. The only requirement is that the purpose, as well as the code of ethics, be tailored to your needs. 2- The second part of your personal code of ethics is the “I will” section of your personal code of ethics. God, in the Bible, set up His “I will’s”...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT