Example #1
0
  /**
   * 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);
    }
  }
Example #2
0
  /** @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);
  }