/**
   * Function that is called when the user clicks on the delete all button. Deletes all of the
   * ingredients, and updates the pantry accordingly.
   */
  public void onDeleteAll() {
    GlobalApplication app = (GlobalApplication) getApplication();
    Pantry pantry = app.getCurrentRecipe().getIngredients();

    // If the ingredients is empty, we don't need to delete anything
    if (pantry.isEmpty()) {
      showMessage("Nothing to delete");
      return;
    }

    // Builds the alert dialog box
    AlertDialog.Builder prompt = new AlertDialog.Builder(this);
    prompt.setTitle("Delete All");
    prompt.setMessage(
        "Are you sure you want to delete all ingredients? They " + "will be gone... forever.");

    prompt.setNegativeButton(
        "No",
        new DialogInterface.OnClickListener() {

          // User has changed their mind
          @Override
          public void onClick(DialogInterface dialog, int which) {
            return;
          }
        });
    prompt.setPositiveButton(
        "Yes",
        new DialogInterface.OnClickListener() {

          // User does want to delete all ingredients
          @Override
          public void onClick(DialogInterface dialog, int which) {
            GlobalApplication app = (GlobalApplication) getApplication();
            Pantry pantry = app.getCurrentRecipe().getIngredients();
            pantry.emptyPantry();
            refresh();
          }
        });
    prompt.show();
    return;
  }
  /**
   * Function that is called when the user clicks the add ingredient button. Adds the ingredient to
   * the recipe. Also refreshes the view to show the new ingredient.
   */
  public void onAdd() {
    EditText quantity = (EditText) findViewById(R.id.addIngredientQuantity);
    EditText name = (EditText) findViewById(R.id.addIngredientName);
    Spinner units = (Spinner) findViewById(R.id.addIngredientMeasurement);

    // Get the units the user inputed
    int position = units.getSelectedItemPosition();
    String unitsString = Constants.getUnitFromPosition(position);
    String nameString = name.getText().toString();
    String quantityString = quantity.getText().toString();

    // Do the integer conversion like this just in case no number is entered
    Integer amount;
    try {
      amount = new Integer(quantityString);
    } catch (NumberFormatException e) {
      showMessage("Invalid number");
      return;
    }

    Ingredient ingredient = new Ingredient(nameString, amount, unitsString);

    // Check for ingredient validity
    if (ingredient.isValidInfo() != Constants.GOOD) {
      showMessage("Invalid info entered");
      return;
    }

    GlobalApplication app = (GlobalApplication) getApplication();
    Pantry pantry = app.getCurrentRecipe().getIngredients();
    pantry.addIngredient(ingredient);

    // Clear the edit text boxes for future ingredients
    name.setText("");
    quantity.setText("");

    refresh();

    return;
  }
예제 #3
0
  public static void main(String[] args) {
    System.out.println("Welcome to Mother Hubbard's Pantry!\n\nThe jams are:");

    Jam goose = new Jam("Gooseberry", "7/4/86", 12);
    Jam apple = new Jam("Crab Apple", "9/30/99", 8);
    Jam rhub = new Jam("Rhubarb", "10/31/99", 16);

    Pantry hubbard = new Pantry(goose, apple, rhub);
    hubbard.print();

    int select = promptSelect();
    int amount = promptAmount();

    while (select != -1) {
      amount = promptAmount();

      hubbard.select(select);
      hubbard.spread(amount);
      System.out.println("\nThe jams are: ");
      hubbard.print();

      select = promptSelect();
    }
  }