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

MUST USE A BINARY SEARCH TREE. Write a Java menu-driven program that creates and

ID: 3535026 • Letter: M

Question

MUST USE A BINARY SEARCH TREE.


Write a Java menu-driven program that creates and manipulates a directory of names, telephone numbers, and home addresses. The following information will be stored for each person in the directory: Name (Last, First) Home address (street address, city, state, zip code) Home telephone number Your program should use a Binary Search Tree data structure to store the directory| and should be able to perform the following basic functions. The tree should remain a Binary Search Tree after each of the operations below: Display the entire directory in sorted order by key value (combination of last and first names) Search and display the contents of a particular entry Delete an existing entry Insert an entry Save the entire directory to a file

Explanation / Answer

import java.util.*;

import java.lang.*;

import java.lang.Object;

import jpb.*;


public class PhoneDir {

private static LinkedList phone;


public static void main(String[] args) {



//Display commands

System.out.println("List of Commands: " +

" a - Show 'a'll records " +

" d - 'd'elete the current record " +

" f - change the 'f'irst name in current record " +

" l - change the 'l'ast name in current record " +

" n - add a 'n'ew record " +

" p - change the 'p'hone number in current record " +

" q - 'q'uit " +

" s - 's'elect a record from record list ");


//Prompt user to enter a command

SimpleIO.prompt("Enter a command from the list above: ");

String command = (String) SimpleIO.readLine();


//Determine if command is legal.

if (command.equalsIgnoreCase("a"))

{

showAll();

}

else if (command.equalsIgnoreCase("d"))

{

deleteRecord();

}

else if (command.equalsIgnoreCase("f"))

{

changeFirst();

}

else if (command.equalsIgnoreCase("l"))

{

changeLast();

}

else if (command.equalsIgnoreCase("n"))

{

addNew();

}

else if (command.equalsIgnoreCase("p"))

{

changePhone();

}

else if (command.equalsIgnoreCase("q"))

{

quit();

}

else if (command.equalsIgnoreCase("s"))

{

selectRecord();

}

else

{


//Illegal command will display an error message.

System.out.println("Illegal command");

}

System.out.println();

}


//Shows all records in the directory

private static void showAll(){

try{   

for (int i = 0; i < phone.size(); i++)

System.out.println(phone);

}

catch (Exception e) {

  

}

}


//Deletes the current record

private static void deleteRecord()

{

SimpleIO.prompt("Enter name to delete: ");

String key = SimpleIO.readLine();

for (int i = 0; i < phone.size(); i++)

{

PhoneDir curr = (PhoneDir) phone.get(i);

String name = curr.getName();

if (name.startsWith(key));

  

phone.remove((String) key);

}

}


//Changes the first name of the current record

private static void changeFirst()

{

SimpleIO.prompt("Enter name to change: ");

String key = SimpleIO.readLine();

for (int i = 0; i < phone.size(); i++)

{

PhoneDir curr = (PhoneDir) phone.get(i);

String FirstName = curr.getFirstName();

if (FirstName.startsWith(key))

System.out.println(curr.getFirstName() + " " + curr.getFirstName());

}

SimpleIO.prompt("Enter name change:");

phone.add((String) key);

}


//Change the last name of the current record

private static void changeLast()

{

SimpleIO.prompt("Enter name to change: ");

String key = SimpleIO.readLine();

for (int i = 0; i < phone.size(); i++)

{

PhoneDir curr = (PhoneDir) phone.get(i);

@SuppressWarnings("LocalVariableHidesMemberVariable")

String LastName = curr.getLastName().toLowerCase();

if (LastName.startsWith(key))

System.out.println(curr.getLastName() + " " + curr.getLastName());

}

SimpleIO.prompt("Enter name changes: ");

phone.add((String) key);

}


//Add a new record

private static void addNew()

{

try {

int number;

SimpleIO.prompt("Enter name: ");

String name = (String) SimpleIO.readLine();

System.out.print("Enter phone number: " );


Scanner input = new Scanner(System.in);

number = input.nextInt();

phone.add(name);

phone.add(number);

}

catch (Exception e){

  

}

}


//Change the phone number of the current record

private static void changePhone()

{

SimpleIO.prompt("Enter number to change: ");

String key = (String)SimpleIO.readLine();

for (int i = 0; i < phone.size(); i++)

{

PhoneDir curr = (PhoneDir) phone.get(i);

String name = (String) curr.getNumber().toLowerCase();

if (name.startsWith(key))

System.out.println(curr.getNumber() + " " + curr.getNumber());

phone.add((String) key);

}

}

//Quits the program

private static void quit()

{

System.exit(0);

}

//Selects a record from the record list

private static void selectRecord()

{

SimpleIO.prompt("Enter name to change: ");

String key = SimpleIO.readLine();

for (int i = 0; i < phone.size(); i++)

{

PhoneDir curr = (PhoneDir) phone.get(i);

String name = curr.getName().toLowerCase();

if (name.startsWith(key))

System.out.println(curr.getName() + " " + curr.getName());

}

}

private String FirstName;

private String LastName;

private String name;

private String number;


public String getFirstName()

{

return FirstName;

}

public String getLastName()

{

return LastName;

}


public String getName()

{

return name;

}

public String getNumber()

{

return number;

}



}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote