Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * 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 a_project;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.Date;
- import java.util.*;
- import java.util.ArrayList;
- import java.sql.SQLException;
- import java.sql.Timestamp;
- import javax.swing.table.DefaultTableModel;
- import javax.swing.JOptionPane;
- import java.awt.Color;
- import java.awt.image.BufferedImage;
- import java.awt.Image;
- import java.io.*;
- import javax.imageio.ImageIO;
- import java.net.URL;
- /**
- *
- * @author brenn
- */
- public class NewJFrame extends javax.swing.JFrame {
- /**
- * Creates new form NewJFrame
- */
- public NewJFrame() {
- initComponents();
- fillMenu();
- }
- /**
- * 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() {
- jComboBox1 = new javax.swing.JComboBox<>();
- jTextField1 = new javax.swing.JTextField();
- jButton1 = new javax.swing.JButton();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setBackground(new java.awt.Color(102, 102, 102));
- setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- setMaximumSize(new java.awt.Dimension(350, 180));
- setMinimumSize(new java.awt.Dimension(350, 180));
- setPreferredSize(new java.awt.Dimension(350, 180));
- setResizable(false);
- jComboBox1.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
- jButton1.setText("Submit");
- 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(20, 20, 20)
- .addComponent(jComboBox1, 0, 167, Short.MAX_VALUE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jButton1)
- .addGap(32, 32, 32))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(62, 62, 62)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jButton1))
- .addContainerGap(37, Short.MAX_VALUE))
- );
- pack();
- }// </editor-fold>
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- // TODO add your handling code here:
- Connection con = null;
- PreparedStatement pstmt = null;
- String user = "USERNAME";
- String pass = "PASSWORD";
- String url = "jdbc:db2://localhost:55000/";
- String sql = "";
- Boolean order_more = false;
- String quantityStr = jTextField1.getText();
- int quantity = 0;
- String selected_item = jComboBox1.getSelectedItem().toString();
- try
- {
- quantity = Integer.parseInt(quantityStr);
- sql = "SELECT R.INV_QTY_NEEDED * " + quantity + " - I.INV_QTY_ONHAND, I.INV_NAME, I.INV_REORDER_PRICE, I.INV_REORDER_AMT" //CHECKING IF INVENTORY IS AVAILABLE, EMPTY IF ALL AVAILABLE
- + " FROM RECIPE R"
- + " JOIN INVENTORY I"
- + " ON R.INV_ID = I.INV_ID"
- + " JOIN BAKERY_ITEMS B"
- + " ON B.BAKERY_ITEM_ID = R.BAKERY_ITEM_ID"
- + " WHERE R.INV_QTY_NEEDED * " + quantity + " > I.INV_QTY_ONHAND AND R.BAKERY_ITEM_ID = (SELECT BAKERY_ITEM_ID FROM BAKERY_ITEMS WHERE BAKERY_ITEM_NAME LIKE ('" + selected_item + "') GROUP BY BAKERY_ITEM_ID)";
- con = DriverManager.getConnection(url, user, pass);
- con.setAutoCommit(false);
- //System.out.println(sql);
- ResultSet rs = null;
- ArrayList<Integer> needed = new ArrayList<Integer>();
- ArrayList<String> names = new ArrayList<String>();
- ArrayList<Double> prices = new ArrayList<Double>();
- ArrayList<Integer> reOrderAmt = new ArrayList<Integer>();
- pstmt = con.prepareStatement(sql);
- rs = pstmt.executeQuery();
- if(rs != null)
- {
- int counter = 0;
- while (rs.next())
- {
- needed.add(Integer.parseInt(rs.getString(1)));
- names.add(rs.getString(2));
- prices.add(Double.parseDouble(rs.getString(3)));
- reOrderAmt.add(Integer.parseInt(rs.getString(4)));
- counter++;
- }
- if(counter == 0) //There WAS enough inventory available, attempt transaction. -----------------------
- {
- java.util.Date date = new java.util.Date();
- java.sql.Date curDate = new java.sql.Date(date.getTime());
- sql = "INSERT INTO ORDERS (ORDER_DATE, BAKERY_ITEM_ID, ORDER_QUANTITY) "
- + "VALUES (DATE("+curDate+"), (SELECT BAKERY_ITEM_ID FROM BAKERY_ITEMS WHERE BAKERY_ITEM_NAME LIKE('"+selected_item+"') GROUP BY BAKERY_ITEM_ID), "+quantity+")"
- ;//+ " UNION "
- pstmt = con.prepareStatement(sql);
- pstmt.executeUpdate();
- java.sql.Timestamp myTStamp = new java.sql.Timestamp(System.currentTimeMillis());
- sql = "INSERT INTO TRANSACTION_JOURNAL (JOURNAL_DESCRIPTION, JOURNAL_AMOUNT, JOURNAL_TIME)"
- + " VALUES "
- + "('SUCCESSFUL ORDER OF ("+selected_item+") x("+quantity+")', "
- + quantity + " * (SELECT BAKERY_ITEM_PRICE FROM BAKERY_ITEMS WHERE BAKERY_ITEM_NAME LIKE('"+selected_item+"') GROUP BY BAKERY_ITEM_PRICE), "
- + "'" + myTStamp + "')";
- pstmt = con.prepareStatement(sql);
- pstmt.executeUpdate();
- jTextField1.setText("");
- JOptionPane.showMessageDialog(null, "Successfully ordered " + selected_item + " x " + quantity + ".", "Notice", JOptionPane.WARNING_MESSAGE);
- con.commit();
- }
- else //There WASN'T enough inventory available, order more inventory. ---------------------
- {
- java.sql.Timestamp myTStamp = new java.sql.Timestamp(System.currentTimeMillis());
- int orderCount = 0;
- double num = 0.0;
- for(int i = 0; i < needed.size(); i++)
- {
- orderCount = (int)(needed.get(i) / reOrderAmt.get(i));
- num = orderCount * prices.get(i);
- num = -num;
- sql = "INSERT INTO TRANSACTION_JOURNAL (JOURNAL_DESCRIPTION, JOURNAL_AMOUNT, JOURNAL_TIME)"
- + " VALUES "
- + "('ORDERING INVENTORY ITEM " + names.get(i) + " WITH A QUANTITY OF " + orderCount + "', "
- + num + ", "//"-" + needed.get(i) * prices.get(i) + ", "
- + "'" + myTStamp + "')";
- System.out.println("Entry " + (i + 1) + " out of " + needed.size());
- pstmt = con.prepareStatement(sql);
- pstmt.executeUpdate();
- }
- sql = "SELECT SUM(JOURNAL_AMOUNT)"
- + " FROM TRANSACTION_JOURNAL";
- pstmt = con.prepareStatement(sql);
- rs = pstmt.executeQuery();
- if(rs != null)
- {
- num = 0.0;
- rs.next();
- num = Double.parseDouble(rs.getString(1));
- if(num < 0)
- {
- jTextField1.setText("");
- JOptionPane.showMessageDialog(null, "Order failed: Money don't grow on trees (AKA Insufficient Funds).", "Notice", JOptionPane.WARNING_MESSAGE);
- con.rollback();
- }
- else
- {
- jTextField1.setText("");
- JOptionPane.showMessageDialog(null, "Insufficient inventory, more inventory has been ordered.", "Notice", JOptionPane.WARNING_MESSAGE);
- con.commit();
- }
- }
- }
- }
- }
- catch(Exception ex)
- {
- jTextField1.setText("");
- JOptionPane.showMessageDialog(null, "Please enter a number value.", "Notice", JOptionPane.WARNING_MESSAGE);
- }
- try
- {
- if(con != null)
- {
- con.close();
- }
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- /**
- * @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(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(NewJFrame.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 NewJFrame().setVisible(true);
- }
- });
- }
- private void fillMenu()
- {
- Connection con = null;
- PreparedStatement pstmt = null;
- String user = "USERNAME";
- String pass = "PASSWORD";
- String url = "jdbc:db2://localhost:55000/";
- String sql = "";
- try
- {
- Class.forName("com.ibm.db2.jcc.DB2Driver");
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- try
- {
- con = DriverManager.getConnection(url, user, pass);
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- return;
- }
- sql = "SELECT BAKERY_ITEM_NAME"
- + " FROM BAKERY_ITEMS";
- ResultSet rs = null;
- try
- {
- pstmt = con.prepareStatement(sql);
- rs = pstmt.executeQuery();
- if(rs != null)
- {
- String item_name = "";
- ArrayList<String> items = new ArrayList<String>();
- int counter = 0;
- while (rs.next()){
- item_name = rs.getString(1);
- items.add(item_name);
- counter++;
- }
- jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(items.toArray()));
- }
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- try
- {
- con.close();
- }
- catch (Exception ex)
- {
- ex.printStackTrace();
- }
- }
- // Variables declaration - do not modify
- private javax.swing.JButton jButton1;
- private javax.swing.JComboBox<String> jComboBox1;
- private javax.swing.JTextField jTextField1;
- // End of variables declaration
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement