예제 #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);
    }
  }
예제 #2
0
  protected void writeAllNativeTables(ClassClass classes[]) {
    sortClasses(classes);
    Vector nativeClasses = new Vector();

    // (1) Write all the function declarations
    for (int i = 0; i < classes.length; i++) {
      boolean classHasNatives = false;
      EVMClass cc = (EVMClass) classes[i];
      if (cc.isPrimitiveClass() || cc.isArrayClass()) {
        continue;
      }
      EVMMethodInfo m[] = cc.methods;
      sortMethods(m);
      int nmethod = (m == null) ? 0 : m.length;
      for (int j = 0; j < nmethod; j++) {
        EVMMethodInfo meth = m[j];
        MethodInfo mi = meth.method;
        if (!isNativeOrEntryFunc(cc, mi)) {
          continue;
        }
        if (!classHasNatives) {
          classHasNatives = true;
          nativeClasses.addElement(cc);
        }
        int entry;
        entry = checkEntry(cc, mi);
        if (entry < 0 || use_entries[entry].length > 4) {
          // This is a "native"
          out.println(
              "extern \"C\" " + mi.getJNIReturnType() + " " + mi.getNativeName(true) + "();");
        }
        if (entry >= 0) {
          // This is an "entry" (could also be a "native")
          out.println("extern \"C\" void " + use_entries[entry][3] + "();");
        }
      }
    }
    out.println();
    out.println();

    // (2) Write all the "_natives[]" and "_entries[]" tables for
    //     individual classes

    for (Enumeration e = nativeClasses.elements(); e.hasMoreElements(); ) {
      EVMClass cc = (EVMClass) e.nextElement();
      EVMMethodInfo m[] = cc.methods;
      int nmethod = (m == null) ? 0 : m.length;
      int num_native_methods = 0;
      int num_entry_methods = 0;

      for (int j = 0; j < nmethod; j++) {
        EVMMethodInfo meth = m[j];
        MethodInfo mi = meth.method;
        if (!isNativeOrEntryFunc(cc, mi)) {
          continue;
        }
        if (isNativeFunc(cc, mi)) {
          num_native_methods++;
        }
        if (isEntryFunc(cc, mi)) {
          num_entry_methods++;
        }
      }

      // Write the "_natives[]" struct
      if (num_native_methods > 0) {
        out.println("static const JvmNativeFunction " + cc.getNativeName() + "_natives[] = " + "{");
        for (int j = 0; j < nmethod; j++) {
          EVMMethodInfo meth = m[j];
          MethodInfo mi = meth.method;
          if (!isNativeOrEntryFunc(cc, mi)) {
            continue;
          }
          if (isNativeFunc(cc, mi)) {
            out.print(pad("  JVM_NATIVE(\"" + mi.name.string + "\",", 30));
            out.print(pad("\"" + mi.type.string + "\", ", 25));
            out.println(mi.getNativeName(true) + "),");
          }
        }
        out.println("  {(char*)0, (char*)0, (void*)0}");
        out.println("};");
        out.println();
      }

      // Write the "_entries[]" struct
      if (num_entry_methods > 0) {
        out.println("static const JvmNativeFunction " + cc.getNativeName() + "_entries[] = " + "{");
        for (int j = 0; j < nmethod; j++) {
          EVMMethodInfo meth = m[j];
          MethodInfo mi = meth.method;
          if (!isNativeOrEntryFunc(cc, mi)) {
            continue;
          }
          int entry;
          if ((entry = checkEntry(cc, mi)) >= 0) {
            out.print(
                "  JVM_ENTRY(\"" + mi.name.string + "\"," + spaces(20 - mi.name.string.length()));
            out.print("\"" + mi.type.string + "\", ");
            out.println(use_entries[entry][3] + "),");
          }
        }
        out.println("  {(char*)0, (char*)0, (void*)0}");
        out.println("};");
        out.println();
      }
    }

    // (3) Write the top-level table

    out.println("const JvmNativesTable jvm_natives_table[] = {");

    for (Enumeration e = nativeClasses.elements(); e.hasMoreElements(); ) {
      EVMClass cc = (EVMClass) e.nextElement();
      String className = cc.ci.className;
      out.println("  JVM_TABLE(\"" + className + "\",");
      EVMMethodInfo m[] = cc.methods;
      int nmethod = (m == null) ? 0 : m.length;

      int num_native_methods = 0;
      int num_entry_methods = 0;

      for (int j = 0; j < nmethod; j++) {
        EVMMethodInfo meth = m[j];
        MethodInfo mi = meth.method;
        if (!isNativeOrEntryFunc(cc, mi)) {
          continue;
        }
        if (isNativeFunc(cc, mi)) {
          num_native_methods++;
        }
        if (isEntryFunc(cc, mi)) {
          num_entry_methods++;
        }
      }

      if (num_native_methods > 0) {
        out.println(spaces(40) + cc.getNativeName() + "_natives,");
      } else {
        out.println(spaces(40) + "(JvmNativeFunction*)0,");
      }
      if (num_entry_methods > 0) {
        out.println(spaces(40) + cc.getNativeName() + "_entries),");
      } else {
        out.println(spaces(40) + "(JvmNativeFunction*)0),");
      }
    }
    out.println("  JVM_TABLE((char*)0, (JvmNativeFunction*)0, " + "(JvmNativeFunction*)0)");
    out.println("};");

    // (4) Write the jvm_native_execution_top-level table

    out.println("const JvmExecutionEntry jvm_api_entries[] = {");
    for (Enumeration e = nativeClasses.elements(); e.hasMoreElements(); ) {
      EVMClass cc = (EVMClass) e.nextElement();
      EVMMethodInfo m[] = cc.methods;
      int nmethod = (m == null) ? 0 : m.length;

      for (int j = 0; j < nmethod; j++) {
        EVMMethodInfo meth = m[j];
        MethodInfo mi = meth.method;
        int index;
        if ((index = checkEntry(cc, mi)) >= 0) {
          String entryName = use_entries[index][3];
          out.println("{(unsigned char*)&" + entryName + ",");
          out.println("(char*)\"" + entryName + "\"},");
        }
      }
    }
    out.println("{(unsigned char*)0, (char*)0}};");
  }
예제 #3
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);
  }
예제 #4
0
    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);
          }
        }
      }
    }