PageRenderTime 45ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/src/inventory_system/itemAdd.java

https://gitlab.com/imughal/invent
Java | 318 lines | 239 code | 40 blank | 39 comment | 7 complexity | 36a6fbaf46632843a05fd6952544525e MD5 | raw file
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package inventory_system;
  7. import java.sql.*;
  8. import java.util.ArrayList;
  9. import javax.swing.*;
  10. /**
  11. *
  12. * @author imran
  13. */
  14. public class itemAdd extends javax.swing.JDialog {
  15. /**
  16. * Creates new form itemAdd
  17. */
  18. public itemAdd() {
  19. super(new javax.swing.JFrame(), true);
  20. initComponents();
  21. }
  22. /**
  23. * This method is called from within the constructor to initialize the form.
  24. * WARNING: Do NOT modify this code. The content of this method is always
  25. * regenerated by the Form Editor.
  26. */
  27. @SuppressWarnings("unchecked")
  28. // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  29. private void initComponents() {
  30. cmbItem = new javax.swing.JComboBox<>();
  31. jLabel1 = new javax.swing.JLabel();
  32. jLabel2 = new javax.swing.JLabel();
  33. tAvail = new javax.swing.JLabel();
  34. jLabel4 = new javax.swing.JLabel();
  35. SpinQty = new javax.swing.JSpinner();
  36. jLabel5 = new javax.swing.JLabel();
  37. jButton1 = new javax.swing.JButton();
  38. jButton2 = new javax.swing.JButton();
  39. jLabel6 = new javax.swing.JLabel();
  40. tAmount = new javax.swing.JLabel();
  41. unitPrice = new javax.swing.JSpinner();
  42. setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
  43. cmbItem.addItemListener(new java.awt.event.ItemListener() {
  44. public void itemStateChanged(java.awt.event.ItemEvent evt) {
  45. cmbItemItemStateChanged(evt);
  46. }
  47. });
  48. jLabel1.setText("item");
  49. jLabel2.setText("Total Availale:");
  50. tAvail.setText("jLabel3");
  51. jLabel4.setText("Quantity");
  52. SpinQty.setModel(new javax.swing.SpinnerNumberModel(0, 0, null, 1));
  53. SpinQty.addChangeListener(new javax.swing.event.ChangeListener() {
  54. public void stateChanged(javax.swing.event.ChangeEvent evt) {
  55. SpinQtyStateChanged(evt);
  56. }
  57. });
  58. jLabel5.setText("Unit Price");
  59. jButton1.setText("Add");
  60. jButton1.addActionListener(new java.awt.event.ActionListener() {
  61. public void actionPerformed(java.awt.event.ActionEvent evt) {
  62. jButton1ActionPerformed(evt);
  63. }
  64. });
  65. jButton2.setText("Cancel");
  66. jButton2.addActionListener(new java.awt.event.ActionListener() {
  67. public void actionPerformed(java.awt.event.ActionEvent evt) {
  68. jButton2ActionPerformed(evt);
  69. }
  70. });
  71. jLabel6.setText("Total Amount:");
  72. tAmount.setText("0");
  73. unitPrice.setModel(new javax.swing.SpinnerNumberModel(0, 0, null, 1));
  74. unitPrice.addChangeListener(new javax.swing.event.ChangeListener() {
  75. public void stateChanged(javax.swing.event.ChangeEvent evt) {
  76. unitPriceStateChanged(evt);
  77. }
  78. });
  79. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  80. getContentPane().setLayout(layout);
  81. layout.setHorizontalGroup(
  82. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  83. .addGroup(layout.createSequentialGroup()
  84. .addContainerGap()
  85. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  86. .addGroup(layout.createSequentialGroup()
  87. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  88. .addComponent(jLabel4)
  89. .addComponent(jLabel5)
  90. .addComponent(jLabel1))
  91. .addGap(18, 18, 18)
  92. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  93. .addComponent(cmbItem, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  94. .addComponent(SpinQty, javax.swing.GroupLayout.DEFAULT_SIZE, 62, Short.MAX_VALUE)
  95. .addComponent(unitPrice)))
  96. .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  97. .addGap(18, 18, 18)
  98. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  99. .addGroup(layout.createSequentialGroup()
  100. .addComponent(jLabel2)
  101. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  102. .addComponent(tAvail))
  103. .addGroup(layout.createSequentialGroup()
  104. .addComponent(jLabel6)
  105. .addGap(18, 18, Short.MAX_VALUE)
  106. .addComponent(tAmount))
  107. .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  108. .addGap(29, 29, 29))
  109. );
  110. layout.setVerticalGroup(
  111. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  112. .addGroup(layout.createSequentialGroup()
  113. .addContainerGap()
  114. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  115. .addComponent(cmbItem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  116. .addComponent(jLabel1)
  117. .addComponent(jLabel2)
  118. .addComponent(tAvail))
  119. .addGap(18, 18, 18)
  120. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  121. .addComponent(jLabel4)
  122. .addComponent(SpinQty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  123. .addGap(18, 18, 18)
  124. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  125. .addComponent(jLabel5)
  126. .addComponent(jLabel6)
  127. .addComponent(tAmount)
  128. .addComponent(unitPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  129. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
  130. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  131. .addComponent(jButton1)
  132. .addComponent(jButton2))
  133. .addContainerGap())
  134. );
  135. pack();
  136. }// </editor-fold>//GEN-END:initComponents
  137. int row = 0;
  138. public ArrayList<Object> adding(String cat_id, int rows) {
  139. loading(cat_id);
  140. row = rows + 1;
  141. this.setVisible(true);
  142. return rList;
  143. }
  144. ArrayList<String> qtys = new ArrayList<>();
  145. ArrayList<String> item_ids = new ArrayList<>();
  146. ArrayList<String> itemName = new ArrayList<>();
  147. ArrayList<Object> rList = new ArrayList<>();
  148. private void loading(String cat_id) {
  149. try {
  150. try (Connection con = databaseCon.getConn()) {
  151. String sql = "SELECT * From invitems where cat_id = ?";
  152. PreparedStatement st = con.prepareStatement(sql);
  153. st.setString(1, cat_id);
  154. ResultSet rs = st.executeQuery();
  155. while (rs.next()) {
  156. qtys.add(rs.getString("qty"));
  157. item_ids.add(rs.getString("item_id"));
  158. //cmbItem.addItem(rs.getString("item"));
  159. itemName.add(rs.getString("item"));
  160. }
  161. con.close();
  162. }
  163. for (String p : itemName) {
  164. cmbItem.addItem(p);
  165. }
  166. } catch (Exception e) {
  167. JOptionPane.showMessageDialog(null, e);
  168. }
  169. }
  170. private void SpinQtyStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_SpinQtyStateChanged
  171. int item_qty = (Integer) SpinQty.getValue();
  172. int item_price = (Integer) unitPrice.getValue();
  173. String total_amount = Integer.toString(item_price * item_qty);
  174. tAmount.setText(total_amount);
  175. }//GEN-LAST:event_SpinQtyStateChanged
  176. private void unitPriceStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_unitPriceStateChanged
  177. int item_qty = (Integer) SpinQty.getValue();
  178. int item_price = (Integer) unitPrice.getValue();
  179. String total_amount = Integer.toString(item_price * item_qty);
  180. tAmount.setText(total_amount);
  181. }//GEN-LAST:event_unitPriceStateChanged
  182. private void cmbItemItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbItemItemStateChanged
  183. //int qtyo = qtys.indexOf(cmbItem.getSelectedIndex());
  184. tAvail.setText(qtys.get(cmbItem.getSelectedIndex()));
  185. // getSelectedData(item_ids.get(cmbItem.getSelectedIndex()));
  186. //JOptionPane.showMessageDialog(null, cmbItem.getSelectedIndex());
  187. }//GEN-LAST:event_cmbItemItemStateChanged
  188. private void getSelectedData(String id){
  189. try {
  190. try (Connection con = databaseCon.getConn()) {
  191. String sql = "SELECT * From invitems where item_id = ?";
  192. PreparedStatement st = con.prepareStatement(sql);
  193. st.setString(1, id);
  194. ResultSet rs = st.executeQuery();
  195. while (rs.next()) {
  196. }
  197. con.close();
  198. }
  199. } catch (Exception e) {
  200. JOptionPane.showMessageDialog(null, e);
  201. }
  202. }
  203. private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
  204. int avail = Integer.parseInt(tAvail.getText());
  205. if ((int) SpinQty.getValue() > avail) {
  206. JOptionPane.showMessageDialog(null, "Error, Quantity Out of Stock");
  207. } else {
  208. // rList.add(row);
  209. // rList.add((String) cmbItem.getSelectedItem());
  210. // rList.add(SpinQty.getValue().toString());
  211. // rList.add(unitPrice.getValue().toString());
  212. // rList.add(tAmount.getText());
  213. rList.add(item_ids.get(cmbItem.getSelectedIndex()));
  214. rList.add((Integer) SpinQty.getValue());
  215. rList.add((Integer) unitPrice.getValue());
  216. this.setVisible(false);
  217. this.dispose();
  218. }
  219. }//GEN-LAST:event_jButton1ActionPerformed
  220. private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
  221. this.setVisible(false);
  222. this.dispose();
  223. }//GEN-LAST:event_jButton2ActionPerformed
  224. /**
  225. * @param args the command line arguments
  226. */
  227. public static void main(String args[]) {
  228. /* Set the Nimbus look and feel */
  229. //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  230. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  231. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  232. */
  233. try {
  234. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  235. if ("Nimbus".equals(info.getName())) {
  236. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  237. break;
  238. }
  239. }
  240. } catch (ClassNotFoundException ex) {
  241. java.util.logging.Logger.getLogger(itemAdd.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  242. } catch (InstantiationException ex) {
  243. java.util.logging.Logger.getLogger(itemAdd.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  244. } catch (IllegalAccessException ex) {
  245. java.util.logging.Logger.getLogger(itemAdd.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  246. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  247. java.util.logging.Logger.getLogger(itemAdd.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  248. }
  249. //</editor-fold>
  250. /* Create and display the dialog */
  251. java.awt.EventQueue.invokeLater(new Runnable() {
  252. public void run() {
  253. itemAdd dialog = new itemAdd();
  254. dialog.addWindowListener(new java.awt.event.WindowAdapter() {
  255. @Override
  256. public void windowClosing(java.awt.event.WindowEvent e) {
  257. System.exit(0);
  258. }
  259. });
  260. dialog.setVisible(true);
  261. }
  262. });
  263. }
  264. // Variables declaration - do not modify//GEN-BEGIN:variables
  265. private javax.swing.JSpinner SpinQty;
  266. private javax.swing.JComboBox<String> cmbItem;
  267. private javax.swing.JButton jButton1;
  268. private javax.swing.JButton jButton2;
  269. private javax.swing.JLabel jLabel1;
  270. private javax.swing.JLabel jLabel2;
  271. private javax.swing.JLabel jLabel4;
  272. private javax.swing.JLabel jLabel5;
  273. private javax.swing.JLabel jLabel6;
  274. private javax.swing.JLabel tAmount;
  275. private javax.swing.JLabel tAvail;
  276. private javax.swing.JSpinner unitPrice;
  277. // End of variables declaration//GEN-END:variables
  278. }