Feedjit

Articles for you

Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Wednesday, June 5, 2013

A Form in Java to Check for the 2 numbers. Either Greater Equal or Less for Java Beginners. Advance Computer Programming Subject

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package assignemnt1;

import javax.swing.JOptionPane;

/**
 *
 * @author Saqib Khan
 */
public class lessgreater extends javax.swing.JFrame {

    /**
     * Creates new form lessgreater
     */
    public lessgreater() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        jLabel1.setText("jLabel1");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        getContentPane().add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 20, 117, -1));
        getContentPane().add(jTextField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 50, 117, -1));

        jLabel2.setText("Second Nmber");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 50, -1, -1));
        jLabel2.getAccessibleContext().setAccessibleName("");

        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 150, -1, -1));

        jLabel4.setText("First Number");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 20, -1, -1));

        jButton1.setText("Show Result");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 90, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
       
     
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
         int num1=Integer.parseInt( jTextField1.getText());
         int num2=Integer.parseInt( jTextField2.getText());
       if(num1>num2)
       {
          JOptionPane.showMessageDialog(null, "The "+num2+" is less than "+num1);
       }
       else
       {
          JOptionPane.showMessageDialog(null, "The "+num1+" is less than "+num2);
       }
       
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(lessgreater.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(lessgreater.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(lessgreater.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(lessgreater.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
         
            @Override
            public void run() {
                new lessgreater().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
}

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