Menu

[Solved]Java Please Write Program Provides Way Store Retrieve Telephone Numbers Three Classes Pers Q37051258

In Java please. Write a program that provides away for you to store and retrieve telephone numbers. It has threeclasses Person Class, PhoneBook class, and MenuClass. Design a console program that provides thefollowing operations:

Add: Adds a person’s name and phone number tothe phone book.

Delete: Deletes a given person’s name and phonenumber from the phone book, given only the name.

Find: Locates a person’s phone number, givenonly the person’s name.

Change: Changes a person’s phone number, giventhe person’s name and new phone number.

Quit: Quits the application, after first savingthe phone book in a text file.

You can proceed as follows:

– Design and implement the class Person, whichrepresents the name and phone number of a person. You will storeinstances of this class in the phone book.

-Design and implement the class PhoneBook,which represents the phone book. The class should contain a binarysearch tree as a data field. This tree contains the people in thebook.

-Add methods that use a text file to save and restore thetree.

– Design and implement the class Menu, whichprovides the program’s user interface.

The program should read data from a text file when it begins andsave data into the text file when the user quits the program. Canyou fix this code?

//Person.java

package sample;

public class Person {

   private String name;
   private String phoneNo;
   public Person(String name, String phoneNo) {
       super();
       this.name = name;
       this.phoneNo = phoneNo;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public String getPhoneNo() {
       return phoneNo;
   }
   public void setPhoneNo(String phoneNo) {
       this.phoneNo = phoneNo;
   }
  
  
}

//PhoneBook.java

package sample;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.StringTokenizer;

class BinarySearchTree {
     
/* Class containing left and right child of current node and keyvalue*/
class Node {
Person p;
Node left, right;

public Node(Person parg) {
p = parg;
left = right = null;
}
}

// Root of BST
Node root;

// Constructor
BinarySearchTree() {
root = null;
}

// This method mainly calls insertRec()
void insert(Person p) {
root = insertRec(root, p);
}

/* A recursive function to insert a new key in BST */
Node insertRec(Node root, Person p) {

/* If the tree is empty, return a new node */
if (root == null) {
root = new Node(p);
return root;
}

/* Otherwise, recur down the tree */
int result = p.getName().compareTo(root.p.getName());
if ( result < 0 )
root.left = insertRec(root.left, p);
else if (result > 0)
root.right = insertRec(root.right, p);

/* return the (unchanged) node pointer */
return root;
}

// This method mainly calls InorderRec()
void inorder() {
inorderRec(root);
}

// A utility function to do in order traversal of BST
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.println(root.p);
inorderRec(root.right);
}
}
  
void deleteKey(String name)
{
root = deleteRec(root, name);
}

/* A recursive function to insert a new key in BST */
Node deleteRec(Node root, String name)
{
/* Base Case: If the tree is empty */
if (root == null) return root;

/* Otherwise, recur down the tree */
int result = name.compareTo(root.p.getName());
if (result < 0)
root.left = deleteRec(root.left, name);
else if (result > 0)
root.right = deleteRec(root.right, name);

// if key is same as root’s key, then This is the node
// to be deleted
else
{
// node with only one child or no child
if (root.left == null)
return root.right;
else if (root.right == null)
return root.left;

// node with two children: Get the inorder successor(smallest
// in the right subtree)
root.p = minValue(root.right);

// Delete the inorder successor
root.right = deleteRec(root.right, name);
}

return root;
}
  
Person minValue(Node root)
{
Person minv = root.p;
while (root.left != null)
{
minv = root.left.p;
root = root.left;
}
return minv;
}
  
public Person find(String name)
{
   Node n=search(root, name);
  
   return n.p;
}
public Node search(Node root,String name)
{
// Base Cases: root is null or key is present at root
if (root==null || root.p.getName().equals(name))
return root;

// val is greater than root’s key
int result = name.compareTo(root.p.getName());
if (result < 0)
return search(root.left, name);

// val is less than root’s key
return search(root.right, name);
}
  
