/**
   * This method contains the definition for the 'update' method of the observer interface. This
   * method updates the Food Stock panel in the user interface window.
   */
  public void update(Observable observable, Object obj) {
    VendingMachine machine = (VendingMachine) observable;
    itemList = (Hashtable) obj;
    Enumeration e = itemList.elements();

    for (int i = 0; e.hasMoreElements(); i++) { // Display the updates in the available Food Stocks
      VendingMachine.FoodStock stock = (VendingMachine.FoodStock) e.nextElement();
      FoodItem item = stock.item;

      itemLabelArray[i].setText(item.getDescription());

      Integer code = item.getItemCode();
      String str = code.toString();
      codeLabelArray[i].setText("Item Code: " + str);

      Double price = item.getPrice();
      String str1 = price.toString();
      priceLabelArray[i].setText("Price: " + str1);

      Integer quant = machine.GetQuantity(code);
      String str2 = quant.toString();
      quantityLabelArray[i].setText("Quantity: " + str2);
    }
  }
  /** @param parentUI an instance of MainUI class */
  public ConsumerUI(MainUI parentUI) {
    super("SmartCal Vending Machine - Consumer Operations");
    super.setLocation(200, 10);
    this.parentUI = parentUI;

    VendingMachine machine = parentUI.getVendingMachine(); // Get an instance of Vending Machine
    machine.addObserver(
        this); // Register ConsumerUI as an observer to receive VendingMachine updates

    Container container1 = getContentPane();
    container1.setLayout(new BoxLayout(container1, BoxLayout.Y_AXIS));

    // itemPanel = Food Stock Panel
    itemPanel = new JPanel(new GridLayout(0, 4));
    itemPanel.setBorder(BorderFactory.createTitledBorder("Food Stock"));

    itemLabelArray = (JLabel[]) Array.newInstance(JLabel.class, 15);
    codeLabelArray = (JLabel[]) Array.newInstance(JLabel.class, 15);
    priceLabelArray = (JLabel[]) Array.newInstance(JLabel.class, 15);
    quantityLabelArray = (JLabel[]) Array.newInstance(JLabel.class, 15);

    for (int i = 0; i < 15; i++) { // Maximum No. of Food Item Slots is 15
      itemLabelArray[i] = new JLabel();
      codeLabelArray[i] = new JLabel();
      priceLabelArray[i] = new JLabel();
      quantityLabelArray[i] = new JLabel();

      itemPanel.add(itemLabelArray[i]);
      itemPanel.add(codeLabelArray[i]);
      itemPanel.add(priceLabelArray[i]);
      itemPanel.add(quantityLabelArray[i]);
    }

    itemList = machine.getFoodStocks(); // Get the list of Food Items from the Vending Machine &
    Enumeration e = itemList.elements(); // Store it in the Enumeration

    for (int i = 0;
        e.hasMoreElements();
        i++) { // Store the Items from Enumeration to the ConsumerUI Variables
      VendingMachine.FoodStock stock = (VendingMachine.FoodStock) e.nextElement();
      FoodItem item = stock.item;

      itemLabelArray[i].setText(item.getDescription());

      Integer code = item.getItemCode();
      String str = code.toString();
      codeLabelArray[i].setText("Item Code: " + str);

      Double price = item.getPrice();
      String str1 = price.toString();
      priceLabelArray[i].setText("Price: " + str1);

      Integer quant = machine.GetQuantity(code);
      String str2 = quant.toString();
      quantityLabelArray[i].setText("Quantity: " + str2);
    }

    container1.add(itemPanel);

    // displayPanel
    displayPanel = new JPanel(new FlowLayout());
    displayLabel = new JLabel(display);
    displayPanel.add(displayLabel);
    displayPanel.setBorder(BorderFactory.createTitledBorder("Display Screen"));

    container1.add(displayPanel);

    // actionPanel1 = Consumer Operations Panel
    actionPanel1 = new JPanel(new GridLayout(0, 1));
    actionPanel1.setBorder(BorderFactory.createTitledBorder("Consumer Operations"));

    // actionPanel1 = subPanel1 + subPanel2  + subPanel3

    // subPanel1
    subPanel1 = new JPanel(new FlowLayout());
    buyItem = new JRadioButton("Buy a Food Item");
    acceptLabel = new JLabel(accept);
    codeText = new JTextField(10);

    subPanel1.add(buyItem);
    subPanel1.add(acceptLabel);
    subPanel1.add(codeText);

    actionPanel1.add(subPanel1);

    // subPanel2
    subPanel2 = new JPanel(new FlowLayout());
    displayNutri = new JRadioButton("Display Nutritional Info");
    acceptLabel1 = new JLabel(accept);
    codeText1 = new JTextField(10);
    ok3 = new JButton("Display"); // for enter itemCode

    subPanel2.add(displayNutri);
    subPanel2.add(acceptLabel1);
    subPanel2.add(codeText1);
    subPanel2.add(ok3);

    actionPanel1.add(subPanel2);

    // subPanel3
    subPanel3 = new JPanel(new FlowLayout());
    querySuggest = new JRadioButton("Query Suggestion of Food Items");
    calorieLabel = new JLabel("Maximum Calorie Limit");
    calorieText = new JTextField(10);
    ok4 = new JButton("Display");

    subPanel3.add(querySuggest);
    subPanel3.add(calorieLabel);
    subPanel3.add(calorieText);
    subPanel3.add(ok4);

    actionPanel1.add(subPanel3);

    container1.add(actionPanel1);

    // actionPanel2 - H/W Slots = actionPanel2 + dispatchPanel
    actionPanel2 = new JPanel(new FlowLayout());
    actionPanel2.setBorder(BorderFactory.createTitledBorder("Hardware Slots"));
    enterLabel = new JLabel(enterAmount);
    amountText = new JTextField(10);
    buy = new JButton("BUY");
    cancel = new JButton("Cancel");
    changeLabel = new JLabel(change);
    changeText = new JTextField(10);
    ok1 = new JButton("Collect Change");

    actionPanel2.add(enterLabel);
    actionPanel2.add(amountText);
    actionPanel2.add(buy);
    actionPanel2.add(cancel);
    actionPanel2.add(changeLabel);
    actionPanel2.add(changeText);
    actionPanel2.add(ok1);

    // Dispatch Panel
    dispatchPanel = new JPanel(new FlowLayout());
    dispatcherLabel = new JLabel(dispatcher);
    ok2 = new JButton("Collect Item");

    dispatchPanel.add(dispatcherLabel);
    dispatchPanel.add(ok2);

    actionPanel2.add(dispatchPanel);

    container1.add(actionPanel2);

    // Exit Panel
    exitPanel = new JPanel(new FlowLayout());
    exit = new JButton("Back to Main Menu");
    exitPanel.add(exit);

    container1.add(exitPanel);

    // Registering the components to the Event Handlers
    // Register Buy button to Event Handler
    BuyHandler BHandler = new BuyHandler();
    buy.addActionListener(BHandler);

    // Register button of cancel to Event Handler
    CancelHandler CHandler = new CancelHandler();
    cancel.addActionListener(CHandler);

    // Register RadioButton of 'Buy Item' to Event Handler
    BuyRadioHandler BRHandler = new BuyRadioHandler();
    buyItem.addActionListener(BRHandler);

    // Register RadioButton of 'Display Nutritional Info' to Event Handler
    NutriRadioHandler NutriHandler = new NutriRadioHandler();
    displayNutri.addActionListener(NutriHandler);

    // Register RadioButton of 'Query Suggestion' to Event Handler
    QueryRadioHandler QueryHandler = new QueryRadioHandler();
    querySuggest.addActionListener(QueryHandler);

    // Register Button for 'Display Nutritional Info' to Event Handler
    DisplayNutriHandler DNHandler = new DisplayNutriHandler();
    ok3.addActionListener(DNHandler);

    // Register Button for 'Query Suggestion' to Event Handler
    DisplayNutriHandler DNHandler1 = new DisplayNutriHandler();
    ok4.addActionListener(DNHandler1);

    // Register Button for 'Back to Main Menu' to Event Handler
    MainMenuHandler1 MMHandler1 = new MainMenuHandler1();
    exit.addActionListener(MMHandler1);

    // Register Button for 'Collect Change' to Event Handler
    CollectChangeHandler CCHandler = new CollectChangeHandler();
    ok1.addActionListener(CCHandler);

    // Register Button for 'Collect Food Item' to Event Handler
    CollectItemHandler CIHandler = new CollectItemHandler();
    ok2.addActionListener(CIHandler);
  }
    public void actionPerformed(ActionEvent event) {
      if (displayNutri.isSelected()
          && codeText.getText() != null) { // for Display Nutritional Info Button
        try {
          intValue2 = Integer.valueOf(codeText1.getText()); // Get the text value of Item Code
        } catch (NumberFormatException e) {
          display = "Enter Correct Item Code"; // Display Error Message for Wrong Input
          displayLabel.setForeground(Color.magenta);
          displayLabel.setText(display);
          codeText1.setText(null);
          return;
        }
        FoodItem item =
            DisplayNutritionalInfo(intValue2); // Call to the Method to Display Nutritional Info

        if (item != null) {
          calories =
              item.GetCalories(); // Get the nutrional Info of the Item Code from the FoodItem
          sugar = item.GetSugarContents();
          fats = item.GetFatContents();

          display =
              "Calorific Value = "
                  + calories
                  + "   Sugar Contents = "
                  + sugar
                  + "gms"
                  + "   Fat Contents = "
                  + fats
                  + "gms";
          displayLabel.setForeground(Color.magenta);
          displayLabel.setText(display);
        } else {
          display = "Enter Correct item Code"; // Display Error Message for Wrong Input
          displayLabel.setForeground(Color.magenta);
          displayLabel.setText(display);
          codeText.setText(null);
        }
      }

      if (querySuggest.isSelected()
          && calorieText.getText() != null) { // for Display Query Suggestion Button
        try {
          intValue2 =
              Integer.valueOf(calorieText.getText()); // Get the text value of Maximum Calorie Limit
        } catch (NumberFormatException e) {
          display = "Enter Correct Calorie Limit"; // Display Error Message for Wrong Input
          displayLabel.setForeground(Color.magenta);
          displayLabel.setText(display);
          calorieText.setText(null);
          return;
        }

        if (intValue2 < 0) {
          display =
              "Enter Correct Calorie Limit"; // Display Error Message for Wrong (negative) Input
          displayLabel.setForeground(Color.magenta);
          displayLabel.setText(display);
          calorieText.setText(null);
        } else {
          Hashtable itemList = DisplaySuggestion();
          Enumeration e = itemList.elements();

          String finalStr = "";
          for (int i = 0;
              e.hasMoreElements();
              i++) { // Get the list of Food items below the entered Calorie Limit
            VendingMachine.FoodStock stock = (VendingMachine.FoodStock) e.nextElement();
            FoodItem item = stock.item;
            if (item.GetCalories() <= intValue2) {
              flag = 1;
              if (!finalStr.equals("")) {
                finalStr = finalStr + " , " + item.getDescription();
              } else {
                finalStr = item.getDescription();
              }
            }
          }
          if (flag == 1) {
            display = finalStr; // Display the list of Food items below the entered Calorie Limit
            displayLabel.setForeground(Color.magenta);
            displayLabel.setText(display);
            flag = 0;
          } else {
            display =
                "No items in the Machine below Calorie Limit "
                    + intValue2; // No Items below the entered Calorie Limit
            displayLabel.setForeground(Color.magenta);
            displayLabel.setText(display);
          }
        }
      }
    }