Esempio n. 1
0
  public void addAction() {
    String symbol = symbolField.getText().toUpperCase();

    // CHECK IF A VALUE HAS BEEN ENTERED
    if (!(symbolField.getText().equals("")) || symbolField.getText().equals("Symbol")) {
      Stock stock = new Stock(symbolField.getText().toUpperCase());
      ActiveModelEvent ev = new ActiveModelEvent(this, symbol, true, stock.getId());
      if (activeModelListener != null) {
        activeModelListener.activeModelAddEventOccurred(ev);
        symbolField.setText("");
      }
    } else {
      JOptionPane.showMessageDialog(
          null,
          "Please enter a valid stock symbol",
          "Invalid stock symbol",
          JOptionPane.ERROR_MESSAGE);
    }
  }
  private void calculateSpecificStockMACD(Stock thisStock, Stock previousStock) {
    if (previousStock == null) {
      thisStock.setStockMACDFastEMA(thisStock.getStockAvg());
      thisStock.setStockMACDSlowEMA(thisStock.getStockAvg());
      thisStock.setStockMACDDIF(thisStock.getStockMACDFastEMA() - thisStock.getStockMACDSlowEMA());
      thisStock.setStockMACDDEA(thisStock.getStockMACDFastEMA() - thisStock.getStockMACDSlowEMA());
      thisStock.setStockMACD(thisStock.getStockMACDDIF() - thisStock.getStockMACDDEA());
      thisStock.setStockMACDLastOne(0);
    } else {
      thisStock.setStockMACDFastEMA(
          thisStock.getStockAvg() * 2.0 / (RuleConfig.getInstance().getMACDFastEMA() / 2.0)
              + previousStock.getStockMACDFastEMA()
                  * (RuleConfig.getInstance().getMACDFastEMA() / 2.0 - 2)
                  / (RuleConfig.getInstance().getMACDFastEMA() / 2.0));
      thisStock.setStockMACDSlowEMA(
          thisStock.getStockAvg() * 2.0 / (RuleConfig.getInstance().getMACDSlowEMA() / 2.0)
              + previousStock.getStockMACDSlowEMA()
                  * (RuleConfig.getInstance().getMACDSlowEMA() / 2.0 - 2)
                  / (RuleConfig.getInstance().getMACDSlowEMA() / 2.0));

      thisStock.setStockMACDDIF(thisStock.getStockMACDFastEMA() - thisStock.getStockMACDSlowEMA());
      thisStock.setStockMACDDEA(
          thisStock.getStockMACDDIF() * 2.0 / (RuleConfig.getInstance().getMACDDEAEMA() / 2.0)
              + previousStock.getStockMACDDEA()
                  * (RuleConfig.getInstance().getMACDDEAEMA() / 2.0 - 2)
                  / (RuleConfig.getInstance().getMACDDEAEMA() / 2.0));
      thisStock.setStockMACD(thisStock.getStockMACDDIF() - thisStock.getStockMACDDEA());
      thisStock.setStockMACDLastOne(previousStock.getStockMACD());
    }
  }
  public void checkStatus() {
    boolean nothingToReport = true;
    Set<Stock> critical = Service.getCriticalStocks(75);
    if (critical != null && critical.size() > 0) {
      if (critical.size() <= 1) {
        String name = "Unnamed";
        for (Stock s : critical) {
          if (s != null && s.getName() != null && s.getName().trim().length() > 0) {
            name = s.getName();
            break;
          }
        }
        lblStocks.setText(
            "<html><strong style=\"color:#777700;\">We are in a bit of a pickle</strong>: one of your stocks ("
                + name
                + ") is running out of space. </html>");
      } else {
        String names = "";
        for (Stock s : critical) {
          if (s != null && s.getName() != null && s.getName().trim().length() > 0) {
            if (names.length() > 0) names += ", ";
            names += s.getName();
          } else {
            if (names.length() > 0) names += ", ";
            names += "Unnamed";
          }
        }
        lblStocks.setText(
            "<html><strong style=\"color:red;\">You are in a world of hurt</strong>: "
                + critical.size()
                + " of your stocks ("
                + names
                + ") are running out of space. </html>");
      }
      nothingToReport = false;
    } else {
      lblStocks.setText(
          "<html><strong style=\"color:green;\">Your storage is in tip-top shape</strong>: there are no Stocks running out of space. </html>");
    }

    Set<Tray> expiring = Service.getExpiringTrays();
    if (expiring != null && expiring.size() > 0) {
      Map<Stock, Integer> stocks = new HashMap<Stock, Integer>();
      for (Tray tray : expiring) {
        if (tray != null
            && tray.getStorageUnit() != null
            && tray.getStorageUnit().getStock() != null) {
          Stock stock = tray.getStorageUnit().getStock();
          if (stocks.containsKey(stock)) {
            stocks.put(stock, stocks.get(stock) + 1);
          } else {
            stocks.put(stock, 1);
          }
        } else {
          if (stocks.containsKey(null)) {
            stocks.put(null, stocks.get(null) + 1);
          } else {
            stocks.put(null, 1);
          }
        }
      }
      String names = "";
      for (Stock s : stocks.keySet()) {
        if (s != null && s.getName() != null && s.getName().trim().length() > 0) {
          if (names.length() > 0) names += ", ";
          names += s.getName();
          names += " - " + stocks.get(s);
        } else if (s == null) {
          if (names.length() > 0) names += ", ";
          names += "Unknown stock";
          names += " - " + stocks.get(s);
        }
      }
      lblExpiring.setText(
          "<html><strong style=\"color:#777700;\">The situation is getting hairy</strong>: the following stocks have aging trays: "
              + names
              + ". </html>");
      nothingToReport = false;
    } else {
      lblExpiring.setText(
          "<html><strong style=\"color:green;\">There is no problem with expiring trays</strong>: there are no expiring trays at the moment. </html>");
    }
    Set<Tray> wasted = Service.getWastedTrays();
    if (wasted != null && wasted.size() > 0) {
      Map<Stock, Integer> stocks = new HashMap<Stock, Integer>();
      for (Tray tray : wasted) {
        if (tray != null
            && tray.getStorageUnit() != null
            && tray.getStorageUnit().getStock() != null) {
          Stock stock = tray.getStorageUnit().getStock();
          if (stocks.containsKey(stock)) {
            stocks.put(stock, stocks.get(stock) + 1);
          } else {
            stocks.put(stock, 1);
          }
        } else {
          if (stocks.containsKey(null)) {
            stocks.put(null, stocks.get(null) + 1);
          } else {
            stocks.put(null, 1);
          }
        }
      }
      String names = "";
      for (Stock s : stocks.keySet()) {
        if (s != null && s.getName() != null && s.getName().trim().length() > 0) {
          if (names.length() > 0) names += ", ";
          names += s.getName();
          names += " - " + stocks.get(s);
        } else if (s == null) {
          if (names.length() > 0) names += ", ";
          names += "Unknown stock";
          names += " - " + stocks.get(s);
        }
      }
      lblWasted.setText(
          "<html><strong style=\"color:red;\">The reality is getting very sad</strong>: the following stocks have wasted trays: "
              + names
              + ". </html>");
      nothingToReport = false;
    } else {
      lblWasted.setText(
          "<html><strong style=\"color:green;\">There is no problem with wasted trays</strong>: there are no wasted trays at the moment. </html>");
    }

    if (nothingToReport) {
      this.setPreferredSize(new Dimension(0, 0));
    } else {
      this.setPreferredSize(new Dimension(0, 50));
    }
  }