/*
      Chapter 9:     PhoneBook
      Programmer:    Brad Shedd
      Date:          September 09, 2006
      Filename:      PhoneBook.java
      Purpose:       This program maintains entries for a phone  book.
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class PhoneBook extends JFrame implements ActionListener
{ 
private JTextField jtfName, jtfPhone;
  
private JButton jbtAdd, jbtDelete, jbtUpdate, jbtFind;
  
private ArrayList nameList, phoneList;

   
public static void main(String[] args)
    {
     
int width = 200;
     
int height = 130;
        PhoneBook frame =
new PhoneBook();
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(width, height);
      frame.centerOnScreen(width, height);
      frame.setVisible(
true);
    }

   
public PhoneBook()
    {
        nameList =
new ArrayList(20); //sets up for 20 entries
        phoneList = new ArrayList(20); //sets up for 20 entries

        setTitle("Phone Book");

        JPanel p =
new JPanel();
        p.setLayout(
new FlowLayout());
        p.add(
new JLabel("Name:   "));
       p.add(jtfName =
new JTextField(20));
       p.add(
new JLabel("Phone:   "));
       p.add(jtfPhone =
new JTextField(20));

       p.add(jbtAdd =
new JButton("Add"));
        p.add(jbtDelete =
new JButton("Delete"));
        p.add(jbtUpdate =
new JButton("Update"));
        p.add(jbtFind =
new JButton("Find"));

        jbtAdd.addActionListener(
this);
        jbtDelete.addActionListener(
this);
        jbtUpdate.addActionListener(
this);
        jbtFind.addActionListener(
this);

        getContentPane().setLayout(
new BorderLayout(10,10));
        getContentPane().add(p,BorderLayout.CENTER);

        addWindowListener(
new WindowAdapter() {
        
public void windowClosing(WindowEvent e) { System.exit(0); }
        });
    }

  
public void centerOnScreen(int width, int height)
   {
    
int top, left, x, y;

    
// Get the screen dimension
     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

    
// Determine the location for the top left corner of the frame
     x = (screenSize.width - width)/2;
     y = (screenSize.height - height)/2;
     top = (x < 0) ? 0 : x;
     left = (y < 0) ? 0 : y;

    
// Set the frame to the specified location
     this.setLocation(top, left);
   }

  
public void actionPerformed(ActionEvent e)
    {
      String nameEntered, phoneEntered;
     
int index;

      nameEntered = jtfName.getText();
      phoneEntered= jtfPhone.getText();

     
if(nameEntered.length() > 0)
      {
        
if(e.getSource() == jbtAdd)      // user clicked Add
            {
              
if(phoneEntered.length() > 0)
               {
                 
try
                  {
                     addEntry(nameEntered, phoneEntered);
                     JOptionPane.showMessageDialog(
this,"Entry added to phone book.", "Successful!", JOptionPane.INFORMATION_MESSAGE);

                        jtfName.setText(
"");
                        jtfPhone.setText(
"");
                        jtfName.requestFocus();
               }
                 
catch (Exception ex)
               {
                         JOptionPane.showMessageDialog(
this, ex.getMessage(), "Add failed.", JOptionPane.ERROR_MESSAGE);

               }
            }
           
else
               {
               JOptionPane.showMessageDialog(
this,"Please enter a phone number.", "Invalid entry.Try again.", JOptionPane.INFORMATION_MESSAGE);
               jtfPhone.requestFocus();
            }
          }

           
else if  (e.getSource() == jbtUpdate)     // user clicked Update
            {
              
try
                  {
                     updateEntry(nameEntered, phoneEntered);
                     JOptionPane.showMessageDialog(
this,"Entry updated in phone book.", "Successful!", JOptionPane.INFORMATION_MESSAGE);

                           jtfName.setText(
"");
                        jtfPhone.setText(
"");
                        jtfName.requestFocus();
               }
              
catch (Exception ex)
            {
                      JOptionPane.showMessageDialog(
this, ex.getMessage(), "Update failed.", JOptionPane.ERROR_MESSAGE);

            }
         }

           
else if  (e.getSource() == jbtDelete)     // user clicked Delete
            {
              
try
               {
                  deleteEntry(nameEntered);
                  JOptionPane.showMessageDialog(
this,"Entry deleted in phone book.", "Successful!", JOptionPane.INFORMATION_MESSAGE);

                     jtfName.setText(
"");
                  jtfPhone.setText(
"");
                  jtfName.requestFocus();
            }
              
catch (Exception ex)
            {
                      JOptionPane.showMessageDialog(
this, ex.getMessage(), "Delete failed.", JOptionPane.ERROR_MESSAGE);

                   jtfName.requestFocus();
            }
         }
        
else if  (e.getSource() == jbtFind)    // user clicked Find
            {
             
try
              {
               index = findEntry(nameEntered);
               jtfName.setText((String)nameList.get(index));
               jtfPhone.setText((String)phoneList.get(index));
               jtfName.requestFocus();
           }
          
catch (Exception ex)
           {
                   JOptionPane.showMessageDialog(
this, ex.getMessage(), "Find failed.", JOptionPane.ERROR_MESSAGE);

               jtfName.requestFocus();
           }
         }
      }
     
else
      {
         JOptionPane.showMessageDialog(
this,"Please enter a name.", "Invalid entry. Try again.", JOptionPane.ERROR_MESSAGE);

         jtfName.requestFocus();
      }
   }

  
private void addEntry(String name, String phone) throws Exception
   {
     
if(!nameList.contains(name))
      {
         name.trim();
         phone.trim();
         nameList.add(name);
         phoneList.add(phone);
      }
     
else
         throw new Exception("Name already in phone book.");
   }

    
private void updateEntry(String name, String phone) throws Exception
       {
              
int index = 0;

            name.trim();
            phone.trim();
            index = nameList.indexOf(name);
           
if (index < 0)
              
throw new Exception("Name not in phone book.");
        
else
         {
            nameList.set(index, name);
            phoneList.set(index, phone);
         }
      }
       
private void deleteEntry(String name)throws Exception
        {
        
int index = 0;
         index = nameList.indexOf(name.trim());
        
if(index < 0)
           
throw new Exception("Name not in phone book.");
       
else
        {
         nameList.remove(index);
         phoneList.remove(index);
        }
    }
   
private int findEntry(String name)throws Exception
        {
        
int index = 0;
         index = nameList.indexOf(name.trim());
        
if(index < 0)
           
throw new Exception("Name not in phone book.");

           
return index;
        }
   }

Homepage