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 pkginterface; import control.PaymentDetailsControl; import control.TransactionControl; import domain.PaymentDetails; import domain.Staff; import domain.TransactionDetails; import java.awt.event.KeyEvent; import java.sql.ResultSet; import java.sql.SQLException; import java.text.DecimalFormat; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import java.io.FileNotFoundException; import java.io.FileOutputStream; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import control.BusDetailsControl; import control.BusScheduleControl; import control.CustTypeControl; import domain.BusDetailsDomain; import domain.BusScheduleDomain; import domain.CustTypeDomain; import java.awt.Font; import static java.awt.Font.ITALIC; import java.text.SimpleDateFormat; import java.util.Date; import static javax.swing.JOptionPane.YES_NO_OPTION; /** * * @author waiho */ public class PayByCash extends javax.swing.JFrame { /** * Creates new form PayByCash */ TransactionControl tControl = new TransactionControl(); double total = 0; String tempD = null; String tempI = null; PaymentDetailsControl pControl = new PaymentDetailsControl(); public PayByCash() { initComponents(); setLocationRelativeTo(null); PaymentDetails pd = pControl.getLastRow(); if (pd != null) { PaymentDetails paymentDetails = pControl.getRecord(pd.getPaycountId()); total = paymentDetails.getTotalAmount(); String strTotal = "" + total; totalAmount.setText(strTotal); } } /** * 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(); jLabel2 = new javax.swing.JLabel(); totalAmount = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); cashReceived = new javax.swing.JTextField(); Pay = new javax.swing.JButton(); jLabel4 = new javax.swing.JLabel(); jtfReturn = new javax.swing.JTextField(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setFont(new java.awt.Font("Pristina", 3, 48)); // NOI18N jLabel1.setText("Cash"); jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N jLabel2.setText("Total Amount :"); totalAmount.setEditable(false); totalAmount.setBackground(new java.awt.Color(204, 204, 204)); jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N jLabel3.setText("Cash Received :"); cashReceived.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { cashReceivedActionPerformed(evt); } }); cashReceived.addKeyListener(new java.awt.event.KeyAdapter() { public void keyTyped(java.awt.event.KeyEvent evt) { cashReceivedKeyTyped(evt); } }); Pay.setText("Pay"); Pay.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { PayActionPerformed(evt); } }); jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N jLabel4.setText("Cash Return :"); jtfReturn.setEditable(false); jtfReturn.setBackground(new java.awt.Color(204, 204, 204)); jButton1.setText("OK"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addGap(23, 23, 23) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addGroup(layout .createSequentialGroup().addComponent(jLabel4).addGap(18, 18, 18) .addComponent( jtfReturn, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(Pay) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(layout.createSequentialGroup().addComponent(jLabel3) .addGap(18, 18, 18).addComponent(cashReceived)) .addGroup(layout.createSequentialGroup().addComponent(jLabel2) .addGap(18, 18, 18).addComponent(totalAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))) .addGroup(layout.createSequentialGroup().addGap(53, 53, 53).addComponent( jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE))) .addComponent(jButton1)) .addContainerGap(42, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(totalAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel3).addComponent(cashReceived, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18).addComponent(Pay).addGap(18, 18, 18) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jtfReturn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(jLabel4)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 30, Short.MAX_VALUE) .addComponent(jButton1).addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents private void cashReceivedActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cashReceivedActionPerformed // TODO add your handling code here: }//GEN-LAST:event_cashReceivedActionPerformed private void PayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PayActionPerformed // TODO add your handling code here: DecimalFormat df = new DecimalFormat("#.##"); String validation = "[0-9]+(\\.){0,1}[0-9]"; String cashReceive = cashReceived.getText(); if (cashReceive.matches(validation)) { double cash = Double.parseDouble(cashReceive); double result = cash - total; if (result < 0) { JOptionPane.showMessageDialog(null, "Not Enough Cash!", "Error!", JOptionPane.ERROR_MESSAGE); } else { jtfReturn.setText("" + df.format(result)); } } else { JOptionPane.showMessageDialog(null, "The Price Format is Incorrect! \nPlease try again!", "Error!", JOptionPane.ERROR_MESSAGE); cashReceived.setText(""); } }//GEN-LAST:event_PayActionPerformed private void cashReceivedKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cashReceivedKeyTyped // TODO add your handling code here: }//GEN-LAST:event_cashReceivedKeyTyped private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed // TODO add your handling code here: String validation = "[0-9]+(\\.){0,1}[0-9]"; String cashReceive = cashReceived.getText(); if (cashReceive.equals("")) { JOptionPane.showMessageDialog(null, "Please Fill in the Price!", "Error!", JOptionPane.ERROR_MESSAGE); cashReceived.setText(""); } else if (!cashReceive.matches(validation)) { JOptionPane.showMessageDialog(null, "The Price Format is Incorrect! \nPlease try again!", "Error!", JOptionPane.ERROR_MESSAGE); cashReceived.setText(""); } else { ResultSet trs = null; trs = tControl.getRecordWithTranstStatus("Pending"); try { while (trs.next()) { TransactionDetails transID = tControl.getRecord(trs.getString(1)); String id = transID.getTranstId(); String status = "Paid"; TransactionDetails setPaid = new TransactionDetails(status, id); tControl.setStatus(setPaid); } } catch (SQLException ex) { Logger.getLogger(Payment.class.getName()).log(Level.SEVERE, null, ex); } catch (NumberFormatException e) { } int confirmDialog = JOptionPane.showConfirmDialog(rootPane, "Payment Completed!\nDo you want to generate receipt?", null, YES_NO_OPTION); //This part is to generate ticket Document document = new Document(); Document document1 = new Document(); BusDetailsControl bdControl = new BusDetailsControl(); BusScheduleControl bsControl = new BusScheduleControl(); CustTypeControl ctControl = new CustTypeControl(); ResultSet rs = Payment.getForTandR(); PdfWriter writer = null; try { writer = PdfWriter.getInstance(document, new FileOutputStream("../" + Payment.getPaymentID() + ".pdf")); document.open(); while (rs.next()) { BusScheduleDomain bs = bsControl.getRecord(rs.getString(2)); BusDetailsDomain bd = bdControl.getRecord(bs.getBusId().getBusId()); CustTypeDomain ct = ctControl.getRecord(rs.getString(3)); tempD = bs.getDepartLocation(); tempI = bs.getArriveLocation(); document.add(new Paragraph("TICKET")); document.add(new Paragraph("Depart Location : " + bs.getDepartLocation())); document.add(new Paragraph("Arrive Location : " + bs.getArriveLocation())); document.add(new Paragraph("Depart Date : " + bs.getScheduleDate())); document.add(new Paragraph("Depart Time : " + bs.getScheduleTime())); document.add(new Paragraph("Customer Type : " + ct.getCustType())); document.add(new Paragraph("Bus Plate Number : " + bd.getBusPlateNum())); document.add(new Paragraph("Bus Type : " + bd.getBusType())); document.add(new Paragraph( "Price : " + bs.getPrice() * ct.getDiscountRate())); document.add(new Paragraph(" " + " ")); } } catch (DocumentException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (SQLException ex) { Logger.getLogger(PayByCash.class.getName()).log(Level.SEVERE, null, ex); } if (confirmDialog == JOptionPane.YES_OPTION) { PdfWriter writer1 = null; Date today = new Date(); SimpleDateFormat dateF = new SimpleDateFormat("d-M-yyyy"); SimpleDateFormat timeF = new SimpleDateFormat("HH:mm:ss"); try { writer1 = PdfWriter.getInstance(document1, new FileOutputStream("../" + Payment.getPaymentID() + " Receipt.pdf")); document1.open(); document1.add(new Paragraph(" SERI MEJU BUS Malaysia Sdn. Bhd.")); document1.add(new Paragraph(" L3-11, JLN LINGKARAN TENGAG LI,")); document1.add(new Paragraph(" BANDAR TASIK SELATAN,")); document1.add(new Paragraph(" 57100 KUALA LUMPUR,")); document1.add(new Paragraph(" WILAYAH PERSEKUTUAN KUALA LUMPUR,")); document1.add(new Paragraph(" MALAYSIA")); document1.add(new Paragraph(" ")); document1.add(new Paragraph("RECEIPT")); document1.add(new Paragraph( "---------------------------------------------------------------------------------------------")); document1.add(new Paragraph("Staff ID : " + new StaffMaintenance().getLoginUser())); document1.add(new Paragraph("Date : " + dateF.format(today))); document1.add(new Paragraph("Time : " + timeF.format(today))); document1.add(new Paragraph("From : " + tempD + ">>>>>>>>>>>>>>>>>>" + "To : " + tempI)); document1.add(new Paragraph("Total : " + total)); document1.add(new Paragraph("Payment Type : Cash ")); document1.add(new Paragraph( "---------------------------------------------------------------------------------------------")); document1.add(new Paragraph("Contact us : ")); document1.add(new Paragraph("(Tel)1300-88-1111 (Email)serimejubusservice@gmail.com")); document1.close(); JOptionPane.showMessageDialog(null, "The Ticket(s) and Receipt have been stored to the Desktop of this PC."); } catch (FileNotFoundException ex) { Logger.getLogger(PayByCash.class.getName()).log(Level.SEVERE, null, ex); } catch (DocumentException ex) { Logger.getLogger(PayByCash.class.getName()).log(Level.SEVERE, null, ex); } } else { JOptionPane.showMessageDialog(null, "The Ticket(s) have been stored to the Desktop of this PC."); } document.close(); writer.close(); //End of generate ticket dispose(); new MainMenu(); } }//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(PayByCash.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(PayByCash.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(PayByCash.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(PayByCash.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 PayByCash().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton Pay; private javax.swing.JTextField cashReceived; 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 jtfReturn; private javax.swing.JTextField totalAmount; // End of variables declaration//GEN-END:variables }