c++
sql
phyton
javascript
html

Recommended Answers

What are you trying to say???

Jump to Post

All 2 Replies

What are you trying to say???

import java.awt.Desktop;
import java.io.*;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Scanner;
import java.util.Set;     

 class PhoneBookEntry implements Serializable{

 private String Name;
 private String Phone;


 PhoneBookEntry(String name,String phone){
  this.Name=name;
  this.Phone=phone; 
 }
 public String getName(){
  return Name;
 }
 public String getPhone(){
  return Phone;
 }
 public void printInfo(){
  System.out.println("Name:"+Name+", Phone:"+Phone);
     }
}
 public class PhoneBook1{
 static Hashtable<String,PhoneBookEntry> phonebook; 
   public static void main(String[] args){ 
    int ch;
    char con='c';
    Scanner sc=new Scanner(System.in);     
    while(con=='c'){
     showMenu();
     System.out.println("Make a Choice:");
     ch=sc.nextInt();    
     switch(ch){

  case 1:viewAll();break;
  case 2:addToPhoneBook();break;
  case 3:deleteFromPhonebook();break;
  case 4:searchByName();break;
  case 5:System.exit(0);break;
  default: System.out.println("Wrong choice");
  }

 try{
  InputStreamReader isr=new InputStreamReader(System.in);
  System.out.println("Press c to continue:");
  con=(char)isr.read();
 }catch(IOException ie){}


}


   }
      public static void viewAll(){

if(phonebook!=null){

 for(Enumeration<String> e=phonebook.keys(); e.hasMoreElements();){
  PhoneBookEntry entry=phonebook.get(e.nextElement());
  entry.printInfo();
 }

}

   }
     public static void addToPhoneBook(){
       if(phonebook==null) phonebook=new Hashtable<String,PhoneBookEntry>();
    try{
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter a name:");
    String name=br.readLine();    
    System.out.println("Enter a phone number:");
    String phonenumber=br.readLine();   
    PhoneBookEntry st=new PhoneBookEntry(name,phonenumber);
    phonebook.put(name,st);      
     }catch(IOException e){}
   }

public static void deleteFromPhonebook(){
if(phonebook!=null){
 int si=phonebook.size();     
 try{
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 System.out.println("Name:");
 String key=br.readLine();
 phonebook.remove(key); 
 if(phonebook.size()<si){ 

  System.out.println("The entry has been deleted.");
 }
 else
  System.out.println("Invalid name");
 }catch(IOException ie){}


}
  }


public static void searchByName(){
if(phonebook!=null){
 try{
 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
 System.out.println("Search by name:");
 String key=br.readLine();
 PhoneBookEntry cu=phonebook.get(key);
 if(cu!=null)
  cu.printInfo();

else
  System.out.println("Not found");
}catch(IOException ie){}

         }
   }


 public static void showMenu(){
    System.out.println("1. View all Entries");
    System.out.println("2. Add Entry");
    System.out.println("3. Remove an Entry");
    System.out.println("4. Find an Entry");    
    System.out.println("5. End program");
     }
   }

i have trouble on "View Entry" "Delete Entry" " Search by Name"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.