Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 22nd, 2005, 1:08 PM   #1
ellomoto
Newbie
 
Join Date: Nov 2004
Posts: 4
Rep Power: 0 ellomoto is on a distinguished road
Searching a collection

Hi all,
I am having trouble figuring out how to search a collection. The collection holds three strings (name, address, telephonenum) i would like to be able to search using a name as the search criteria.

public class estateGUI extends javax.swing.JFrame {
    
        public estateGUI() {        
        initComponents();    }    
        public String name, address, telephonenum, find;
    
        private void initComponents() {
        addclient = new javax.swing.JButton();
        findclient = new javax.swing.JButton();
        logo = new javax.swing.JLabel();
        cleartext = new javax.swing.JButton();
        nametextfield = new javax.swing.JTextField();
        addresstextfield = new javax.swing.JTextField();
        numtextfield = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        findnametextfield = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        displaydetails = new javax.swing.JTextPane();

        getContentPane().setLayout(new     org.netbeans.lib.awtextra.AbsoluteLayout());

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("UWE Estates");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setMaximizedBounds(new java.awt.Rectangle(500, 500, 400, 420));
        setName("MainFrame");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });
        addclient.setText("Add Client");
        addclient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addclientActionPerformed(evt);
            }
        });
        getContentPane().add(addclient, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 210, 130, -1));        findclient.setText("FIND");
        findclient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findclientActionPerformed(evt);
            }
        });
        getContentPane().add(findclient, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 300, 90, -1));
        logo.setBackground(new java.awt.Color(255, 255, 255));
        logo.setFont(new java.awt.Font("Arial", 1, 24));
        logo.setForeground(new java.awt.Color(51, 51, 255));
        logo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        logo.setText("UWE Estates");
        getContentPane().add(logo, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 20, 180, 30));
        cleartext.setText("Clear Text");
        cleartext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cleartextActionPerformed(evt);
            }
        });
        getContentPane().add(cleartext, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 210, 130, -1));
        nametextfield.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nametextfieldActionPerformed(evt);
            }
        });
        getContentPane().add(nametextfield, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 80, 130, -1));
        addresstextfield.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addresstextfieldActionPerformed(evt);
            }
        });
        getContentPane().add(addresstextfield, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 130, 130, -1));
        numtextfield.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                numtextfieldActionPerformed(evt);
            }
        });
        getContentPane().add(numtextfield, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 180, 130, -1));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Add Name");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 60, 130, -1));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Add Address");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 110, 130, -1));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Add Telephone Number");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 160, 160, -1));

        findnametextfield.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findnametextfieldActionPerformed(evt);
            }
        });
        getContentPane().add(findnametextfield, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 270, 260, -1));

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("Input Name and Click Find");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 250, 160, -1));

        getContentPane().add(displaydetails, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 330, 260, 90));

        pack();
    }

    private void findclientActionPerformed(java.awt.event.ActionEvent evt) {
       
        find = findnametextfield.getText();
        
        branch newbranch = new branch();         
        newbranch.find(find);
        
    }

    private void findnametextfieldActionPerformed(java.awt.event.ActionEvent evt) {
                
    }

    private void numtextfieldActionPerformed(java.awt.event.ActionEvent evt) {
        
    }

    private void addresstextfieldActionPerformed(java.awt.event.ActionEvent evt) {
    
    }
    
    
    private void cleartextActionPerformed(java.awt.event.ActionEvent evt) {
    clearFields();        
    }

    private void addclientActionPerformed(java.awt.event.ActionEvent evt) {
       
        name = nametextfield.getText();         
        address = addresstextfield.getText();         
        telephonenum = numtextfield.getText();
        
        branch newbranch = new branch();         
        newbranch.add(name, address, telephonenum);    
    }  private void nametextfieldActionPerformed(java.awt.event.ActionEvent evt) {   
    }   
    private void exitForm(java.awt.event.WindowEvent evt) {
        System.exit(0);
    }    
    private void clearFields()
    {
        nametextfield.setText("");
        addresstextfield.setText(""); 
        numtextfield.setText(""); 
    }      public static void main(String args[]) 
    {
        new estateGUI().show();      
    }        
       private javax.swing.JButton addclient;
    private javax.swing.JTextField addresstextfield;
    private javax.swing.JButton cleartext;
    private javax.swing.JTextPane displaydetails;
    private javax.swing.JButton findclient;
    private javax.swing.JTextField findnametextfield;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel logo;
    private javax.swing.JTextField nametextfield;
    private javax.swing.JTextField numtextfield;  
}

Branch class that recieves input from estateGUI.
import javax.swing.*;
import java.awt.*;
import javax.swing.JOptionPane;
import java.awt.event.*;
public class branch {    
    public branch()     
    {}      
    
    public int customerID = 1;
    public int maxclients = 5;
    public int count;
    private int index = 0;
    public CustomerCollection clients = new CustomerCollection();
   
    public void add(String name, String address, String telephonenum)
    {           
        if (count < 2)
        {   
           count++;        
           clients.addClient(name,address,telephonenum);    
          
           JOptionPane.showMessageDialog(null, "Client Stored \n" +name+ "\n" 
           + address + "\n" + telephonenum);          
           
        }
        else 
        {
             JOptionPane.showMessageDialog(null, "Error. Maximum number of" +
             "clients exceeded");             
        }         
    }    
    public void amend()
    {}    
    public void delete()
    {}    
    public void find(String details)
    {        
       clients.findclient(details);        
    }
    
    public void print()
    {}
}

CustomerCollection class which builds the collection and has the method for add/find client etc:
import javax.swing.*;
import java.awt.*;
import javax.swing.JOptionPane;
import java.awt.event.*;
public class CustomerCollection
{   
   public Client[] collection;    
   private int arraySize = 5;
   private int index = 0;
  
   public CustomerCollection ()
   {
      collection = new Client[arraySize];      
   } 
   
   public void addClient (String name, String address, 
   String telephonenum)
   {      
        if(index == collection.length)
                    {           
             collection[index] = new Client (name,address,telephonenum);          
        }    index++;
   }
   
   public void findclient(String details)
   {       
   }

Does anyone have any ideas on how i can find a specific client in a collection.

Thanks
ellomoto is offline   Reply With Quote
Old Feb 24th, 2005, 12:05 AM   #2
ZenMasterJG
Hobbyist Programmer
 
ZenMasterJG's Avatar
 
Join Date: Nov 2004
Location: Boston, MA
Posts: 148
Rep Power: 4 ZenMasterJG is on a distinguished road
Send a message via AIM to ZenMasterJG
in your findclient method, try somthing like this:
public void findclient(String details)
{
   for(int x=0; x<arraySize; x++) {
      //I donno how your Client class is defined, but you'll need a method that 
      //returns the name
      if( (collection[x].getName()).equalsIgnoreCase(details)) {
           //Found it! do whatever you want here.
      }
   }
}

if you want that method to return some data found in there, i'd make sure you search the whole array and put the found entries on a new array, and return THAT, as names have a nasty habit of being non-unique, and just returning the first one you find could leave out a lot of data.

Last edited by ZenMasterJG; Feb 24th, 2005 at 12:05 AM. Reason: oops, missed a brace.
ZenMasterJG is online now   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:56 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC