Java tutorial
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.net.UnknownHostException; 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. */ import com.mongodb.*; /** * * @author yogesh */ public class Add_train extends javax.swing.JFrame implements ActionListener { /** * Creates new form Add_train */ public Add_train() { initComponents(); } public boolean isAlpha(String name) { char[] chars = name.toCharArray(); for (char c : chars) { if (!Character.isLetter(c)) { return false; } } return true; } /** * 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"> private void initComponents() { jTextField4 = new javax.swing.JTextField(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jTextField1 = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); jTextField2 = new javax.swing.JTextField(); jLabel4 = new javax.swing.JLabel(); jTextField3 = new javax.swing.JTextField(); jLabel5 = new javax.swing.JLabel(); jComboBox1 = new javax.swing.JComboBox(); jComboBox5 = new javax.swing.JComboBox(); jLabel6 = new javax.swing.JLabel(); jTextField5 = new javax.swing.JTextField(); jLabel7 = new javax.swing.JLabel(); jPassword1 = new javax.swing.JPasswordField(); jPassword2 = new javax.swing.JPasswordField(); jLabel8 = new javax.swing.JLabel(); jLabel9 = new javax.swing.JLabel(); jLabel10 = new javax.swing.JLabel(); jLabel13 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jButton1 = new javax.swing.JButton(); jButton2 = new javax.swing.JButton(); jTextField4.setText("jTextField4"); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setBackground(new java.awt.Color(102, 255, 204)); jLabel1.setFont(new java.awt.Font("SansSerif", 3, 24)); // NOI18N jLabel1.setForeground(new java.awt.Color(204, 255, 204)); jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel1.setText("Receptionist Details"); jLabel1.setBounds(159, 23, 283, 77); add(jLabel1); jLabel2.setFont(new java.awt.Font("SansSerif", 3, 14)); // NOI18N jLabel2.setForeground(new java.awt.Color(204, 255, 204)); jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel2.setText("First Name : "); jLabel2.setBounds(20, 113, 100, 40); add(jLabel2); jTextField1.setBounds(134, 118, 140, 30); add(jTextField1); jLabel3.setFont(new java.awt.Font("SansSerif", 3, 14)); // NOI18N jLabel3.setForeground(new java.awt.Color(204, 255, 204)); jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel3.setText("Last Name : "); jLabel3.setBounds(290, 113, 100, 40); add(jLabel3); jTextField2.setBounds(410, 118, 140, 30); add(jTextField2); jLabel4.setFont(new java.awt.Font("SansSerif", 3, 14)); // NOI18N jLabel4.setForeground(new java.awt.Color(204, 255, 204)); jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel4.setText("Phone Number : "); jLabel4.setBounds(5, 183, 120, 40); add(jLabel4); jTextField3.setBounds(134, 188, 140, 30); add(jTextField3); jLabel5.setFont(new java.awt.Font("SansSerif", 3, 14)); // NOI18N jLabel5.setForeground(new java.awt.Color(204, 255, 204)); jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel5.setText("Age : "); jLabel5.setBounds(287, 183, 100, 40); add(jLabel5); jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46" })); jComboBox1.setBounds(410, 188, 80, 30); add(jComboBox1); jComboBox1.addActionListener(this); jLabel6.setFont(new java.awt.Font("SansSerif", 3, 14)); // NOI18N jLabel6.setForeground(new java.awt.Color(204, 255, 204)); jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel6.setText("Username : "); jLabel6.setBounds(19, 253, 100, 40); add(jLabel6); jTextField5.setBounds(134, 258, 140, 30); add(jTextField5); jLabel13.setFont(new java.awt.Font("SansSerif", 3, 14)); // NOI18N jLabel13.setForeground(new java.awt.Color(204, 255, 204)); jLabel13.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel13.setText("Batches : "); jLabel13.setBounds(267, 253, 120, 40); add(jLabel13); jComboBox5.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Morning", "Evening", "Night" })); jComboBox5.setBounds(410, 258, 90, 30); add(jComboBox5); jLabel7.setFont(new java.awt.Font("SansSerif", 3, 14)); // NOI18N jLabel7.setForeground(new java.awt.Color(204, 255, 204)); jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel7.setText("Password : "); jLabel7.setBounds(18, 323, 100, 40); add(jLabel7); jPassword1.setBounds(134, 328, 140, 30); add(jPassword1); jLabel10.setFont(new java.awt.Font("SansSerif", 3, 14)); // NOI18N jLabel10.setForeground(new java.awt.Color(204, 255, 204)); jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel10.setText("Confirm Password : "); jLabel10.setBounds(287, 323, 150, 40); add(jLabel10); jPassword2.setBounds(450, 328, 140, 30); add(jPassword2); jLabel8.setFont(new java.awt.Font("SansSerif", 3, 14)); // NOI18N jLabel8.setForeground(new java.awt.Color(204, 255, 204)); jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel8.setText("Address : "); jLabel8.setBounds(18, 393, 100, 40); add(jLabel8); jTextArea1.setColumns(20); jTextArea1.setRows(5); jScrollPane1.setViewportView(jTextArea1); jTextArea1.setBounds(134, 398, 300, 80); add(jTextArea1); jButton1.setBackground(new java.awt.Color(51, 0, 51)); jButton1.setFont(new java.awt.Font("SansSerif", 3, 12)); // NOI18N jButton1.setForeground(new java.awt.Color(255, 255, 255)); jButton1.setText("Add Customer"); jButton1.setBounds(190, 540, 210, 30); add(jButton1); jButton1.addActionListener(this); jButton2.setBackground(new java.awt.Color(51, 0, 51)); jButton2.setFont(new java.awt.Font("SansSerif", 3, 12)); // NOI18N jButton2.setForeground(new java.awt.Color(255, 255, 255)); jButton2.setText("Back"); jButton2.setBounds(20, 20, 80, 40); add(jButton2); jButton2.addActionListener(this); getContentPane().setBackground(new java.awt.Color(88, 0, 0)); setLayout(null); setSize(650, 680); }// </editor-fold> /** * @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(Add_train.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(Add_train.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(Add_train.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(Add_train.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 Add_train().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JButton jButton2; private javax.swing.JComboBox jComboBox1; private javax.swing.JComboBox jComboBox5; 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.JLabel jLabel8; private javax.swing.JLabel jLabel9; private javax.swing.JLabel jLabel10; private javax.swing.JLabel jLabel13; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField2; private javax.swing.JTextField jTextField3; private javax.swing.JTextField jTextField4; private javax.swing.JTextField jTextField5; private javax.swing.JPasswordField jPassword1; private javax.swing.JPasswordField jPassword2; // End of variables declaration @Override /* Check The code below for insertion of values */ public void actionPerformed(ActionEvent ae) { // TODO Auto-generated method stub add(jLabel9); jLabel9.setText(""); if (ae.getSource() == jButton1) { try { MongoClient mongo = new MongoClient("localhost", 27017); DB db = mongo.getDB("Gym"); DBCollection Allot = db.getCollection("Batch_Trainer"); DBCursor cursor1 = Allot.find(); DBCollection Gym_Collection = db.getCollection("trainer"); String first_name = jTextField1.getText(); String last_name = jTextField2.getText(); String password = jPassword1.getText().toString(); String dup = jPassword2.getText().toString(); String username = jTextField5.getText().toString(); String address = jTextArea1.getText(); String Batches = jComboBox5.getSelectedItem().toString(); float Slots, Morning, Evening, Night; cursor1.next(); Slots = Float.parseFloat(cursor1.curr().get("Slots").toString()); Morning = Float.parseFloat(cursor1.curr().get("Morning").toString()); Evening = Float.parseFloat(cursor1.curr().get("Evening").toString()); Night = Float.parseFloat(cursor1.curr().get("Night").toString()); int age = Integer.parseInt(jComboBox1.getSelectedItem().toString()); String phone_number = jTextField3.getText().toString(); DBCursor cursor = Gym_Collection.find(); int i = 0; while (cursor.hasNext()) { cursor.next(); if (username.equals(cursor.curr().get("Username").toString())) { i = 1; } } if (i == 1) { JOptionPane.showMessageDialog(this, "Username has been taken !!!"); jTextField5.setText(""); } else if (!isAlpha(first_name) || !isAlpha(last_name)) { JOptionPane.showMessageDialog(this, "Enter a valid name"); jTextField1.setText(""); jTextField2.setText(""); } else if (phone_number.length() < 8 || phone_number.length() > 10) { JOptionPane.showMessageDialog(this, "Enter a valid Phone no."); jTextField3.setText(""); } else if (!password.equals(dup)) { JOptionPane.showMessageDialog(this, "Passwords didn't match"); jPassword1.setText(""); jPassword2.setText(""); } else { int flag = 0, flag1 = 0, flag2 = 0, flag3 = 0; if (Batches.equals("Morning")) { Morning += 1; flag1 = 1; } else if (Batches.equals("Evening")) { Evening += 1; flag2 = 1; } else if (Batches.equals("Night")) { Night += 1; flag3 = 1; } if (Morning > Slots || Evening > Slots || Night > Slots) { if (flag1 == 1) { Morning -= 1; } else if (flag2 == 1) { Evening -= 1; } else if (flag3 == 1) { Night -= 1; } flag = 1; } if (flag == 1) { float temp1, temp2; if (flag1 == 1) { temp1 = Slots - Evening; temp2 = Slots - Night; JOptionPane.showMessageDialog(this, "This time slot slot is full. Evening has " + temp1 + " Slots. Night has " + temp2 + " Slots."); } else if (flag2 == 1) { temp1 = Slots - Morning; temp2 = Slots - Night; JOptionPane.showMessageDialog(this, "This time slot slot is full. Morning has " + temp1 + " Slots. Night has " + temp2 + " Slots."); } else { temp1 = Slots - Morning; temp2 = Slots - Evening; JOptionPane.showMessageDialog(this, "This time slot slot is full. Morning has " + temp1 + " Slots. Evening has " + temp2 + " Slots."); } } else { BasicDBObject doc = new BasicDBObject("First_Name", first_name.toLowerCase()) .append("Last_Name", last_name.toLowerCase()).append("Batch", Batches) .append("Phone_Number", phone_number).append("Age", age) .append("Username", username).append("Password", password) .append("Address", address); Gym_Collection.insert(doc); jTextField1.setText(""); jTextField2.setText(""); jTextField3.setText(""); jTextField5.setText(""); jPassword1.setText(""); jPassword2.setText(""); jTextArea1.setText(""); jLabel9.setFont(new java.awt.Font("SansSerif", 3, 16)); // NOI18N jLabel9.setForeground(new java.awt.Color(204, 255, 204)); jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER); jLabel9.setText("Successfully Added"); jLabel9.setBounds(200, 10, 200, 40); BasicDBObject newDocument = new BasicDBObject(); newDocument.append("$set", new BasicDBObject().append("Morning", Morning) .append("Evening", Evening).append("Night", Night)); BasicDBObject searchQuery = new BasicDBObject().append("Slots", Slots); Allot.update(searchQuery, newDocument); setVisible(true); } } } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NumberFormatException e) { JOptionPane.showMessageDialog(this, "Please Check the Data types"); jTextField1.setText(""); jTextField2.setText(""); jTextField3.setText(""); jTextField5.setText(""); jPassword1.setText(""); jPassword2.setText(""); jTextArea1.setText(""); } } else if (ae.getSource() == jButton2) { this.setVisible(false); new Admin().setVisible(true); } } }