Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The two methods, removeNumber and displayRecords has already completed. Complete

ID: 674058 • Letter: T

Question

The two methods, removeNumber and displayRecords has already completed. Complete the remaining 4 methods.

// Represents a record containing a name and a phone number
class PhoneRecord {
   private String firstName;
   private String lastName;
   private String number;
   private int zipCode;

   /**
   * NAME:       PhoneRecord
   * BEHAVIOR:   Constructs a phone record containing the specified name and phone number
   * PARAMETERS: personName - name of a person phoneNumber - phone number for that person
   */

   public PhoneRecord(String firstName, String lastName, String phoneNumber, int zipCode) {
       this.firstName = firstName;
       this.lastName = lastName;
       number = phoneNumber;
       this.zipCode = zipCode;
   }

   /**
   * NAME:       getName
   * BEHAVIOR:   Returns the name stored in this record
   * PARAMETERS: None
   * RETURNS:    The name stored in this record
   */
   public String getName() {
       return firstName+" "+lastName;
   }

   /**
   * NAME:       getNumber
   * BEHAVIOR:   Returns the phone number stored in this record
   * PARAMETERS: None
   * RETURNS:    The phone number stored in this record
   */
   public String getNumber() {
       return number;
   }
  
   /**
   * NAME:       getZipCode
   * BEHAVIOR:   Returns the zip code stored in this record
   * PARAMETERS: None
   * RETURNS:    zip code
   */

   public int getZipCode(){
       return zipCode;
   }
}

-----------------------------------------------------------------------------------------------------------------------------------------

import java.util.Scanner;

public class PhoneDirectory {
   // Class variables
   static PhoneRecord[] records = new PhoneRecord[50];
   static int recordCount = 0;
   static Scanner sc ;


   public static void main(String[] args) {


       // Display list of commands
       System.out.println("Phone directory commands: "
               + " a - Add a new phone number "
               + " d - Display All "
               + " da -Display all with same Last Name "
               + " f - Find a phone number "
               + " r - Remove a phone number "
               + " rc -Remove all records with same zip code "
               + " q - Quit ");

       // Read and execute commands
       while (true) {
           sc= new Scanner(System.in);

           // Prompt user to enter a command
           System.out.print("Enter command (a, d, da, f, r, rc or q): ");
           String command = sc.nextLine();

           // Determine whether command is "a", "f", "q", or
           // illegal; execute command if legal.
           if (command.equalsIgnoreCase("a")) {

               // Command is "a". Call addNumber to add a new
               // name and number to the database
               addNumber();
           } else if (command.equalsIgnoreCase("f")) {

               // Command is "f". Call findNumber to find phone
               // numbers for given first name.
               findNumber();
           } else if (command.equalsIgnoreCase("d")) {

               // Command is "d". Call displayRecords to display all phone
               // numbers
               displayRecords();
           }else if (command.equalsIgnoreCase("da")) {

               // Command is "d". Call displayRecords to display all phone
               // numbers
               displayRecordsWithSameLastName();
           }
           else if (command.equalsIgnoreCase("r")) {

               // Command is "r". Call removeNumber to remove phone
               // numbers that match the user's criteria.
               removeNumber();
           }else if (command.equalsIgnoreCase("rc")) {

               // Command is "r". Call removeNumber to remove all phone
               // numbers that match the given zipcode.
               removeNumberWithSameZipCode();
           }
           else if (command.equalsIgnoreCase("q")) {

               // Command is "r". Call removeNumber to remove phone
               // numbers that match the user's criteria.
               break;
           }
           else {

               // Command is illegal. Display error message.
               System.out.println("Command was not recognized; "
                       + "please enter only a, d, da, f, r, rc or q.");
           }

           System.out.println();
       }
   }

   /**
   * NAME:       addNumber
   * BEHAVIOR:   add a new name (first and last) , phone number and zipcode to the database
   * PARAMETERS: None
   * COMMENTS:   refer the word doc for example of adding record
   */
   private static void addNumber() {
   }

   /**
   * NAME:       removeNumber
   * BEHAVIOR:   removes the first record of a given phone no.,
   */

