Feedjit

Articles for you

Wednesday, June 5, 2013

Creating/Adding/Updating/Deleting/Searching of a contact record. Address Book In Java. File Oriented/Array List

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 PersonInfo.java
*/
package addressbook;
import java.io.*;
import java.util.*;
import java.nio.Buffer;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
 *
 * @author Saqib Khan
 */
public class PersonInfo
{
  public static ArrayList al=new ArrayList();
   public  String name=null;
   public  String Address=null;
    public String Phone=null;
    public PersonInfo(){}
    public PersonInfo(String n,String add,String ph){
    name=n;
    Address=add;
    Phone=ph;
    }
    public void display()
    {
     
       if(!al.isEmpty())
       {
           JOptionPane.showMessageDialog(null, "The size of Collection is: " + al.size());
        for(int i=0;i<al.size();i++)
        {
        PersonInfo result=(PersonInfo)al.get(i);
        JOptionPane.showMessageDialog(null, "The Name of Person is: " + result.name.toUpperCase());
        JOptionPane.showMessageDialog(null, "The Address of Person is: " + result.Address.toUpperCase());
        JOptionPane.showMessageDialog(null, "The Contact Numer of Person is: " + result.Phone.toUpperCase());
        }
       }
       else
       {
           JOptionPane.showMessageDialog(null, "The Collection is Empty ");
       }
    }
    public void add(PersonInfo p)
    {
        al.add(p);     
       
    }
    public void search()
    {
        boolean flag=false;
        if(!al.isEmpty())
        {
            String nam=JOptionPane.showInputDialog(null, "Enter the name of Person");
        for(int i=0;i<al.size();i++)
        {
            PersonInfo result=(PersonInfo)al.get(i);
            if(  result.name.equals(nam))
                {
                    JOptionPane.showMessageDialog(null, "The Name of Person is: " + result.name.toUpperCase());
                    JOptionPane.showMessageDialog(null, "The Address of Person is: " + result.Address.toUpperCase());
                    JOptionPane.showMessageDialog(null, "The Contact Numer of Person is: " + result.Phone.toUpperCase());
                    flag=true;
                }
       }
        if(flag==false)
        {
            JOptionPane.showMessageDialog(null, "There is no record of: "+nam.toUpperCase() );
        }
       }
         else
       {
           JOptionPane.showMessageDialog(null, "The Collection is Empty ");
       }
    }
    public void delete()
         {
             boolean flag= false;
             if(!al.isEmpty())
             {
             String nam=JOptionPane.showInputDialog(null, "Enter the name of Person");
             for(int i=0;i<al.size();i++)
             {
                 PersonInfo result=(PersonInfo)al.get(i);
                 if(result.name.equals(nam))
                 {
                     String temp=result.name;
                     al.remove(i);
                     JOptionPane.showMessageDialog(null, "The Information is deleted of: " + temp.toUpperCase());
                     flag=true;
                 }
                
             }
             if(flag==false)
                 {
                     JOptionPane.showMessageDialog(null, "The Record of: " +nam.toUpperCase()+ " is not saved");
                 }
             }
              else
                {
                        JOptionPane.showMessageDialog(null, "The Collection is Empty ");
                }
         }
    public void ReadFromFile() throws IOException
    {
        FileReader fr = new FileReader("E:/hami.txt");
        BufferedReader br = new BufferedReader(fr);
      String s = br.readLine();      
     while(s!=null)
     {
       
         PersonInfo pi=new PersonInfo();
       String[] tokens =s.split(",");
        pi.name=tokens[0];
        pi.Phone=tokens[1];
        pi.Address=tokens[2];
        al.add(pi);
        s = br.readLine();
        
     } 
    }
    public void SaveintoFile() throws IOException
    {
     FileWriter filew= new FileWriter("E:/hami.txt");
    // filew.write("");
     BufferedWriter fw = new BufferedWriter(new FileWriter("E:/hami.txt", true));
   
     for(int i=0;i<al.size();i++)
       {
       PersonInfo p=(PersonInfo)al.get(i);
       String temp=p.name;
       temp+=",";
       temp+=p.Address;
       temp+=",";
       temp+=p.Phone;
    
          fw.write(temp);
          fw.newLine();
          
       }
      
       
       fw.close();
    
    }
}
//////////////////////////////////////////
Address Book.java
/////////////////////////////
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package addressbook;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JOptionPane;
import java.util.ArrayList;
/**
 *
 * @author Saqib Khan
 */
public class AddressBook
{
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException
    {
       
        PersonInfo pi= new PersonInfo();
       
       pi.ReadFromFile();
      
       while(true)
       {
        String num=JOptionPane.showInputDialog(null, "Enter Your Choice \n"
                                                    + "1 for Add \n"
                                                    + "2 for Display \n"
                                                    + "3 for Search \n"
                                                    + "4 for Delete \n"
                                                    + "5 to exit\n");
        int cho=Integer.parseInt(num);
        switch(cho)
        {
            case 1:
                String name=JOptionPane.showInputDialog(null, "Enter the name of Person");
                String add=JOptionPane.showInputDialog(null, "Enter the Address of: "+name.toUpperCase());
                String phne=JOptionPane.showInputDialog(null, "Enter the Contact Number of: "+name.toUpperCase());
                pi= new PersonInfo(name, add, phne);
                pi.add(pi);
                  
            break;
            case 2:
                pi.display();
                break;
            case 3:
                pi.search();
                break;
            case 4:
                pi.delete();
                break;    
            case 5:
             pi.SaveintoFile();
             System.exit(cho);
               
       }
       }   
    }
}
///////////////////////////////////////////////////

Read More

Articles for you