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 GUI; import com.itextpdf.text.BaseColor; import com.itextpdf.text.Document; import com.itextpdf.text.Element; import com.itextpdf.text.Font; import com.itextpdf.text.FontFactory; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; import dao.PanierDao; import dao.ProduitDao; import entite.Produit; import java.io.FileOutputStream; import java.sql.ResultSet; import java.util.Date; import javax.swing.JOptionPane; import javax.swing.table.TableModel; import utils.PanieruserModel; /** * * @author Toshiba */ public class FrameUserPanier extends javax.swing.JFrame { ResultSet rsNom = null; PanierDao pdao = new PanierDao(); ProduitDao prodao = new ProduitDao(); /** * Creates new form FrameUserPanier */ public FrameUserPanier() { initComponents(); //loadAllNom(); // jLabel8.setText(""+CalculSomme()); } /** * 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() { jButton1 = new javax.swing.JButton(); PanelListProduitPanier = new javax.swing.JPanel(); jScrollPane1 = new javax.swing.JScrollPane(); TablePanier = new javax.swing.JTable(); jLabel1 = new javax.swing.JLabel(); PanelPanier = new javax.swing.JPanel(); jLabel4 = new javax.swing.JLabel(); TFNomprod = new javax.swing.JTextField(); BtnSupprimerprod = new javax.swing.JButton(); jLabel6 = new javax.swing.JLabel(); TFidpan = new javax.swing.JTextField(); jLabel7 = new javax.swing.JLabel(); TFnomut = new javax.swing.JTextField(); BtnValider = new javax.swing.JButton(); jLabel2 = new javax.swing.JLabel(); TFnombreprod = new javax.swing.JTextField(); jLabel3 = new javax.swing.JLabel(); TFdescription = new javax.swing.JTextField(); btnsupppan = new javax.swing.JButton(); jLabel5 = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); jButton1.setText("jButton1"); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); PanelListProduitPanier.setBorder(javax.swing.BorderFactory.createTitledBorder("")); PanelListProduitPanier.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { PanelListProduitPanierMouseClicked(evt); } }); TablePanier.setModel(new PanieruserModel()); TablePanier.addMouseListener(new java.awt.event.MouseAdapter() { public void mouseClicked(java.awt.event.MouseEvent evt) { TablePanierMouseClicked(evt); } public void mouseEntered(java.awt.event.MouseEvent evt) { TablePanierMouseEntered(evt); } }); jScrollPane1.setViewportView(TablePanier); jLabel1.setFont(new java.awt.Font("Tahoma", 3, 18)); // NOI18N jLabel1.setText("Liste de Produit Dans le panier"); javax.swing.GroupLayout PanelListProduitPanierLayout = new javax.swing.GroupLayout(PanelListProduitPanier); PanelListProduitPanier.setLayout(PanelListProduitPanierLayout); PanelListProduitPanierLayout.setHorizontalGroup(PanelListProduitPanierLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(PanelListProduitPanierLayout.createSequentialGroup() .addGroup(PanelListProduitPanierLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(PanelListProduitPanierLayout.createSequentialGroup().addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(PanelListProduitPanierLayout.createSequentialGroup().addGap(153, 153, 153) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 287, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))); PanelListProduitPanierLayout.setVerticalGroup(PanelListProduitPanierLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(PanelListProduitPanierLayout.createSequentialGroup().addContainerGap() .addComponent(jLabel1).addGap(20, 20, 20).addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(56, Short.MAX_VALUE))); PanelPanier.setBorder(javax.swing.BorderFactory.createTitledBorder("")); jLabel4.setText("Nom du produit :"); TFNomprod.setEditable(false); BtnSupprimerprod.setText("Supprimer Produit du Panier"); BtnSupprimerprod.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BtnSupprimerprodActionPerformed(evt); } }); jLabel6.setText("IdPanier :"); TFidpan.setEditable(false); jLabel7.setText("Username :"); TFnomut.setEditable(false); BtnValider.setText("valider Panier"); BtnValider.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { BtnValiderActionPerformed(evt); } }); jLabel2.setText("Nombre Produit:"); jLabel3.setText("Description:"); btnsupppan.setText("Supprimer Panier"); btnsupppan.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnsupppanActionPerformed(evt); } }); jLabel8.setText("jLabel8"); javax.swing.GroupLayout PanelPanierLayout = new javax.swing.GroupLayout(PanelPanier); PanelPanier.setLayout(PanelPanierLayout); PanelPanierLayout.setHorizontalGroup( PanelPanierLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup( javax.swing.GroupLayout.Alignment.TRAILING, PanelPanierLayout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addComponent(BtnValider)) .addGroup(PanelPanierLayout.createSequentialGroup().addContainerGap() .addGroup(PanelPanierLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(PanelPanierLayout .createSequentialGroup().addGroup(PanelPanierLayout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4).addComponent(jLabel7) .addComponent(jLabel6)) .addGap(18, 18, 18) .addGroup(PanelPanierLayout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(TFidpan, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(PanelPanierLayout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent( TFNomprod, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE) .addComponent(TFnomut)))) .addGroup(PanelPanierLayout.createSequentialGroup() .addGroup(PanelPanierLayout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2) .addGroup(PanelPanierLayout .createParallelGroup( javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel5).addComponent(jLabel3))) .addGap(18, 18, 18) .addGroup(PanelPanierLayout .createParallelGroup( javax.swing.GroupLayout.Alignment.LEADING) .addComponent(TFnombreprod).addComponent(TFdescription)))) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addGroup(PanelPanierLayout.createSequentialGroup() .addGroup(PanelPanierLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jLabel8).addComponent(BtnSupprimerprod)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent( btnsupppan, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE))); PanelPanierLayout.setVerticalGroup(PanelPanierLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(PanelPanierLayout.createSequentialGroup().addGap(16, 16, 16).addComponent(BtnValider) .addGroup(PanelPanierLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(PanelPanierLayout.createSequentialGroup() .addGroup(PanelPanierLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel6) .addComponent(TFidpan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE) .addGroup(PanelPanierLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(TFnomut, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(PanelPanierLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel4).addComponent(TFNomprod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(PanelPanierLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel2).addComponent(TFnombreprod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(PanelPanierLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(TFdescription, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(28, 28, 28).addComponent(jLabel5)) .addGroup(PanelPanierLayout.createSequentialGroup().addGap(0, 0, Short.MAX_VALUE) .addComponent(jLabel8))) .addGap(18, 18, 18) .addGroup(PanelPanierLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(BtnSupprimerprod).addComponent(btnsupppan)))); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(PanelListProduitPanier, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(PanelPanier, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(36, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap().addGroup(layout .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(PanelPanier, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(PanelListProduitPanier, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(34, Short.MAX_VALUE))); pack(); }// </editor-fold>//GEN-END:initComponents private void TablePanierMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TablePanierMouseClicked // TODO add your handling code here: int i = TablePanier.getSelectedRow(); TableModel model = TablePanier.getModel(); TFidpan.setText(model.getValueAt(i, 0).toString()); TFnomut.setText(model.getValueAt(i, 2).toString()); TFNomprod.setText(model.getValueAt(i, 4).toString()); TFnombreprod.setText(model.getValueAt(i, 5).toString()); TFdescription.setText(model.getValueAt(i, 6).toString()); }//GEN-LAST:event_TablePanierMouseClicked private void BtnSupprimerprodActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnSupprimerprodActionPerformed FrameUserPanier f = new FrameUserPanier(); int i = TablePanier.getSelectedRow(); Object l = TablePanier.getValueAt(i, 3); PanierDao pdao = new PanierDao(); int j = JOptionPane.showConfirmDialog(null, " Etes-vous sur de vouloir continuer?", "Veuillez confirmer votre choix", JOptionPane.YES_NO_OPTION); if (j == 0) { pdao.removeProduit((int) l, LoginForm.getUser_id()); TablePanier.setModel(new PanieruserModel()); jScrollPane1.setViewportView(TablePanier); } }//GEN-LAST:event_BtnSupprimerprodActionPerformed private void PanelListProduitPanierMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PanelListProduitPanierMouseClicked // TODO add your handling code here: }//GEN-LAST:event_PanelListProduitPanierMouseClicked private void TablePanierMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TablePanierMouseEntered // TODO add your handling code here: }//GEN-LAST:event_TablePanierMouseEntered private void BtnValiderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnValiderActionPerformed jLabel8.setText("" + CalculSomme()); String val1 = TFidpan.getText(); String val2 = TFnomut.getText(); String val3 = TFNomprod.getText(); String val4 = TFnombreprod.getText(); String val5 = TFdescription.getText(); try { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream("Report.pdf")); document.open(); document.add(new Paragraph("VotrePanier", FontFactory.getFont(FontFactory.TIMES_BOLD, 18, Font.BOLD, BaseColor.BLUE))); document.add(new Paragraph(new Date().toString())); PdfPTable table = new PdfPTable(5); PdfPCell cell = new PdfPCell(new Paragraph("Mes achats")); cell.setColspan(5); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBackgroundColor(BaseColor.BLUE); table.addCell(cell); table.addCell("IdPanier"); table.addCell("NomUtilsateur"); table.addCell("Nomprod"); table.addCell("Nombreprod"); table.addCell("Description"); cell.setBackgroundColor(BaseColor.BLUE); int i = TablePanier.getRowCount(); TableModel model = TablePanier.getModel(); for (int j = 0; j < i; j++) { val1 = model.getValueAt(j, 0).toString(); val2 = model.getValueAt(j, 2).toString(); val3 = model.getValueAt(j, 4).toString(); val4 = model.getValueAt(j, 5).toString(); val5 = model.getValueAt(j, 6).toString(); table.addCell(val1); table.addCell(val2); table.addCell(val3); table.addCell(val4); table.addCell(val5); } int z = TablePanier.getRowCount(); model = TablePanier.getModel(); float sum = 0; int bonus = 0; float net = 0; for (int j = 0; j < z; j++) { String x = model.getValueAt(j, 3).toString(); int idprod = Integer.parseInt(x); String q = model.getValueAt(j, 5).toString(); int qu = Integer.parseInt(q); Produit p = pdao.findByprodId(idprod); sum += p.getPrix() * qu; bonus += (p.getPtbonus() * qu); net += sum - (p.getReduction() * qu); } table.addCell("Somme: " + sum); table.addCell(""); table.addCell("Net: " + net); table.addCell(""); table.addCell("Bonus :" + bonus); document.add(table); document.close(); JOptionPane.showMessageDialog(null, "Report Saved"); } catch (Exception e) { JOptionPane.showMessageDialog(null, e); } }//GEN-LAST:event_BtnValiderActionPerformed private void btnsupppanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsupppanActionPerformed FrameUserPanier f = new FrameUserPanier(); int i = TablePanier.getSelectedRow(); Object l = TablePanier.getValueAt(i, 1); PanierDao pdao = new PanierDao(); int j = JOptionPane.showConfirmDialog(null, "la suppression est irrversible. Etes-vous sur de vouloir continuer?", "Veuillez confirmer votre choix", JOptionPane.YES_NO_OPTION); if (j == 0) { pdao.removeById((int) l); TablePanier.setModel(new PanieruserModel()); jScrollPane1.setViewportView(TablePanier); } }//GEN-LAST:event_btnsupppanActionPerformed public float CalculSomme() { int i = TablePanier.getRowCount(); TableModel model = TablePanier.getModel(); float sum = 0; int bonus = 0; float net = 0; for (int j = 0; j < i; j++) { String x = model.getValueAt(j, 3).toString(); int idprod = Integer.parseInt(x); String q = model.getValueAt(j, 5).toString(); int qu = Integer.parseInt(q); Produit p = pdao.findByprodId(idprod); sum += p.getPrix() * qu; bonus += (p.getPtbonus() * qu); net += sum - (p.getReduction() * qu); } return sum; } /** * @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(FrameUserPanier.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(FrameUserPanier.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(FrameUserPanier.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(FrameUserPanier.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() { @Override public void run() { new FrameUserPanier().setVisible(true); } }); } /* private void loadAllNom() { try { ResultSet res = panier.getAllNom(); while (res.next()) { CBNPRech.addItem(res.getString(1)); } } catch (SQLException ex) { Logger.getLogger(FramePanier.class.getName()).log(Level.SEVERE, null, ex); } */ // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton BtnSupprimerprod; private javax.swing.JButton BtnValider; private javax.swing.JPanel PanelListProduitPanier; private javax.swing.JPanel PanelPanier; private javax.swing.JTextField TFNomprod; private javax.swing.JTextField TFdescription; private javax.swing.JTextField TFidpan; private javax.swing.JTextField TFnombreprod; private javax.swing.JTextField TFnomut; private javax.swing.JTable TablePanier; private javax.swing.JButton btnsupppan; 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.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JScrollPane jScrollPane1; // End of variables declaration//GEN-END:variables }