Ejemplo n.º 1
0
  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(cancel)) this.dispose();
    else if (e.getSource().equals(save)) {
      String name = "";
      String description = "";
      int purchasePrice = 0;
      int rentalPrice = 0;
      int rentalDuration = 0;
      boolean isRentable = rentable.isSelected();

      try {
        name = nameField.getText();
        description = descriptionField.getText();
        purchasePrice = Integer.valueOf(purchasePriceField.getText());
        rentalPrice = Integer.valueOf(rentalPriceField.getText());
        rentalDuration = Integer.valueOf(rentalDurationField.getText());
      } catch (NumberFormatException ex) {
      }

      if (name.equals("")) {
        JOptionPane.showMessageDialog(
            null, "Invalid name!", "Error updating item type", JOptionPane.ERROR_MESSAGE);
      } else if (description.equals("")) {
        JOptionPane.showMessageDialog(
            null,
            "Invalid description! The description may not be blank.",
            "Error updating item type",
            JOptionPane.ERROR_MESSAGE);
      } else if (purchasePrice < 0) {
        JOptionPane.showMessageDialog(
            null, "Invalid purchase price!", "Error updating item type", JOptionPane.ERROR_MESSAGE);
      } else if (isRentable && rentalPrice <= 0) {
        JOptionPane.showMessageDialog(
            null, "Invalid rental price!", "Error updating item type", JOptionPane.ERROR_MESSAGE);
      } else if (isRentable && rentalDuration <= 0) {
        JOptionPane.showMessageDialog(
            null,
            "Invalid rental duration!",
            "Error updating item type",
            JOptionPane.ERROR_MESSAGE);
      } else {
        try {
          ItemDatabase.updateItemType(
              new ItemType(
                  itemType.getTypeId(),
                  name,
                  description,
                  purchasePrice,
                  isRentable,
                  rentalPrice,
                  rentalDuration));
          parent.refresh();
          this.dispose();
        } catch (SQLException ex) {
          JOptionPane.showMessageDialog(
              null,
              "Unable to update item type!",
              "Error updating item type",
              JOptionPane.ERROR_MESSAGE);
        }
      }
    }
  }