private void populateTable() { while (tableModel.getRowCount() > 0) { tableModel.removeRow(0); } products = model.getProducts(); for (Products product : products) { tableModel.addRow( new Object[] { String.valueOf(product.getProductID()), product.getAddmotoCode(), productLineMap.get(product.getProductLineID()), (product.getDescription() + " " + product.getCharacteristics() + " " + product.getMotors()) .replaceAll("\\s+", " ") .trim(), "PhP " + Formatter.format(product.getUnitPrice()), "PhP " + Formatter.format(product.getSellingPrice()) }); } }
private void showProductDetails(int ID) { List<Products> productWithID = products.stream().filter((p) -> p.getProductID() == ID).collect(Collectors.toList()); List<Supplier> supplierWithID = suppliers .stream() .filter((p) -> p.getSupplierID() == p.getSupplierID()) .collect(Collectors.toList()); Products product = productWithID.get(0); Supplier supplier = supplierWithID.get(0); String productName_ = (productLineMap.get(product.getProductLineID()) + " " + product.getDescription() + " " + product.getCharacteristics() + " " + product.getMotors()) .replaceAll("\\s+", " "); String productDescription_ = (product.getDescription() + " " + product.getCharacteristics() + " " + product.getMotors()) .replaceAll("\\s+", " "); iProductName.setText(productName_); iItemNumber.setText(String.valueOf(product.getProductID())); iAddMotoCode.setText(product.getAddmotoCode()); iProductLine.setText(productLineMap.get(product.getProductLineID())); iItemDescription.setText(productDescription_); iSellingPrice.setText(Formatter.format(product.getSellingPrice())); iUnitCost.setText(Formatter.format(product.getUnitPrice())); iOnHandQuantity.setText(String.valueOf(product.getCurrentQuantity())); iQtyThreshold.setText(String.valueOf(product.getThresholdCount())); iStatus.setText("Not Yet Implemented"); iSupplier.setText(supplier.getSupplierName()); iEditUpdateRSP.setEnabled(true); iEditUpdateUC.setEnabled(true); iEditUpdateQT.setEnabled(true); }
@Override public void setListeners() { iSearchField.addKeyListener( new KeyListener() { @Override public void keyTyped(KeyEvent e) {} @Override public void keyPressed(KeyEvent e) {} @Override public void keyReleased(KeyEvent e) { isAddingRows = true; iSearchField.setText(Formatter.makeUpperCase(iSearchField.getText())); String input = iSearchField.getText(); List<Products> productWithID = products .stream() .filter( (p) -> (p.getAddmotoCode().toUpperCase().contains(input.toUpperCase()) || (p.getDescription() + " " + p.getCharacteristics() + p.getMotors()) .replaceAll("\\s+", " ") .trim() .toUpperCase() .contains(input.toUpperCase())) || getProductLineName(p.getProductLineID()) .toUpperCase() .contains(input.toUpperCase())) .collect(Collectors.toList()); while (tableModel.getRowCount() > 0) { tableModel.removeRow(0); } for (Products product : productWithID) { tableModel.addRow( new Object[] { String.valueOf(product.getProductID()), product.getAddmotoCode(), productLineMap.get(product.getProductLineID()), (product.getDescription() + " " + product.getCharacteristics() + " " + product.getMotors()) .replaceAll("\\s+", " ") .trim(), "PhP " + Formatter.format(product.getUnitPrice()), "PhP " + Formatter.format(product.getSellingPrice()) }); } isAddingRows = false; } }); iAddNewPLine.addActionListener( new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String newProductLine = JOptionPane.showInputDialog( null, "Product Line Name:", "New Product Line", JOptionPane.QUESTION_MESSAGE); try { if (newProductLine.equals("")) { JOptionPane.showMessageDialog( null, "You cannot create an empty product line name.", "Error", JOptionPane.ERROR_MESSAGE); } else { int rows = model.insertNewProductLine(newProductLine); if (rows != 0) { JOptionPane.showMessageDialog( null, "Product Line list updated.", "Success", JOptionPane.INFORMATION_MESSAGE); populate(); } else { JOptionPane.showMessageDialog( null, "Product Line may be existing already!", "Error", JOptionPane.ERROR_MESSAGE); } } } catch (Exception ex) { } } }); iShowHistory.addMouseListener( new MouseListener() { @Override public void mouseClicked(MouseEvent e) { JOptionPane.showMessageDialog(view, "Clicked"); } @Override public void mousePressed(MouseEvent e) {} @Override public void mouseReleased(MouseEvent e) {} @Override public void mouseEntered(MouseEvent e) { setRedForeground(iShowHistory); } @Override public void mouseExited(MouseEvent e) { setWhiteForeground(iShowHistory); } }); iSellingPrice.addFocusListener( new FocusListener() { @Override public void focusGained(FocusEvent e) { iSellingPrice.selectAll(); } @Override public void focusLost(FocusEvent e) {} }); iSellingPrice.addKeyListener( new KeyListener() { @Override public void keyTyped(KeyEvent e) { char ch = e.getKeyChar(); if (!((ch >= '0') && (ch <= '9') || (ch == KeyEvent.VK_BACK_SPACE) || (ch == KeyEvent.VK_DELETE) || (ch == '.'))) { tk.beep(); e.consume(); } } @Override public void keyPressed(KeyEvent e) {} @Override public void keyReleased(KeyEvent e) {} }); iUnitCost.addFocusListener( new FocusListener() { @Override public void focusGained(FocusEvent e) { iUnitCost.selectAll(); } @Override public void focusLost(FocusEvent e) {} }); iUnitCost.addKeyListener( new KeyListener() { @Override public void keyTyped(KeyEvent e) { char ch = e.getKeyChar(); if (!((ch >= '0') && (ch <= '9') || (ch == KeyEvent.VK_BACK_SPACE) || (ch == KeyEvent.VK_DELETE) || (ch == '.'))) { tk.beep(); e.consume(); } } @Override public void keyPressed(KeyEvent e) {} @Override public void keyReleased(KeyEvent e) {} }); iQtyThreshold.addFocusListener( new FocusListener() { @Override public void focusGained(FocusEvent e) { iQtyThreshold.selectAll(); } @Override public void focusLost(FocusEvent e) {} }); iSellingPrice.addKeyListener( new KeyListener() { @Override public void keyTyped(KeyEvent e) { char ch = e.getKeyChar(); if (!((ch >= '0') && (ch <= '9') || (ch == KeyEvent.VK_BACK_SPACE) || (ch == KeyEvent.VK_DELETE) || (ch == '.'))) { tk.beep(); e.consume(); } else if ((ch == '.' && iSellingPrice.getText().contains(".")) || (ch == '.' && iSellingPrice.getText().equals(""))) { tk.beep(); e.consume(); } } @Override public void keyPressed(KeyEvent e) {} @Override public void keyReleased(KeyEvent e) {} }); iQtyThreshold.addKeyListener( new KeyListener() { @Override public void keyTyped(KeyEvent e) { char ch = e.getKeyChar(); if (!((ch >= '0') && (ch <= '9') || (ch == KeyEvent.VK_BACK_SPACE) || (ch == KeyEvent.VK_DELETE))) { tk.beep(); e.consume(); } } @Override public void keyPressed(KeyEvent e) {} @Override public void keyReleased(KeyEvent e) {} }); iEditUpdateRSP.addMouseListener( new MouseListener() { @Override public void mouseClicked(MouseEvent e) { if (!iAddMotoCode.getText().equals("")) { if (iEditUpdateRSP.getText().equals("Edit")) { sellingPriceValue = iSellingPrice.getText(); setToDefault(iSellingPrice, iEditUpdateRSP, iCancelRSP, false); } else if (iEditUpdateRSP.getText().equals("Update")) { if (iSellingPrice.getText().equals("") || iUnitCost.getText().equals("") || iSellingPrice.getText().split("\\.")[1].length() > 2 || iUnitCost.getText().split("\\.")[1].length() > 2) { JOptionPane.showMessageDialog( null, "There are empty fields or number format is invalid", "Error", JOptionPane.ERROR_MESSAGE); iSellingPrice.setText(String.valueOf(sellingPriceValue)); } else { double sellingPrice = Double.parseDouble(iSellingPrice.getText()); double unitPrice = Double.parseDouble(iUnitCost.getText()); if (sellingPrice >= unitPrice * 1.30) { int option = JOptionPane.showConfirmDialog( null, "Are you sure you want to proceed with the update?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (option == JOptionPane.YES_OPTION) { int itemID = Integer.parseInt(iItemNumber.getText()); int updatedRow = model.updateSellingPrice(itemID, sellingPrice); if (updatedRow != 0) { JOptionPane.showMessageDialog( null, "Product selling price updated!", "Success", JOptionPane.INFORMATION_MESSAGE); setIndexZero(); setDefaultViews(); isAddingRows = true; populate(); isAddingRows = false; } else { JOptionPane.showMessageDialog( null, "There is an error while updating item selling price.", "Error", JOptionPane.ERROR_MESSAGE); } } } else { JOptionPane.showMessageDialog( null, "You must have atleast 30% profit for this item.", "Error", JOptionPane.ERROR_MESSAGE); } } setToDefault(iSellingPrice, iEditUpdateRSP, iCancelRSP, true); } } } @Override public void mousePressed(MouseEvent e) {} @Override public void mouseReleased(MouseEvent e) {} @Override public void mouseEntered(MouseEvent e) { setRedForeground(iEditUpdateRSP); } @Override public void mouseExited(MouseEvent e) { setBlackForeground(iEditUpdateRSP); } }); iUnitCost.addKeyListener( new KeyListener() { @Override public void keyTyped(KeyEvent e) { char ch = e.getKeyChar(); if (!((ch >= '0') && (ch <= '9') || (ch == KeyEvent.VK_BACK_SPACE) || (ch == KeyEvent.VK_DELETE))) { tk.beep(); e.consume(); } else if ((ch == '.' && iUnitCost.getText().contains(".")) || (ch == '.' && iUnitCost.getText().equals(""))) { tk.beep(); e.consume(); } } @Override public void keyPressed(KeyEvent e) {} @Override public void keyReleased(KeyEvent e) {} }); iEditUpdateUC.addMouseListener( new MouseListener() { @Override public void mouseClicked(MouseEvent e) { if (!iAddMotoCode.getText().equals("")) { if (iEditUpdateUC.getText().equals("Edit")) { unitCostValue = iUnitCost.getText(); setToDefault(iUnitCost, iEditUpdateUC, iCancelUC, false); } else if (iEditUpdateUC.getText().equals("Update")) { if (iSellingPrice.getText().equals("") || iUnitCost.getText().equals("") || iSellingPrice.getText().split("\\.")[1].length() > 2 || iUnitCost.getText().split("\\.")[1].length() > 2) { JOptionPane.showMessageDialog( null, "There are empty fields or number format is invalid", "Error", JOptionPane.ERROR_MESSAGE); iUnitCost.setText(String.valueOf(unitCostValue)); } else { double sellingPrice = Double.parseDouble(iSellingPrice.getText()); double unitPrice = Double.parseDouble(iUnitCost.getText()); if (sellingPrice >= unitPrice * 1.30) { int option = JOptionPane.showConfirmDialog( null, "Are you sure you want to proceed with the update?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (option == JOptionPane.YES_OPTION) { int itemID = Integer.parseInt(iItemNumber.getText()); int updatedRow = model.updateUnitPrice(itemID, unitPrice); if (updatedRow != 0) { JOptionPane.showMessageDialog( null, "Product unit price updated!", "Success", JOptionPane.INFORMATION_MESSAGE); setIndexZero(); setDefaultViews(); isAddingRows = true; populate(); isAddingRows = false; } else { JOptionPane.showMessageDialog( null, "There is an error while updating item unit price.", "Error", JOptionPane.ERROR_MESSAGE); } } } else { JOptionPane.showMessageDialog( null, "You must have atleast 30% profit for this item.", "Error", JOptionPane.ERROR_MESSAGE); } } setToDefault(iUnitCost, iEditUpdateUC, iCancelUC, true); } } } @Override public void mousePressed(MouseEvent e) {} @Override public void mouseReleased(MouseEvent e) {} @Override public void mouseEntered(MouseEvent e) { setRedForeground(iEditUpdateUC); } @Override public void mouseExited(MouseEvent e) { setBlackForeground(iEditUpdateUC); } }); iQtyThreshold.addKeyListener( new KeyListener() { @Override public void keyTyped(KeyEvent e) { char ch = e.getKeyChar(); if (!((ch >= '0') && (ch <= '9') || (ch == KeyEvent.VK_BACK_SPACE) || (ch == KeyEvent.VK_DELETE))) { tk.beep(); e.consume(); } } @Override public void keyPressed(KeyEvent e) {} @Override public void keyReleased(KeyEvent e) {} }); iEditUpdateQT.addMouseListener( new MouseListener() { @Override public void mouseClicked(MouseEvent e) { if (!iAddMotoCode.getText().equals("")) { if (iEditUpdateQT.getText().equals("Edit")) { qtyThresholdValue = iQtyThreshold.getText(); setToDefault(iQtyThreshold, iEditUpdateQT, iCancelQT, false); } else if (iEditUpdateQT.getText().equals("Update")) { if (iQtyThreshold.getText().equals("")) { JOptionPane.showMessageDialog( null, "There are empty fields or number format is invalid", "Error", JOptionPane.ERROR_MESSAGE); iQtyThreshold.setText(String.valueOf(qtyThresholdValue)); } else { int threshold = Integer.parseInt(iQtyThreshold.getText()); int option = JOptionPane.showConfirmDialog( null, "Are you sure you want to proceed with the update?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); if (option == JOptionPane.YES_OPTION) { int itemID = Integer.parseInt(iItemNumber.getText()); int updatedRow = model.updateThreshold(itemID, threshold); if (updatedRow != 0) { JOptionPane.showMessageDialog( null, "Product threshold count updated!", "Success", JOptionPane.INFORMATION_MESSAGE); setIndexZero(); setDefaultViews(); isAddingRows = true; populate(); isAddingRows = false; } else { JOptionPane.showMessageDialog( null, "There is an error while updating item threshold count.", "Error", JOptionPane.ERROR_MESSAGE); } } } setToDefault(iQtyThreshold, iEditUpdateQT, iCancelQT, true); } } } @Override public void mousePressed(MouseEvent e) {} @Override public void mouseReleased(MouseEvent e) {} @Override public void mouseEntered(MouseEvent e) { setRedForeground(iEditUpdateQT); } @Override public void mouseExited(MouseEvent e) { setBlackForeground(iEditUpdateQT); } }); iCancelRSP.addMouseListener( new MouseListener() { @Override public void mouseClicked(MouseEvent e) { iSellingPrice.setText(sellingPriceValue); setToDefault(iSellingPrice, iEditUpdateRSP, iCancelRSP, true); } @Override public void mousePressed(MouseEvent e) {} @Override public void mouseReleased(MouseEvent e) {} @Override public void mouseEntered(MouseEvent e) { setRedForeground(iCancelRSP); } @Override public void mouseExited(MouseEvent e) { setBlackForeground(iCancelRSP); } }); iCancelUC.addMouseListener( new MouseListener() { @Override public void mouseClicked(MouseEvent e) { iUnitCost.setText(unitCostValue); setToDefault(iUnitCost, iEditUpdateUC, iCancelUC, true); } @Override public void mousePressed(MouseEvent e) {} @Override public void mouseReleased(MouseEvent e) {} @Override public void mouseEntered(MouseEvent e) { setRedForeground(iCancelUC); } @Override public void mouseExited(MouseEvent e) { setBlackForeground(iCancelUC); } }); iCancelQT.addMouseListener( new MouseListener() { @Override public void mouseClicked(MouseEvent e) { iQtyThreshold.setText(qtyThresholdValue); setToDefault(iQtyThreshold, iEditUpdateQT, iCancelQT, true); } @Override public void mousePressed(MouseEvent e) {} @Override public void mouseReleased(MouseEvent e) {} @Override public void mouseEntered(MouseEvent e) { setRedForeground(iCancelQT); } @Override public void mouseExited(MouseEvent e) { setBlackForeground(iCancelQT); } }); selectionModel.addListSelectionListener( (ListSelectionEvent e) -> { if (e.getValueIsAdjusting()) { return; } if (!isAddingRows) { int row = iProductsTable.getSelectedRow(); int ID = Integer.parseInt((String) iProductsTable.getValueAt(row, 0)); System.out.println("ID -> " + ID); iProductsTable.scrollRowToVisible(row); showProductDetails(ID); } }); iSellingPriceText.addKeyListener( new KeyListener() { @Override public void keyTyped(KeyEvent e) { char ch = e.getKeyChar(); if (!((ch >= '0') && (ch <= '9') || (ch == KeyEvent.VK_BACK_SPACE) || (ch == KeyEvent.VK_DELETE) || (ch == '.'))) { tk.beep(); e.consume(); } else if ((ch == '.' && iSellingPriceText.getText().contains(".")) || (ch == '.' && iSellingPriceText.getText().equals(""))) { tk.beep(); e.consume(); } } @Override public void keyPressed(KeyEvent e) {} @Override public void keyReleased(KeyEvent e) {} }); iUnitPriceText.addKeyListener( new KeyListener() { @Override public void keyTyped(KeyEvent e) { char ch = e.getKeyChar(); if (!((ch >= '0') && (ch <= '9') || (ch == KeyEvent.VK_BACK_SPACE) || (ch == KeyEvent.VK_DELETE) || (ch == '.'))) { tk.beep(); e.consume(); } else if ((ch == '.' && iUnitPriceText.getText().contains(".")) || (ch == '.' && iUnitPriceText.getText().equals(""))) { tk.beep(); e.consume(); } } @Override public void keyPressed(KeyEvent e) {} @Override public void keyReleased(KeyEvent e) {} }); iQtyText.addKeyListener( new KeyListener() { @Override public void keyTyped(KeyEvent e) { char ch = e.getKeyChar(); if (!((ch >= '0') && (ch <= '9') || (ch == KeyEvent.VK_BACK_SPACE) || (ch == KeyEvent.VK_DELETE))) { tk.beep(); e.consume(); } } @Override public void keyPressed(KeyEvent e) {} @Override public void keyReleased(KeyEvent e) {} }); iThresholdText.addKeyListener( new KeyListener() { @Override public void keyTyped(KeyEvent e) { char ch = e.getKeyChar(); if (!((ch >= '0') && (ch <= '9') || (ch == KeyEvent.VK_BACK_SPACE) || (ch == KeyEvent.VK_DELETE))) { tk.beep(); e.consume(); } } @Override public void keyPressed(KeyEvent e) {} @Override public void keyReleased(KeyEvent e) {} }); iAddNew.addActionListener( (ActionEvent e) -> { int choice = JOptionPane.showOptionDialog( view, addItem, "ADD Moto - Motorcycle Parts and Accessories", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null); if (choice == JOptionPane.OK_OPTION) { String supplierName = getFormattedString((String) iSupplierList.getSelectedItem()); String productLine = getFormattedString((String) iProductLineList.getSelectedItem()); String supplierCode = getFormattedString(iSupplierCodeText.getText()); String unitPrice = getFormattedString(iUnitPriceText.getText()); String sellingPrice = getFormattedString(iSellingPriceText.getText()); String qtyOnHand = getFormattedString(iQtyText.getText()); String threshold = getFormattedString(iThresholdText.getText()); String characteristics = getFormattedString(iCharacteristics.getText()); String _model = getFormattedString(iModelText.getText()); String description = getFormattedString(iDescriptionText.getText()); if (supplierName.isEmpty() || productLine.isEmpty() || supplierCode.isEmpty() || unitPrice.isEmpty() || sellingPrice.isEmpty() || qtyOnHand.isEmpty() || threshold.isEmpty() || characteristics.isEmpty() || _model.isEmpty() || description.isEmpty()) { JOptionPane.showMessageDialog( view, "All fields must not be empty!", "Error", JOptionPane.ERROR_MESSAGE); } else { Products product = new Products( productLine.toUpperCase().substring(0, 2) + "AD" + supplierCode, supplierCode, parseInt(qtyOnHand), parseDouble(unitPrice), parseDouble(sellingPrice), parseDouble(sellingPrice) - parseDouble(unitPrice), parseInt(threshold), null, description, characteristics, _model, getKeybyValue_p(productLine), getKeybyValue_s(supplierName)); System.out.println(product.toString()); int returnValue = model.addNewProduct(product); if (returnValue == 0) { JOptionPane.showMessageDialog( view, "There must be some kind of error.", "Error", JOptionPane.ERROR_MESSAGE); } else { JOptionPane.showMessageDialog( view, "Successfully added new product!", "Success", JOptionPane.INFORMATION_MESSAGE); setIndexZero(); setDefaultViews(); isAddingRows = true; repopulateTable(); isAddingRows = false; } } } }); }