void QuitApp(OutputStream out) throws IOException
{
   serializeTree(root, out);
}
  
void serializeTree(Node root, OutputStream out) throws IOException{

       //root can be null too!
       if (out == null) {
           out.write(0);//this is ‘null’ node. left and right written too.
           return;
       }
       String str =root.p.getName().concat(” “);
       str =str.concat(root.p.getPhoneNo());
       byte[] data = new byte[2];
       data = str.getBytes();
       out.write(data.length);
       out.write(data);
       serializeTree(root.left,out);
       serializeTree(root.right,out);
   }

   Node deserializeTree(InputStream in) throwsIOException {

       int size = in.read(); //getssize of data.
       if (size == 0) {
           returnnull;
       }
       byte[] data = new byte[size];
       in.read(data, 0,data.length);
       String str =Arrays.toString(data);
      
       StringTokenizer st = newStringTokenizer(str, ” “);
       Person p = newPerson(st.nextToken(), st.nextToken());
       Node n = new Node(p);
       n.left = deserializeTree(in);
       n.right =deserializeTree(in);
       return n;
   }
}

public class PhoneBook {
  
   BinarySearchTree phonebook = newBinarySearchTree();

   public BinarySearchTree getPhonebook() {
       return phonebook;
   }

   public void setPhonebook(BinarySearchTreephonebook) {
       this.phonebook = phonebook;
   }

  
}

//Menu.java

package sample;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Scanner;

public class Menu {
   //Add
   //Delete
   //Find
   //Change
   //Quit
   public static PhoneBook book = new PhoneBook();
   public static BinarySearchTree bst =book.getPhonebook();

   public static void Add(Person p)
   {
       bst.insert(p);
   }
   public static void Delete(String name)
   {
       bst.deleteKey(name);
   }
  
   public static String Find(String name)
   {
       Person p = bst.find(name);
       return p.getPhoneNo();
      
   }
   public static void Change(String name,Stringnumber)
   {
       Person p = bst.find(name);
      
       p.setPhoneNo(number);
   }
  
   public static void Quit() throws IOException
   {
       FileOutputStream fout=newFileOutputStream(“D:WS17.2SamplesrcsamplePhonebook.txt”);
       bst.QuitApp(fout);
   }
  
  
   public static void main(String []args) throwsIOException
   {

       Scanner sc = newScanner(System.in);
  
       while(true)
       {
          System.out.println(“Please choose a option :”);
          System.out.println(“1.Add a Person”);
          System.out.println(“1.Delete a Person”);
          System.out.println(“3.Find a Person”);
          System.out.println(“4.Change number for Person”);
          System.out.println(“5.Quit”);
          
           int choice =sc.nextInt();
          
           switch (choice){
           case 1:
           {
              System.out.println(“Please enter person name andphone number to add”);
              String name = sc.next();
              String number =sc.next();
             
              Person p = new Person(name, number);
              Add(p);
              break;
           }
           case 2:
           {
              System.out.println(“Please enter a person nameto delete”);
              String name = sc.next();
             
              Delete(name);
              break;
           }
           case 3:
           {
              System.out.println(“Please enter a person nameto find”);
              String name = sc.next();
              System.out.println(“Number of “+ name +” is :”+Find(name));
              break;
           }
           case 4:
           {
              System.out.println(“Please enter a person nameand new phone number to update”);
              String name = sc.next();
              String number = sc.next();
             
              Change(name,number);
           }
           case 5:
           {
              Quit();
              break;
           }

          default:
              System.out.println(“Please enter a validchoice”);
              break;
           }
          
           if(choice ==5)
              break;
       }
      
      
   }

}

Expert Answer


Answer to In Java please. Write a program that provides a way for you to store and retrieve telephone numbers. It has three classe… . . .

OR


Leave a Reply

Your email address will not be published. Required fields are marked *