2 Replies - 600 Views - Last Post: 20 December 2013 - 03:29 PM Rate Topic: -----

#1 helpp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-December 13

how to make searching button

Posted 18 December 2013 - 08:30 AM

this is my code..previously i have i button that can add data like name,no account,adress..and display it into the text area..now i want to add the search button that can search the name from the data i adding..any suggestion?
private void showActionPerformed(java.awt.event.ActionEvent evt) {                                     

    String name, address;
    int accNo, currMeter, pastMeter, Usage;
    double Amount = 0;
    
     accNo = Integer.parseInt(this.AccNo.getText());
     name = Name.getText();
     address = Address.getText();
     currMeter = Integer.parseInt(this.CM.getText());
     pastMeter = Integer.parseInt(this.PM.getText());
     
     Usage = currMeter - pastMeter;
      if(Usage<0)
     {
          Usage=0;
         JOptionPane.showMessageDialog(null,"Your output must be positive Integer number","Error Message",JOptionPane.ERROR_MESSAGE);
          
          
     }
      this.usage.setText(MessageFormat.format("{0} liter", Usage));
      
    double minAmount = 6.00;
    // First 20 liter 
    if ( Usage <= 20){
            Amount = Usage * 0.50;
    }
    // Next 21 to 35 liter 
    else if ( Usage <= 35)
    {
            Amount = (20 * 0.50) + ((Usage- 20)* 0.90);

    }
    else
    // 36 liter above 
    {
            Amount =  (20 * 0.50) + (15 * 0.90) + (Usage - 35) * 1.30;
    }
    if (Amount <6.00) {
         Amount = minAmount;
        
        JOptionPane.showMessageDialog(null,"Your Total Bill should not be less than 6 RM","Error Message",JOptionPane.ERROR_MESSAGE);
    }
    this.bill.setText(MessageFormat.format("RM {0}", Amount));
     
     
}                                    

private void btn_exitActionPerformed(java.awt.event.ActionEvent evt) {                                         
dispose();
}                                        

    private void SDActionPerformed(java.awt.event.ActionEvent evt) {                                   
        // TODO add your handling code here:
        
        String pn[]=Name.getText().split("\n");
        String Account[]=AccNo.getText().split("\n");
        String Addr[]=Address.getText().split("\n");
        String Amnt[]=bill.getText().split("\n");
        String PassMeter[]=PM.getText().toString().split("\n");
        
        String CurrentMeter[]=CM.getText().toString().split("\n");
        String Usg[]=usage.getText().toString().split("\n");
         for(int s=0;s<pn.length;s++)
         {
           //this.txtArea.append(String.valueOf(new StringBuilder().append("Account Number: ").append(pn[s]).append("\nName: ").append(Account[s]).append("\nAddress: ").append(Addr[s]).append("\nCurrent Meter Reading: ").append(CurrentMeter[s]).append("\nPast Meter Reading: ").append(PassMeter[s]).append("\nUsage: ").append(Usage[s]).append("\nAmount: RM").append(Amount[s]).toString()));
         this.txtArea.append(new StringBuilder().append("Name :").append(pn[s]).append("\n").append("Account No. :").append(Account[s]).append("\n").append("Address :").append(Addr[s]).append("\n").append("Past Meter").append(PassMeter[s]).append("\n").append("Current Meter :").append(CurrentMeter[s]).append("\n").append("Total Usage :").append(Usg[s]).append("\n").append("Total Bill :").append(Amnt[s].toString()).append("\n\n\n").toString());
         }
    }                                  

    private void AccNoKeyTyped(java.awt.event.KeyEvent evt) {                               
        // TODO add your handling code here:
        char c= evt.getKeyChar();
        if(!(Character.isDigit(c)|| (c==KeyEvent.VK_BACK_SPACE) ||(c==KeyEvent.VK_DELETE) ))
            
        {
            evt.consume();
        }
    }                              

    private void NameKeyTyped(java.awt.event.KeyEvent evt) {                              
        // TODO add your handling code here:
       /* int i= evt.getKeyChar();
        if(!(Character.isAlphabetic(i) || (i==KeyEvent.VK_BACK_SPACE) ||(i==KeyEvent.VK_DELETE) ))
        {
            evt.consume();
        }*/
            
        
    }                             

    private void CMKeyTyped(java.awt.event.KeyEvent evt) {                            
        // TODO add your handling code here:
        
        char c= evt.getKeyChar();
        if(!(Character.isDigit(c)|| (c==KeyEvent.VK_BACK_SPACE) ||(c==KeyEvent.VK_DELETE) ))
            
        {
            evt.consume();
        }
    }                           

    private void PMKeyTyped(java.awt.event.KeyEvent evt) {                            
        // TODO add your handling code here:
        char c= evt.getKeyChar();
        if(!(Character.isDigit(c)|| (c==KeyEvent.VK_BACK_SPACE) ||(c==KeyEvent.VK_DELETE) ))
            
        {
            evt.consume();
        }
    }                


Is This A Good Question/Topic? 0
  • +

Replies To: how to make searching button

#2 ahlmo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 13-November 09

Re: how to make searching button

Posted 18 December 2013 - 01:35 PM

I would personally put the data into an ArrayList and when the Search button is pressed then run a for loop through the arraylist and use List.contains on the entered name.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,257
  • Joined: 27-December 08

Re: how to make searching button

Posted 20 December 2013 - 03:29 PM

View Postahlmo, on 18 December 2013 - 03:35 PM, said:

I would personally put the data into an ArrayList and when the Search button is pressed then run a for loop through the arraylist and use List.contains on the entered name.

If the data is designed in a proper OO manner, the latter part won't work. Part of the model should include a Person class (or Resident class) with the appropriate fields such as name, address, etc. One would have to loop through the List and check the name on each Person object, whereas List.contains() would look for a String.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1