import java.util.*;
import java.io.*;
public class PhoneBook
{
    Vector  entries;
    public PhoneBook()
    {
       entries = new Vector();
    }

    public void add(PhoneEntry pe)
    {
        entries.add(pe);
    }

    public boolean delete(String name)
   {
      Iterator  itr;

      itr = entries.iterator();
      while (  itr.hasNext())
      {
         PhoneEntry pe = (PhoneEntry) itr.next();
         if ( pe.name.equals(name) )
         {
             itr.remove();
             return  true;
         }
      }
      return false;
     } 

    public PhoneEntry  getPhoneEntry(String name)
    {
      Iterator  itr;

      itr = entries.iterator();
      while (  itr.hasNext())
      {
         PhoneEntry pe = (PhoneEntry) itr.next();
         if ( pe.name.equals(name) )
         {
            return  pe;
         }

     }
     return null;


    }     
 
     public void writeToDisk()
     {
      try
      {
       FileWriter fw = new FileWriter("phones.dat");

      Iterator itr = entries.iterator();
     
      while ( itr.hasNext())
      {
       PhoneEntry pe =(PhoneEntry) itr.next();           
       fw.write( pe.toString() + "\n");
      }
      fw.close();
     }
     catch(Exception ex)
     {  System.out.println(ex); }
    } // end of writToDisk



    public void readFromDisk()
    {
    String line;;
    String name,phoneno;
    char ptype;
    int p1,p2;
    try
    {
     FileReader fr = new FileReader("phones.dat");
     BufferedReader br  = new BufferedReader(fr);

     while (  (line = br.readLine() ) != null)
     {
     if ( line.trim().length() == 0 ) continue;

     p1 = line.indexOf(":");
     name = line.substring(0,p1);
     p2 = line.indexOf(":",p1+1);
     phoneno =  line.substring(p1+1,p2);
     ptype = line.charAt(p2+1);
     PhoneEntry pe = new PhoneEntry(name,phoneno,ptype);
     entries.add(pe);
    } // end of while
    br.close();
   }
   catch(Exception ex)
   {  System.out.println(ex); }
  
  } // end of function 
        


  public String getAllEntries()
  {String st= "";
    Iterator itr = entries.iterator();
    while ( itr.hasNext())
       st = st + itr.next().toString() + "\n";
    return st;
  }



  public String getSelectedEntries(String pat,boolean ignorecase)
  {
   String st= "",name;
   PhoneEntry pe;

    if (ignorecase)
          pat= pat.toUpperCase();

    Iterator itr = entries.iterator();
    while (itr.hasNext())
    {
     
       pe = (PhoneEntry) itr.next();
       if ( ignorecase)
             name = pe.name.toUpperCase();
       else
             name= pe.name;
        
       if ( name.indexOf(pat) >= 0 )
            st = st + pe.toString() + "\n";
    }
    return st;
  }



  public Vector getNames()
  {
    Vector v = new Vector();

    Iterator itr = entries.iterator();
    PhoneEntry pe;
    while ( itr.hasNext())
    {
       pe = (PhoneEntry) itr.next(); 
       v.add( pe.name);
    }

    return v;

  }
     
  public static void main(String args[])
  {
      PhoneBook pb = new PhoneBook();

      pb.readFromDisk();
      System.out.println( pb.getAllEntries());
      pb.delete("Jason");
      System.out.println(pb.getAllEntries());
      PhoneEntry pe = new PhoneEntry("Peter","838383",'i');
      pb.add(pe);
      System.out.println(pb.getAllEntries());
      pb.writeToDisk();

    }


 }   // end of class