   private static void removeNumber() {
       System.out.println("Enter phone no:");
       String pFind = sc.next();
       int index = 0;
       boolean found = false;
       for(int i=0;i<recordCount;i++){
           if(records[i].getNumber().equals(pFind))
           {
               index =i;
               for(int j=index;j<recordCount;j++){
                   records[j]=records[j+1];
               }
               System.out.println("Record with phone no: "+ pFind+" deleted. Remaining records:"+(recordCount-1));
               records[recordCount-1]=null;
               recordCount--;
               found = true;
               break;
           }

       }
       if(!found)
           System.out.println("Record not found.");


   }

   /**
   * NAME:       displayRecords
   * BEHAVIOR:   display all phone records
   */
   private static void displayRecords() {
       for (int i = 0; i < recordCount; i++) {
           System.out.println(i + 1 + ". " + records[i].getName() + " "
                   + records[i].getNumber()+" "+records[i].getZipCode());
       }
   }

   /**
   * NAME:       removeNumberWithSameZipCode
   * BEHAVIOR:   remove all phone numbers that match the given zipcode.
   */

   private static void removeNumberWithSameZipCode() {
       // your code here
   }

   /**
   * NAME:       findNumber
   * BEHAVIOR:   findNumber to find phone number for a give first name
   * COMMENT:    it should list all the numbers for matching input (Refer word doc for example)
   * HINT:        use PhoneRecord getName() method in conjunction with startsWith(String prefix) String method, see:
   *            http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#startsWith%28java.lang.String%29
   */

   private static void findNumber() {
       // your code here
      
   }

   /**
   * NAME:       displayRecordsWithSameLastName
   * BEHAVIOR:   display all phone records with given last name
   */
   private static void displayRecordsWithSameLastName(){
       // your code here

   }

}

Explanation / Answer

This is the file PhoneDirectory.java


// Class variables
static PhoneRecord[] records = new PhoneRecord[50];
static int recordCount = 0;
static Scanner sc ;

