HarrJ

Day 27 2n3

Oct 15th, 2023
870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.07 KB | None | 0 0
  1. -- para sa group 2 and 3 sample
  2.  
  3. CREATE TABLE tbl_g23_orderlst (
  4.     fld_orid INT AUTO_INCREMENT PRIMARY KEY
  5.     , fld_id INT NOT NULL
  6.     , fld_curr_price DECIMAL(20,4) NOT NULL
  7.     , fld_or_qty INT NOT NULL
  8.     , fld_or_qtytotal DECIMAL(20,4) NOT NULL
  9.     , fld_recid INT NOT NULL
  10. );
  11.  
  12. CREATE TABLE tbl_g23_reciept (
  13.     fld_recid INT AUTO_INCREMENT PRIMARY KEY
  14.     , fld_tr_date DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
  15.     , fld_rec_qty INT NOT NULL
  16.     , fld_rec_qtytotal DECIMAL(20,4) NOT NULL
  17. );
  18.  
  19. INSERT INTO tbl_g23_orderlst(fld_id, fld_curr_price, fld_or_qty, fld_or_qtytotal, fld_recid) VALUES (?,?,?,?);
  20.  
  21. INSERT INTO tbl_g23_reciept(fld_rec_qty, fld_rec_qtytotal) VALUE (?,?);
  22.  
  23. SELECT fld_recid FROM tbl_g23_reciept WHERE fld_rec_qty = ? AND fld_rec_qtytotal = ? ORDER BY fld_recid DESC;
  24.  
  25. -- RECORD RESIBO
  26. -- KUKUNIN NUMBER NG RESIBO
  27. -- RECORD BINILI
  28.  
  29.  
  30. -- para sa group 1 and 4 sample
  31. CREATE TABLE tbl_g14_product_in(
  32.     pIN_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
  33.   ,fld_id INT NOT NULL
  34.   ,pin_date DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
  35.   ,pin_qty INT NOT NULL
  36.   ,pin_staff VARCHAR(30) NOT NULL
  37. );
  38.  
  39.  
  40. CREATE TABLE tbl_g14_product_out(
  41.   pOUT_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
  42.   ,fld_id INT NOT NULL
  43.   ,pout_date DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL
  44.   ,pout_qty INT NOT NULL
  45.   ,pout_staff VARCHAR(30) NOT NULL
  46.   );
  47.  
  48. -- tbl_infolist is proxy for your main table
  49. -- group 1 fld_num_i is PROXY for your tbl_Product_list quantity_in_stock field
  50. -- kayo na bahala sa date
  51. INSERT INTO tbl_g14_product_in(fld_id ,pin_qty, pin_staff)VALUES(?,?,?,?);
  52. -- group 1 follow up for INSERT INTO tbl_product_in
  53. UPDATE tbl_infolist SET fld_num_i = (SELECT fld_num_i + ? FROM tbl_infolist WHERE fld_id = ? LIMIT 1) WHERE fld_id = ?;
  54.  
  55. --sample in action assuming product id 12 have incoming 20 items
  56. INSERT INTO tbl_g14_product_in(fld_id, pin_qty, pin_staff)VALUES(12,20,'Elbert');
  57. UPDATE tbl_infolist
  58. SET fld_num_i = (SELECT fld_num_i + 20 FROM tbl_infolist WHERE fld_id = 12 LIMIT 1))
  59. WHERE fld_id = 12;
  60. -- end sample
  61.  
  62. -- what about product going out
  63. -- need check of there is available number of items
  64. SELECT fld_num_i FROM tbl_infolist WHERE fld_id = ?;
  65. -- gagawa ka ng if statement sa java if fld_num_i > pout_qty
  66. -- di gagana ang out kung mag ne negative ang quantity
  67. -- kayo na bahala sa date
  68. INSERT INTO tbl_g14_product_out(fld_id , pout_qty, pout_staff)VALUES(?,?,?);
  69.  
  70. --Methods-------------------------------------------
  71. package weekDay27;
  72.  
  73. import java.sql.Connection;
  74. import java.sql.DriverManager;
  75. import java.sql.PreparedStatement;
  76. import java.sql.ResultSet;
  77. import javax.swing.JTable;
  78. import javax.swing.table.DefaultTableModel;
  79.  
  80.  
  81. public class Day27G23Methods {
  82.     private String address = "jdbc:mysql://localhost:3306/db_mng_b8";
  83.     private String userName = "root";
  84.     private String passWord = "";
  85.    
  86.     public void getAllRows(JTable tableIn) {
  87.         DefaultTableModel tblModel = (DefaultTableModel) tableIn.getModel();
  88.         tblModel.setRowCount(0);
  89.         String sqlQuery = "SELECT * FROM tbl_infolist;";
  90.        
  91.         try {
  92.             Connection conn = DriverManager.getConnection(address, userName, passWord);
  93.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  94.            
  95.             ResultSet rs = stmt.executeQuery();
  96.             //result set starts with 1 or field name
  97.             while (rs.next()) {
  98.                 Object[] newRow = {
  99.                     rs.getInt(1)
  100.                     ,rs.getString(2)
  101.                     ,rs.getString(3)
  102.                     ,rs.getInt(4)
  103.                     ,rs.getDouble(5)
  104.                     ,rs.getString(6)
  105.                     ,rs.getString(7)};
  106.                 tblModel.addRow(newRow);
  107.             }
  108.             conn.close();
  109.         } catch (Exception e) {
  110.             Object[] error = new Object[7];
  111.             error[0] = "Connection error:\n" + e.getMessage();
  112.             tblModel.addRow(error);
  113.         }
  114.     }
  115.    
  116. //-=-=-=-=-=-=-=-=-=-=-=
  117.    
  118.     public int addNewOrder(int prodID, double prodPrice, int prodQty
  119.             , double prodTotal, int recID){
  120.         int rowsAffected = 0;
  121.         String sqlQuery = "INSERT INTO tbl_g23_orderlst(fld_id, fld_curr_price"
  122.                 + ", fld_or_qty, fld_or_qtytotal, fld_recid) VALUES (?,?,?,?,?);";
  123.        
  124.         try {
  125.             Connection conn = DriverManager.getConnection(address, userName, passWord);
  126.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  127.            
  128.             stmt.setInt(1, prodID);
  129.             stmt.setDouble(2, prodPrice);
  130.             stmt.setInt(3, prodQty);
  131.             stmt.setDouble(4, prodTotal);
  132.             stmt.setInt(5, recID);
  133.            
  134.             rowsAffected = stmt.executeUpdate();
  135.            
  136.             conn.close();
  137.         } catch (Exception e) {
  138.             rowsAffected = -1;
  139.             System.out.println(e.getMessage());
  140.         }
  141.        
  142.         return rowsAffected;
  143.     }
  144.    
  145. //-=-=-=-=-=-=-=-=-=-=-=
  146.    
  147.     public int addNewReciept(int rcQty, double rcTotal){
  148.         int rowsAffected = 0;
  149.         String sqlQuery = "INSERT INTO tbl_g23_reciept(fld_rec_qty, fld_rec_qtytotal) VALUE (?,?);";
  150.        
  151.         try {
  152.             Connection conn = DriverManager.getConnection(address, userName, passWord);
  153.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  154.            
  155.             stmt.setInt(1, rcQty);
  156.             stmt.setDouble(2, rcTotal);
  157.            
  158.             rowsAffected = stmt.executeUpdate();
  159.            
  160.             conn.close();
  161.         } catch (Exception e) {
  162.             rowsAffected = -1;
  163.             System.out.println(e.getMessage());
  164.         }
  165.        
  166.         return rowsAffected;
  167.     }
  168.    
  169.     public int getRecieptID(int rcQty, double rcTotal) {
  170.         int rcID = 0;
  171.         String sqlQuery = "SELECT fld_recid FROM tbl_g23_reciept WHERE fld_rec_qty = ? AND fld_rec_qtytotal = ? ORDER BY fld_recid DESC;";
  172.         // kukuha ng kelangan
  173.         try {
  174.             Connection conn = DriverManager.getConnection(address, userName, passWord);
  175.             PreparedStatement stmt = conn.prepareStatement(sqlQuery);
  176.            
  177.             stmt.setInt(1, rcQty);
  178.             stmt.setDouble(2, rcTotal);
  179.            
  180.             ResultSet rs = stmt.executeQuery();
  181.            
  182.             rs.next();
  183.            
  184.             rcID = rs.getInt(1);
  185.            
  186.             conn.close();
  187.         } catch (Exception e) {
  188. //            rowsAffected = -1;
  189.             System.out.println(e.getMessage());
  190.         }
  191.        
  192.         return rcID;
  193.  
  194.     }
  195. }
  196. --events---------------------------------------
  197. private void btnAddToOrderActionPerformed(java.awt.event.ActionEvent evt) {                                              
  198.         // Taking inspiration from Day22AForm
  199.         Object[] forTblPOut = new Object[5];
  200.         forTblPOut[0] = tblProductList.getValueAt(tblProductList.getSelectedRow(), 0);
  201.         forTblPOut[1] = tblProductList.getValueAt(tblProductList.getSelectedRow(), 4);
  202.         forTblPOut[2] = Integer.parseInt(txtQty.getText());
  203.         Double orTotal = Double.parseDouble(txtQty.getText())
  204.                 * Double.parseDouble(tblProductList.getValueAt(tblProductList.getSelectedRow(), 4).toString());
  205.        
  206.         forTblPOut[3] = String.format("%.4f", orTotal);
  207.        
  208.         DefaultTableModel tblModel = (DefaultTableModel) tblForOrderRecord.getModel();
  209.         tblModel.addRow(forTblPOut);
  210.        
  211.         double totalReceipt = 0;
  212.         int itemSold = 0;
  213.         String printout="";
  214.         for(int i = 0; i < tblForOrderRecord.getRowCount(); i++){
  215. //        JOptionPane.showMessageDialog(this, "awooo");
  216.             itemSold+= Integer.parseInt(tblForOrderRecord.getValueAt(i, 2).toString());
  217.             totalReceipt += Double.parseDouble(tblForOrderRecord.getValueAt(i, 3).toString());
  218.         }
  219.         printout += "Items Sold: " + itemSold;
  220.         printout += "\nTotal Amount: " + totalReceipt;
  221.         JOptionPane.showMessageDialog(this, printout);
  222.         txtTotalQty.setText(String.valueOf(itemSold));
  223.         txtTotalPrice.setText(String.format("%.4f",totalReceipt));
  224.     }                                            
  225.  
  226.     private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
  227.         Day27G23Methods callG23Methods = new Day27G23Methods();
  228.         callG23Methods.getAllRows(tblProductList);
  229.     }                                
  230.  
  231.     private void btnCheckOutActionPerformed(java.awt.event.ActionEvent evt) {                                            
  232.         int rcQty = Integer.parseInt(txtTotalQty.getText());
  233.         double rcTotal = Double.parseDouble(txtTotalPrice.getText());
  234.         int receiptNumber = 0;
  235.         Day27G23Methods callG23Methods = new Day27G23Methods();
  236.        
  237.         int result= 0;
  238.         result = callG23Methods.addNewReciept(rcQty, rcTotal);
  239.         if (result == 1) {
  240.             receiptNumber = callG23Methods.getRecieptID(rcQty, rcTotal);
  241.             txtRecieptNum.setText(String.valueOf(receiptNumber));
  242.             jTextArea1.setText(jTextArea1 + "\n-reciept " + receiptNumber);
  243.         }
  244.             JOptionPane.showMessageDialog(this, "reciept " + receiptNumber);
  245.         if (receiptNumber > 0) {
  246.             for(int i = 0; i < tblForOrderRecord.getRowCount(); i++){
  247.     //        JOptionPane.showMessageDialog(this, "awooo");
  248.                 int currentPID = Integer.parseInt(tblForOrderRecord.getValueAt(i, 0).toString());
  249.                 double currentPrice = Double.parseDouble(tblForOrderRecord.getValueAt(i, 1).toString());  
  250.                 int currentQTY = Integer.parseInt(tblForOrderRecord.getValueAt(i, 2).toString());
  251.                 double currentTotal = Double.parseDouble(tblForOrderRecord.getValueAt(i, 3).toString());
  252.                 int orderRec = callG23Methods.addNewOrder(currentPID, currentPrice, currentQTY, currentTotal, receiptNumber);
  253.                 if (orderRec == 1) {
  254.                     jTextArea1.setText(jTextArea1.getText() + "\n-recorded " + i);
  255.                 }
  256.             }
  257.         }
  258.     }
Advertisement
Add Comment
Please, Sign In to add comment