Java tutorial
/* * 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. */ package payroll; import com.mongodb.BasicDBObject; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import java.awt.Color; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import javax.swing.JOptionPane; import javax.swing.JPanel; /** * * @author Gopinath */ public class FrmLeave extends javax.swing.JFrame { /** * Creates new form test */ public FrmLeave() { initComponents(); this.getContentPane().setBackground(Color.YELLOW); setDefaultCloseOperation(this.HIDE_ON_CLOSE); } /** * 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() { buttonGroup1 = new javax.swing.ButtonGroup(); jPanel3 = new javax.swing.JPanel(); lblRemainingleaves = new javax.swing.JLabel(); jPanel1 = new javax.swing.JPanel(); btnSubmit = new javax.swing.JButton(); lbltemp = new javax.swing.JLabel(); rdbCleave = new javax.swing.JRadioButton(); lblFrom = new javax.swing.JLabel(); lblTo = new javax.swing.JLabel(); rdbSleave = new javax.swing.JRadioButton(); dtFrom = new com.toedter.calendar.JDateChooser(); dtTo = new com.toedter.calendar.JDateChooser(); lblEmpid = new javax.swing.JLabel(); btnBack = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setTitle("LEAVE"); addWindowListener(new java.awt.event.WindowAdapter() { public void windowActivated(java.awt.event.WindowEvent evt) { formWindowActivated(evt); } }); jPanel3.setBackground(new java.awt.Color(255, 255, 0)); lblRemainingleaves.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N lblRemainingleaves.setText("Remaining Leaves Status"); jPanel1.setBackground(new java.awt.Color(51, 255, 153)); jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.SoftBevelBorder( javax.swing.border.BevelBorder.RAISED, null, null, null, java.awt.Color.blue))); btnSubmit.setFont(new java.awt.Font("Times New Roman", 0, 12)); // NOI18N btnSubmit.setText("Submit"); btnSubmit.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSubmitActionPerformed(evt); } }); lbltemp.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N lbltemp.setText("Select the type of leave"); buttonGroup1.add(rdbCleave); rdbCleave.setFont(new java.awt.Font("Times New Roman", 2, 12)); // NOI18N rdbCleave.setText("Casual Leave"); lblFrom.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N lblFrom.setText("From:"); lblTo.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N lblTo.setText("To:"); buttonGroup1.add(rdbSleave); rdbSleave.setFont(new java.awt.Font("Times New Roman", 2, 12)); // NOI18N rdbSleave.setText("Sick Leave"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(jPanel1Layout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(lblTo) .addGap(164, 164, 164)) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(rdbSleave) .addComponent(lbltemp, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(rdbCleave).addComponent(btnSubmit)))) .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup().addComponent(lblFrom).addGap(29, 29, 29) .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(dtTo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(dtFrom, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))) .addContainerGap())); jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(lblFrom).addComponent(dtFrom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(lblTo).addComponent(dtTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(30, 30, 30).addComponent(lbltemp).addGap(18, 18, 18).addComponent(rdbCleave) .addGap(18, 18, 18).addComponent(rdbSleave).addGap(28, 28, 28).addComponent(btnSubmit) .addContainerGap())); lblEmpid.setFont(new java.awt.Font("Times New Roman", 3, 12)); // NOI18N lblEmpid.setText("Employee Id"); btnBack.setText("Back"); btnBack.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnBackActionPerformed(evt); } }); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel3Layout.setHorizontalGroup(jPanel3Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addGap(230, 230, 230) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup().addGap(0, 160, Short.MAX_VALUE) .addComponent(lblRemainingleaves, javax.swing.GroupLayout.PREFERRED_SIZE, 510, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18)) .addGroup(jPanel3Layout.createSequentialGroup().addGap(23, 23, 23).addComponent(lblEmpid) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(btnBack).addGap(28, 28, 28))); jPanel3Layout.setVerticalGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel3Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(lblEmpid).addComponent(btnBack)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE) .addComponent(lblRemainingleaves).addGap(37, 37, 37) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(45, 45, 45))); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent( jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); pack(); }// </editor-fold>//GEN-END:initComponents private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubmitActionPerformed // TODO add your handling code here: Connect1 j = new Connect1(); DBCollection dbc = j.connect("leave"); BasicDBObject bdb = new BasicDBObject(); Date d1 = dtFrom.getDate(); Date d2 = dtTo.getDate(); Calendar calendar = new GregorianCalendar(); if ((d1.getDate() > d2.getDate()) && (d1.getMonth()) == d2.getMonth()) { JOptionPane.showMessageDialog(null, "Invalid order of dates", "FAILURE", JOptionPane.ERROR_MESSAGE); return; } if (d1.getDate() < calendar.get(Calendar.DATE) && d1.getMonth() == calendar.get(Calendar.DATE)) { JOptionPane.showMessageDialog(null, "Only future leaves can be taken", "FAILURE", JOptionPane.ERROR_MESSAGE); System.out.println("1"); return; } if (d1.getMonth() > d2.getMonth()) { JOptionPane.showMessageDialog(null, "Invalid order of dates", "FAILURE", JOptionPane.ERROR_MESSAGE); System.out.println("2"); return; } int end = 0; int from = d1.getDate(); int to = d2.getDate(); int from_month = d1.getMonth(); int to_month = d2.getMonth(); if (from_month != to_month) { switch (from_month) { case 0: end = 31; break; case 1: end = 28; break; case 2: end = 31; break; case 3: end = 30; break; case 4: end = 31; break; case 5: end = 30; break; case 6: end = 31; break; case 7: end = 31; break; case 8: end = 30; break; case 9: end = 31; break; case 10: end = 30; break; case 11: end = 31; break; } String type = ""; if (rdbCleave.isSelected()) { type = "Casual Leave"; } else { type = "Sick Leave"; } bdb.put("ID", lblEmpid.getText()); bdb.put("From", d1.getDate()); bdb.put("To", end); bdb.put("Month", d1.getMonth()); bdb.put("Type", type); BasicDBObject bdb3 = new BasicDBObject(); bdb3.put("ID", lblEmpid.getText()); bdb3.put("From", 1); bdb3.put("To", d2.getDate()); bdb3.put("Month", d2.getMonth()); bdb3.put("Type", type); DBCollection dbc1 = j.connect("remaining_leaves"); BasicDBObject bdb1 = new BasicDBObject(); bdb1.put("ID", lblEmpid.getText()); DBCursor cur = dbc1.find(bdb1); DBObject obj = cur.next(); int rcleaves = (Integer) obj.get("Casual_leaves"); int rsleaves = (Integer) obj.get("Sick_leaves"); if (type.equals("Casual Leave")) { if (rcleaves >= end - d1.getDate()) { bdb.put("Paid_Casual", end - d1.getDate() + 1); bdb.put("Paid_Sick", 0); rcleaves -= end - d1.getDate(); } else { bdb.put("Paid_Casual", rcleaves + 1); bdb.put("Paid_Sick", 0); rcleaves = 0; } } else { if (rsleaves >= end - d1.getDate()) { bdb.put("Paid_Sick", end - d1.getDate() + 1); bdb.put("Paid_Casual", 0); rsleaves -= end - d1.getDate(); } else { bdb.put("Paid_Sick", rsleaves + 1); bdb.put("Paid_Casual", 0); rsleaves = 0; } } if (type.equals("Casual Leave")) { if (rcleaves >= d2.getDate() - 1) { bdb3.put("Paid_Casual", d2.getDate() - 1 + 1); bdb3.put("Paid_Sick", 0); rcleaves -= d2.getDate() - 1; } else { bdb3.put("Paid_Casual", rcleaves + 1); bdb3.put("Paid_Sick", 0); rcleaves = 0; } } else { if (rsleaves >= d2.getDate() - 1) { bdb3.put("Paid_Sick", d2.getDate() - 1 + 1); bdb3.put("Paid_Casual", 0); rsleaves -= d2.getDate() - 1; } else { bdb3.put("Paid_Sick", rsleaves + 1); bdb3.put("Paid_Casual", 0); rsleaves = 0; } } dbc.insert(bdb); dbc.insert(bdb3); bdb1.put("Casual_leaves", rcleaves); bdb1.put("Sick_leaves", rsleaves); dbc1.update(obj, bdb1); final JPanel p2 = new JPanel(); final JOptionPane jo = new JOptionPane(); JOptionPane.showMessageDialog(p2, "Leave Taken", "Success", JOptionPane.INFORMATION_MESSAGE); this.setVisible(false); } BasicDBObject bdb2 = new BasicDBObject(); bdb2.put("Month", from_month); bdb2.put("ID", lblEmpid.getText()); DBCursor cur1 = dbc.find(bdb2); while (cur1.hasNext()) { DBObject obj = cur1.next(); int from1 = (Integer) obj.get("From"); int to1 = (Integer) obj.get("To"); if (from >= from1 && from <= to1) { JOptionPane.showMessageDialog(null, "You have already taken leave in this period", "Failure", JOptionPane.ERROR_MESSAGE); return; } else if (to >= from1 && to <= to1) { JOptionPane.showMessageDialog(null, "You have already taken leave in this period", "Failure", JOptionPane.ERROR_MESSAGE); return; } else if (from <= from1 && to >= to1) { JOptionPane.showMessageDialog(null, "You have already taken leave in this period", "Failure", JOptionPane.ERROR_MESSAGE); return; } } String type = ""; if (rdbCleave.isSelected()) { type = "Casual Leave"; } else { type = "Sick Leave"; } bdb.put("ID", lblEmpid.getText()); bdb.put("From", d1.getDate()); bdb.put("To", d2.getDate()); bdb.put("Month", d1.getMonth()); bdb.put("Type", type); DBCollection dbc1 = j.connect("remaining_leaves"); BasicDBObject bdb1 = new BasicDBObject(); bdb1.put("ID", lblEmpid.getText()); DBCursor cur = dbc1.find(bdb1); DBObject obj = cur.next(); int rcleaves = (Integer) obj.get("Casual_leaves"); int rsleaves = (Integer) obj.get("Sick_leaves"); if (type.equals("Casual Leave")) { if (rcleaves >= d2.getDate() - d1.getDate()) { bdb.put("Paid_Casual", d2.getDate() - d1.getDate()); bdb.put("Paid_Sick", 0); rcleaves -= d2.getDate() - d1.getDate(); } else { bdb.put("Paid_Casual", rcleaves); bdb.put("Paid_Sick", 0); rcleaves = 0; } } else { if (rsleaves >= d2.getDate() - d1.getDate()) { bdb.put("Paid_Sick", d2.getDate() - d1.getDate()); bdb.put("Paid_Casual", 0); rsleaves -= d2.getDate() - d1.getDate(); } else { bdb.put("Paid_Sick", rsleaves); bdb.put("Paid_Casual", 0); rsleaves = 0; } } dbc.insert(bdb); bdb1.put("Casual_leaves", rcleaves); bdb1.put("Sick_leaves", rsleaves); dbc1.update(obj, bdb1); final JPanel p2 = new JPanel(); final JOptionPane jo = new JOptionPane(); JOptionPane.showMessageDialog(p2, "Leave Taken", "Success", JOptionPane.INFORMATION_MESSAGE); this.setVisible(false); }//GEN-LAST:event_btnSubmitActionPerformed private void formWindowActivated(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowActivated // TODO add your handling code here: Connect1 j = new Connect1(); DBCollection dbc = j.connect("remaining_leaves"); String user = lblEmpid.getText(); BasicDBObject bdb = new BasicDBObject(); bdb.put("ID", user); DBCursor cur = dbc.find(); DBObject obj = cur.next(); int rcleaves = (Integer) obj.get("Casual_leaves"); int rsleaves = (Integer) obj.get("Sick_leaves"); if (rsleaves < 0) rsleaves = 0; if (rcleaves < 0) rcleaves = 0; lblRemainingleaves.setText( "You have " + rcleaves + " Casual Leaves remaining and " + rsleaves + " Sick Leaves remaining"); }//GEN-LAST:event_formWindowActivated private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed // TODO add your handling code here: new FrmLogin().setVisible(true); this.setVisible(false); }//GEN-LAST:event_btnBackActionPerformed /** * @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(FrmLeave.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(FrmLeave.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(FrmLeave.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(FrmLeave.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> //</editor-fold> //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new FrmLeave().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnBack; private javax.swing.JButton btnSubmit; private javax.swing.ButtonGroup buttonGroup1; private com.toedter.calendar.JDateChooser dtFrom; private com.toedter.calendar.JDateChooser dtTo; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel3; public static javax.swing.JLabel lblEmpid; private javax.swing.JLabel lblFrom; private javax.swing.JLabel lblRemainingleaves; private javax.swing.JLabel lblTo; private javax.swing.JLabel lbltemp; private javax.swing.JRadioButton rdbCleave; private javax.swing.JRadioButton rdbSleave; // End of variables declaration//GEN-END:variables }