Veehicle_entry.java Source code

Java tutorial

Introduction

Here is the source code for Veehicle_entry.java

Source

/*
 * 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
 */
import java.util.*;
import java.text.SimpleDateFormat;
import com.mongodb.*;
import javax.swing.JOptionPane;

public class Veehicle_entry extends javax.swing.JFrame {

    /**
     * Creates new form Vehicle_entry
     */
    int validator = 0;
    String entry_time_str;
    long time_mills;
    int veh_count = 0;
    int token, exists = 0;

    //Validations---->
    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 checkPassingCode() {
        String regex1 = "[0-9]+";
        String passno = vehno_strt2.getText();
        if ((passno.matches(regex1)) && (passno.length() == 2))
            return true;
        return false;
    }

    public boolean checkVehno() {
        String regex2 = "[0-9]+";
        String vehno1 = vehno_strt4.getText();
        if ((vehno1.matches(regex2)) && (vehno1.length() <= 4))
            return true;
        return false;
    }

    //Validations Ends Here......

    public int validate(DBCollection coll, BasicDBObject dc) {

        if (checkLetters()) {
            if (checkNumbers()) {
                if (checkPassingCode()) {
                    if (checkVehno()) {
                        coll.insert(dc);
                        JOptionPane.showMessageDialog(null, "Token Generated !!!\nToken Number : " + token);
                        return 1;
                    } else {
                        JOptionPane.showMessageDialog(null, "Enter Valid Vehicle No");
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Enter Valid Passing Number");
                }
            } else {
                JOptionPane.showMessageDialog(null, "Enter Valid Mobile No");
            }
        } else {
            JOptionPane.showMessageDialog(null, "Enter Valid User Name");
        }
        return 0;
    }

    public Veehicle_entry(int count) {
        initComponents();

        veh_count = count;
        /*
        Time and Date From System--->
        */

        Calendar cal = Calendar.getInstance();
        SimpleDateFormat dt = new SimpleDateFormat("HH:mm:ss");

        //System.out.println(cal.getTime());

        entry_time_str = cal.getTime().toString();
        TimeDate.setText(entry_time_str);

        time_mills = System.currentTimeMillis();

    }

    /**
     * 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();
        mobno_field = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        Entry_button = new javax.swing.JButton();
        TimeDate = new javax.swing.JTextField();
        vehno_strt1 = new javax.swing.JComboBox();
        vehno_strt2 = new javax.swing.JTextField();
        vehno_strt3 = new javax.swing.JComboBox();
        vehno_strt4 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        cancel_button = new javax.swing.JButton();
        jLabel6 = 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("Name : ");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 90, 75, 28));

        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(300, 90, 149, 28));

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

        mobno_field.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true));
        mobno_field.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mobno_fieldActionPerformed(evt);
            }
        });
        getContentPane().add(mobno_field, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 140, 149, 29));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel3.setText("Mobile No.:");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 140, -1, 35));

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel4.setText("In Time : ");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 260, 80, 30));

        Entry_button.setText("Entry");
        Entry_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                Entry_buttonActionPerformed(evt);
            }
        });
        getContentPane().add(Entry_button, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 360, 100, 40));

        TimeDate.setEditable(false);
        TimeDate.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        TimeDate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TimeDateActionPerformed(evt);
            }
        });
        getContentPane().add(TimeDate, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 260, 180, 30));

        vehno_strt1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "MH", "IGJ", "AP", "KT", "PY" }));
        vehno_strt1.setBorder(null);
        vehno_strt1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vehno_strt1ActionPerformed(evt);
            }
        });
        getContentPane().add(vehno_strt1, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 200, -1, 30));

        vehno_strt2.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        getContentPane().add(vehno_strt2, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 200, 40, 30));

        vehno_strt3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "A", "B", "C", "D", "E", "F", "G",
                "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }));
        vehno_strt3.setBorder(null);
        getContentPane().add(vehno_strt3, new org.netbeans.lib.awtextra.AbsoluteConstraints(400, 200, -1, 30));

        vehno_strt4.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        vehno_strt4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                vehno_strt4ActionPerformed(evt);
            }
        });
        getContentPane().add(vehno_strt4, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 200, 60, 30));

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel5.setText("PARKING ENTRY MANAGEMENT");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 10, 400, 50));

        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(350, 360, 90, 40));

        jLabel6.setIcon(new javax.swing.ImageIcon(
                "C:\\Users\\user\\Desktop\\DBMS Project\\Parking_Project\\Images\\oak-wood.jpg")); // NOI18N
        jLabel6.setToolTipText("");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 610, -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 mobno_fieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mobno_fieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_mobno_fieldActionPerformed

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

        try {

            String name, vehno, mobno;

            Random rand = new Random();
            token = rand.nextInt(10000);

            name = Name_field.getText();

            mobno = mobno_field.getText();

            vehno = vehno_strt1.getSelectedItem().toString() + "-" + vehno_strt2.getText() + "-"
                    + vehno_strt3.getSelectedItem().toString() + "-" + vehno_strt4.getText();

            //System.out.println(vehno);

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

            DBCollection collection1 = db.getCollection("vehicle_count");
            DBCollection collection2 = db.getCollection("statistics");

            if (veh_count == 2) {

                vehno = vehno + "W2";

                BasicDBObject queryx = new BasicDBObject();
                BasicDBObject fieldx = new BasicDBObject();
                fieldx.put("vehno", 1);
                DBCursor cursorx = collection.find(queryx, fieldx);

                while (cursorx.hasNext()) {

                    BasicDBObject objx = (BasicDBObject) cursorx.next();

                    String temp;
                    temp = objx.getString("vehno");
                    if (temp.equals(vehno)) {
                        JOptionPane.showMessageDialog(null, "vehicle number already exits!\n Entry Rejected!");
                        exists = 1;
                        setVisible(false);
                        new Veehicle_entry(veh_count).setVisible(true);

                    }
                }

                if (exists == 0) {
                    int count_bike;

                    BasicDBObject doc = new BasicDBObject("_id", token).append("name", name)
                            .append("mobileno", mobno).append("vehno", vehno).append("intime", time_mills);

                    validator = validate(collection, doc);
                    if (validator == 1) {
                        //Updating vehicle count....
                        BasicDBObject query = new BasicDBObject();
                        BasicDBObject field = new BasicDBObject();
                        field.put("wheeler_2", 1);
                        DBCursor cursor = collection1.find(query, field);

                        BasicDBObject obj = (BasicDBObject) cursor.next();
                        count_bike = obj.getInt("wheeler_2");
                        System.out.println(count_bike);

                        BasicDBObject searchUpdate = new BasicDBObject();
                        searchUpdate.append("wheeler_2", count_bike);

                        BasicDBObject UpdateObject = new BasicDBObject();
                        UpdateObject.append("$set", new BasicDBObject("wheeler_2", count_bike + 1));

                        collection1.update(searchUpdate, UpdateObject);

                        //updating session...
                        int cnt_bike;
                        BasicDBObject query2 = new BasicDBObject();
                        BasicDBObject field2 = new BasicDBObject();
                        field2.put("2wheeler_count", 1);
                        DBCursor cursor2 = collection2.find(query2, field2);

                        BasicDBObject obj2 = (BasicDBObject) cursor2.next();
                        cnt_bike = obj2.getInt("2wheeler_count");
                        System.out.println(cnt_bike);

                        BasicDBObject searchUpdate2 = new BasicDBObject();
                        searchUpdate2.append("2wheeler_count", cnt_bike);

                        BasicDBObject UpdateObject2 = new BasicDBObject();
                        UpdateObject2.append("$set", new BasicDBObject("2wheeler_count", cnt_bike + 1));

                        collection2.update(searchUpdate2, UpdateObject2);

                        setVisible(false);
                        new Entry_OR_Exit().setVisible(true);
                    }
                }
            }

            else if (veh_count == 4) {

                vehno = vehno + "W4";

                BasicDBObject queryx = new BasicDBObject();
                BasicDBObject fieldx = new BasicDBObject();
                fieldx.put("vehno", 1);
                DBCursor cursorx = collection.find(queryx, fieldx);

                while (cursorx.hasNext()) {

                    BasicDBObject objx = (BasicDBObject) cursorx.next();

                    String temp;
                    temp = objx.getString("vehno");
                    if (temp.equals(vehno)) {
                        JOptionPane.showMessageDialog(null, "vehicle number already exits!\n Entry Rejected!");
                        exists = 1;
                        setVisible(false);
                        new Veehicle_entry(veh_count).setVisible(true);

                    }
                }

                if (exists == 0) {
                    int count_car;

                    BasicDBObject doc = new BasicDBObject("_id", token).append("name", name)
                            .append("mobileno", mobno).append("vehno", vehno).append("intime", time_mills);

                    validator = validate(collection, doc);
                    if (validator == 1) {

                        BasicDBObject query = new BasicDBObject();
                        BasicDBObject field = new BasicDBObject();
                        field.put("wheeler_4", 1);
                        DBCursor cursor = collection1.find(query, field);

                        BasicDBObject obj = (BasicDBObject) cursor.next();
                        count_car = obj.getInt("wheeler_4");
                        System.out.println(count_car);

                        BasicDBObject searchUpdate = new BasicDBObject();
                        searchUpdate.append("wheeler_4", count_car);

                        BasicDBObject UpdateObject = new BasicDBObject();
                        UpdateObject.append("$set", new BasicDBObject("wheeler_4", count_car + 1));

                        collection1.update(searchUpdate, UpdateObject);

                        //updating session...
                        int cnt_car;
                        BasicDBObject query2 = new BasicDBObject();
                        BasicDBObject field2 = new BasicDBObject();
                        field2.put("4wheeler_count", 1);
                        DBCursor cursor2 = collection2.find(query2, field2);

                        BasicDBObject obj2 = (BasicDBObject) cursor2.next();
                        cnt_car = obj2.getInt("4wheeler_count");
                        System.out.println(cnt_car);

                        BasicDBObject searchUpdate2 = new BasicDBObject();
                        searchUpdate2.append("4wheeler_count", cnt_car);

                        BasicDBObject UpdateObject2 = new BasicDBObject();
                        UpdateObject2.append("$set", new BasicDBObject("4wheeler_count", cnt_car + 1));

                        collection2.update(searchUpdate2, UpdateObject2);

                        setVisible(false);
                        new Entry_OR_Exit().setVisible(true);
                    }
                }
            }

            else {
                vehno = vehno + "W6";

                BasicDBObject queryx = new BasicDBObject();
                BasicDBObject fieldx = new BasicDBObject();
                fieldx.put("vehno", 1);
                DBCursor cursorx = collection.find(queryx, fieldx);

                while (cursorx.hasNext()) {

                    BasicDBObject objx = (BasicDBObject) cursorx.next();

                    String temp;
                    temp = objx.getString("vehno");
                    if (temp.equals(vehno)) {
                        JOptionPane.showMessageDialog(null, "vehicle number already exits!\n Entry Rejected!");
                        exists = 1;
                        setVisible(false);
                        new Veehicle_entry(veh_count).setVisible(true);

                    }
                }
                if (exists == 0) {

                    int count_other;

                    BasicDBObject doc = new BasicDBObject("_id", token).append("name", name)
                            .append("mobileno", mobno).append("vehno", vehno).append("intime", time_mills);

                    validator = validate(collection, doc);
                    if (validator == 1) {

                        System.out.println("Came 1");
                        BasicDBObject query = new BasicDBObject();
                        BasicDBObject field = new BasicDBObject();
                        field.put("other", 1);
                        DBCursor cursor = collection1.find(query, field);

                        BasicDBObject obj = (BasicDBObject) cursor.next();
                        count_other = obj.getInt("other");
                        System.out.println(count_other);

                        BasicDBObject searchUpdate = new BasicDBObject();
                        searchUpdate.append("other", count_other);

                        BasicDBObject UpdateObject = new BasicDBObject();
                        UpdateObject.append("$set", new BasicDBObject("other", count_other + 1));

                        collection1.update(searchUpdate, UpdateObject);

                        //updating session...
                        int cnt_other;
                        BasicDBObject query2 = new BasicDBObject();
                        BasicDBObject field2 = new BasicDBObject();
                        field2.put("other_count", 1);
                        DBCursor cursor2 = collection2.find(query2, field2);

                        BasicDBObject obj2 = (BasicDBObject) cursor2.next();
                        cnt_other = obj2.getInt("other_count");
                        System.out.println(cnt_other);

                        BasicDBObject searchUpdate2 = new BasicDBObject();
                        searchUpdate2.append("other_count", cnt_other);

                        BasicDBObject UpdateObject2 = new BasicDBObject();
                        UpdateObject2.append("$set", new BasicDBObject("other_count", cnt_other + 1));

                        collection2.update(searchUpdate2, UpdateObject2);

                        setVisible(false);
                        new Entry_OR_Exit().setVisible(true);
                    }
                }
            }
        } catch (Exception e) {

        }

    }//GEN-LAST:event_Entry_buttonActionPerformed

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

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

    }//GEN-LAST:event_vehno_strt1ActionPerformed

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

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

        setVisible(false);
        new Entry_OR_Exit().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(Veehicle_entry.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Veehicle_entry.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Veehicle_entry.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Veehicle_entry.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 Veehicle_entry().setVisible(true);
                /*
                Ask....DawNi Error....
                */
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Entry_button;
    private javax.swing.JTextField Name_field;
    private javax.swing.JTextField TimeDate;
    private javax.swing.JButton cancel_button;
    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.JTextField mobno_field;
    private javax.swing.JComboBox vehno_strt1;
    private javax.swing.JTextField vehno_strt2;
    private javax.swing.JComboBox vehno_strt3;
    private javax.swing.JTextField vehno_strt4;
    // End of variables declaration//GEN-END:variables
}