admin_Add_newEmp.java Source code

Java tutorial

Introduction

Here is the source code for admin_Add_newEmp.java

Source

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
import javax.swing.JOptionPane;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author user
 */
public class admin_Add_newEmp extends javax.swing.JFrame {

    String name, lname, userid, password;
    //long mobno;
    char mobno[] = new char[10];

    /**
     * Creates new form admin_Add_newEmp
     */

    public admin_Add_newEmp() {
        initComponents();

        jLabel2.setVisible(false);
        jLabel3.setVisible(false);
        userid_field.setVisible(false);
        password_field.setVisible(false);
        Add_button.setVisible(false);

    }

    public boolean checkNumbers() {
        String regex = "[0-9]+";
        String contact = mobno_field.getText();
        if ((contact.matches(regex)) && (contact.length() == 10))
            return true;
        return false;
    }

    public boolean checkLetters() {
        String firstName = name_field.getText();
        String letters = "[a-zA-Z]*";
        if ((firstName.matches(letters)))
            return true;
        return false;
    }

    public boolean checkLetters1() {
        String firstName = lname_field.getText();
        String letters = "[a-zA-Z]*";
        if ((firstName.matches(letters)))
            return true;
        return false;
    }

    public boolean fieldnotempty() {
        if ((!name_field.getText().equals("")) && (!lname_field.getText().equals("")))
            return true;
        return false;
    }

    /**
     * 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();
        name_field = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        userid_field = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        password_field = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        mobno_field = new javax.swing.JTextField();
        Add_button = new javax.swing.JButton();
        generate = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        lname_field = new javax.swing.JTextField();
        cancel_button = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();

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

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText("First Name : ");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 110, 110, 23));

        name_field.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true));
        name_field.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                name_fieldActionPerformed(evt);
            }
        });
        getContentPane().add(name_field, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 110, 150, 29));

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel2.setText("User Id :");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 330, 70, 28));
        getContentPane().add(userid_field, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 330, 150, 28));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel3.setText("Password: ");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 340, -1, -1));

        password_field.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                password_fieldActionPerformed(evt);
            }
        });
        getContentPane().add(password_field, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 330, 150, 29));

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel4.setText("Mobile No.:");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 200, 110, -1));

        mobno_field.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true));
        getContentPane().add(mobno_field, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 190, 150, 28));

        Add_button.setText("ADD");
        Add_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Add_buttonActionPerformed(evt);
            }
        });
        getContentPane().add(Add_button, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 420, 100, 30));

        generate.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        generate.setText("Generate ID Password");
        generate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateActionPerformed(evt);
            }
        });
        getContentPane().add(generate, new org.netbeans.lib.awtextra.AbsoluteConstraints(180, 260, 200, 30));

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel5.setText("Last Name :");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 150, 110, 30));

        lname_field.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true));
        getContentPane().add(lname_field, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 150, 150, 30));

        cancel_button.setText("Cancel");
        cancel_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancel_buttonActionPerformed(evt);
            }
        });
        getContentPane().add(cancel_button, new org.netbeans.lib.awtextra.AbsoluteConstraints(390, 420, 100, 30));

        jLabel6.setIcon(new javax.swing.ImageIcon(
                "C:\\Users\\user\\Desktop\\DBMS Project\\Parking_Project\\Images\\oak-wood.jpg")); // NOI18N
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel7.setText("Parking Management System");
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 30, -1, -1));

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

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

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

    private void Add_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Add_buttonActionPerformed
        // TODO add your handling code here:

        try {

            MongoClient mc = new MongoClient("localhost", 27017);
            DB db = mc.getDB("parking_system");
            DBCollection collection = db.getCollection("employee_info");

            name = name_field.getText();
            lname = lname_field.getText();
            mobno = mobno_field.getText().toCharArray();

            //userid = userid_field.getText();
            //password = password_field.getText();
            //mobno = Integer.parseInt(mobno_field.getText());
            //mobno = mobno_field.getText();
            String mobnoStr = new String(mobno);

            BasicDBObject doc = new BasicDBObject("_id", userid).append("name", name).append("lname", lname)
                    .append("password", password).append("mobileno", mobnoStr);

            collection.insert(doc);

            JOptionPane.showMessageDialog(null, "Added!");

            setVisible(false);
            new Admin_rights().setVisible(true);

        } catch (Exception e) {

        }
    }//GEN-LAST:event_Add_buttonActionPerformed

    private void generateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateActionPerformed
        // TODO add your handling code here:
        if (fieldnotempty()) {
            if (checkLetters()) {
                if (checkLetters1()) {
                    if (checkNumbers()) {
                        jLabel2.setVisible(true);
                        jLabel3.setVisible(true);
                        userid_field.setVisible(true);
                        password_field.setVisible(true);
                        Add_button.setVisible(true);
                        name = name_field.getText();
                        lname = lname_field.getText();
                        mobno = mobno_field.getText().toCharArray();
                        userid = name + mobno[8] + mobno[9];
                        userid_field.setText(name + mobno[8] + mobno[9]);
                        password = lname + mobno[0] + mobno[1];
                        password_field.setText(lname + mobno[0] + mobno[1]);
                    } else {
                        JOptionPane.showMessageDialog(null, "Enter Valid Mobile No.!!!");

                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Enter Valid Last Name!!!");
                }
            } else {
                JOptionPane.showMessageDialog(null, "Enter Valid First Name.!!!");
            }
        } else {
            JOptionPane.showMessageDialog(null, "Empty Field Not Allowed!!!");
        }
    }//GEN-LAST:event_generateActionPerformed

    private void cancel_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancel_buttonActionPerformed
        // TODO add your handling code here:
        setVisible(false);
        new Admin_rights().setVisible(true);
    }//GEN-LAST:event_cancel_buttonActionPerformed

    /**
     * @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(admin_Add_newEmp.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(admin_Add_newEmp.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(admin_Add_newEmp.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(admin_Add_newEmp.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new admin_Add_newEmp().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Add_button;
    private javax.swing.JButton cancel_button;
    private javax.swing.JButton generate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JTextField lname_field;
    private javax.swing.JTextField mobno_field;
    private javax.swing.JTextField name_field;
    private javax.swing.JTextField password_field;
    private javax.swing.JTextField userid_field;
    // End of variables declaration//GEN-END:variables
}