
import java.io.*;
import java.util.*;

public class test {

public static void main(String[] args)throws Exception {
	
Scanner input = new Scanner(System.in);
       
                
      // telList t = new telList();
//...........................................................................
       boolean quit = false;
 do { 
 	       int menu = 0;
 	     System.out.println("******************************");
         System.out.println("Telephone Directory");
         System.out.println();
         System.out.println("1. Accept Data");
         System.out.println("2. Search");
         System.out.println("3. List of all persons");
         System.out.println("4. Exit");
         System.out.println("5. update");
         
        
         System.out.print("Please enter your choice: ");
         menu = input.nextInt();
         System.out.println();
//...........................................................................
 switch (menu) {
 case 1:
          System.out.print("Enter Name: ");
          String name = input.next();
          System.out.print("Enter Address: ");
          String address = input.next();
          System.out.print("Enter Phone No: ");
          int no = input.nextInt();
          FileWriter fw = new FileWriter(new File("directory.txt"), true);
          BufferedWriter out = new BufferedWriter(fw);
          out.write(name + " " + address + " " + no);
          out.write(name + " " + address + " " + no);
          out.newLine();
          out.close();
         System.out.println( name + " " + address + " " + no);
      		  
    break;
//...........................................................................
   	case 2://Search
   	     System.out.print("Enter name to search information: ");
         String n = input.next();
         File f = new File("directory.txt");
     try {
          BufferedReader freader = new BufferedReader(new FileReader(f));
          String s;
          while ((s = freader.readLine()) != null) {
          String[] st = s.split(" ");
          String nm = st[0];
          String add = st[1];
          String phoneNo = st[2];
          if (n.equals(nm)) {
          System.out.println("***********Information**************");
          System.out.println("Address : " + add);
          System.out.println("PhoneNo : " + phoneNo);
         }
         }
         freader.close();
         } 
    catch (Exception e) {
                            }
        break;
//...........................................................................   
   case 3://list
 	 FileInputStream fis = new FileInputStream(new File("directory.txt"));
     
     try {
     	DataInputStream dis = new DataInputStream(fis);
     BufferedReader reader = new BufferedReader( new InputStreamReader(dis));
     String st;
   ArrayList al = new ArrayList();
   while ((st = reader.readLine()) != null) {
      al.add(st);
     }
     Iterator itr;
     for (itr = al.iterator(); itr.hasNext();) {
     String str = itr.next().toString();
     String[] splitSt = str.split(" ");
     String na = "", ada = "", ph = "";
     for (int i = 0; i < splitSt.length; i++) {
     na = splitSt[0];
     ada = splitSt[1];
     ph = splitSt[2];
                                       }
    System.out.println(na + " " + ada + " " + ph);
      }
    
         reader.close();
         } 
    catch (Exception e) {
                            }

       break;
//...........................................................................
   case 4 ://exit
          quit = true;
   break;
//...........................................................................
  case 5 ://update 
  	 System.out.print("Enter name to update information: ");
         String a = input.next();
         File fi = new File("directory.txt");
     try {
          BufferedReader freader1 = new BufferedReader(new FileReader(fi));
          String b;
          
          //while ((b = freader1.readLine()) != null) {
          String[] str = a.split(" ");
          String nm = str[0];
        //  String add = str[1];
        //  String phoneNo = str[2];
          if (a.equals(nm)){
          System.out.print("Enter The New Address: ");
          String address1 = input.next();
          System.out.print("Enter The New Phone No: ");
          int no1 = input.nextInt();
          address = address1;
          no = no1;
          FileWriter fwr = new FileWriter(new File("directory.txt"), true);
          BufferedWriter out1 = new BufferedWriter(fwr);
          out1.write( address1 + " " + no1);
          out1.newLine();
          out1.close();
            System.out.println( address + " " + no);
          
        //  }
          }
         freader1.close();
         } 
    catch (Exception e) {
                            }
  break;
   default:
        System.out.println("Invalid Entry!");
        }
        } while (!quit);
}
}