public static void main(String[] args) {

// Display list of commands
System.out.println("Phone directory commands: "
+ " a - Add a new phone number "
+ " d - Display All "
+ " da -Display all with same Last Name "
+ " f - Find a phone number "
+ " r - Remove a phone number "
+ " rc -Remove all records with same zip code "
+ " q - Quit ");
// Read and execute commands
while (true) {
sc= new Scanner(System.in);
// Prompt user to enter a command
System.out.print("Enter command (a, d, da, f, r, rc or q): ");
String command = sc.nextLine();
// Determine whether command is "a", "f", "q", or
// illegal; execute command if legal.
if (command.equalsIgnoreCase("a")) {
// Command is "a". Call addNumber to add a new
// name and number to the database
addNumber();
} else if (command.equalsIgnoreCase("f")) {
// Command is "f". Call findNumber to find phone
// numbers for given first name.
findNumber();
} else if (command.equalsIgnoreCase("d")) {
// Command is "d". Call displayRecords to display all phone
// numbers
displayRecords();
}else if (command.equalsIgnoreCase("da")) {
// Command is "d". Call displayRecords to display all phone
// numbers
displayRecordsWithSameLastName();
}
else if (command.equalsIgnoreCase("r")) {
// Command is "r". Call removeNumber to remove phone
// numbers that match the user's criteria.
removeNumber();
}else if (command.equalsIgnoreCase("rc")) {
// Command is "r". Call removeNumber to remove all phone
// numbers that match the given zipcode.
removeNumberWithSameZipCode();
}
else if (command.equalsIgnoreCase("q")) {
// Command is "r". Call removeNumber to remove phone
// numbers that match the user's criteria.
break;
}
else {
// Command is illegal. Display error message.
System.out.println("Command was not recognized; "
+ "please enter only a, d, da, f, r, rc or q.");
}
System.out.println();
}
}
/**
* NAME: addNumber
* BEHAVIOR: add a new name (first and last) , phone number and zipcode to the database
* PARAMETERS: None
* COMMENTS: refer the word doc for example of adding record
*/
private static void addNumber() {
System.out.print("Enter the first name: ");
String firstName = sc.next();
System.out.print("Enter the last name: ");
String lastName = sc.next();
System.out.print("Enter phone no: ");
String pNumber = sc.next();
System.out.print("Enter the zipcode: ");
int zipcode = sc.nextInt();
records[recordCount] = new PhoneRecord(firstName, lastName, pNumber, zipcode);
recordCount++;
System.out.println("One record added. Total records: "+recordCount);
}
/**
* NAME: removeNumber
* BEHAVIOR: removes the first record of a given phone no.,
*/
private static void removeNumber() {
System.out.println("Enter phone no:");
String pFind = sc.next();
int index = 0;
boolean found = false;
for(int i=0;i<recordCount;i++){
if(records[i].getNumber().equals(pFind))
{
index =i;
for(int j=index;j<recordCount-1;j++) records[j]=records[j+1];
System.out.println("Record with phone no: "+ pFind+" deleted. Remaining records:"+(recordCount-1));
records[recordCount-1]=null;
recordCount--;
found = true;
break;
}   
}
if(!found)
System.out.println("Record not found.");

}
/**
* NAME: displayRecords
* BEHAVIOR: display all phone records
*/
private static void displayRecords() {
for (int i = 0; i < recordCount; i++) {
System.out.println(i + 1 + ". " + records[i].getName() + " "
+ records[i].getNumber()+" "+records[i].getZipCode());
}
}
/**
* NAME: removeNumberWithSameZipCode
* BEHAVIOR: remove all phone numbers that match the given zipcode.
*/
private static void removeNumberWithSameZipCode() {
int removedRecordCount = 0;
System.out.println("Enter zipcode:");
int pFind = sc.nextInt();
int index = 0;
boolean found = false;
for(int i=0;i<recordCount;i++){
if(records[i].getZipCode()==(pFind))
{
removedRecordCount++;
index =i;
for(int j=index;j<recordCount-1;j++) records[j]=records[j+1];
}
System.out.println("Records with Zipcode: "+ pFind+" deleted. Remaining records:"+(recordCount-removedRecordCount));
for(i=0;i<removedRecordCount;i++)
records[recordCount-1-i]=null;
recordCount = recordCount - removedRecordCount;
found = true;
break;
}
if(!found)
System.out.println("Record not found.");
}
/**
* NAME: findNumber
* BEHAVIOR: findNumber to find phone number for a give first name
* COMMENT: it should list all the numbers for matching input (Refer word doc for example)
* HINT: use PhoneRecord getName() method in conjunction with startsWith(String prefix) String method, see:
* http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#startsWith%28java.lang.String%29
*/
private static void findNumber() {
System.out.println("Enter the first name: ");
String pFind = sc.next();
boolean found = false;
for(int i = 0; i < recordCount; i++) {
if(records[i].getName().startsWith(pFind))
{
System.out.println(records[i].getName()+": "+records[i].getNumber());
found = true;
}
}
if(!found)
System.out.println("No record found with given FirstName.");
  
}
/**
* NAME: displayRecordsWithSameLastName
* BEHAVIOR: display all phone records with given last name
*/
private static void displayRecordsWithSameLastName(){
System.out.println("Enter the last name: ");
String pFind = sc.next();
boolean found = false;
for(int i = 0; i < recordCount; i++) {
if(records[i].getName().endsWith(pFind))
{
System.out.println(records[i].getName()+": "+records[i].getNumber());
found = true;
}
}
if(!found)
System.out.println("No record found with given LastName.");   
}

This is the file PhoneRecord.java

// Represents a record containing a name and a phone number
class PhoneRecord {
private String firstName;
private String lastName;
private String number;
private int zipCode;
/**
* NAME: PhoneRecord
* BEHAVIOR: Constructs a phone record containing the specified name and phone number
* PARAMETERS: personName - name of a person phoneNumber - phone number for that person
*/
public PhoneRecord(String firstName, String lastName, String phoneNumber, int zipCode) {
this.firstName = firstName;
this.lastName = lastName;
this.number = phoneNumber;
this.zipCode = zipCode;
}
/**
* NAME: getName
* BEHAVIOR: Returns the name stored in this record
* PARAMETERS: None
* RETURNS: The name stored in this record
*/
public String getName() {
return firstName+" "+lastName;
}
/**
* NAME: getNumber
* BEHAVIOR: Returns the phone number stored in this record
* PARAMETERS: None
* RETURNS: The phone number stored in this record
*/
public String getNumber() {
return number;
}
  
/**
* NAME: getZipCode
* BEHAVIOR: Returns the zip code stored in this record
* PARAMETERS: None
* RETURNS: zip code
*/
public int getZipCode(){
return zipCode;
}
}