public Object findCandy(int weight) throws InvalidWeightException { for (Candy candy : candy1) { if (candy.getWeight() == weight) { return "The candy with " + weight + " weight is " + candy.toString(); } } return "The candy with " + weight + " weight is not found."; }
public Object findCandy(String name) { for (Candy candy : candy1) { if (candy.getName().compareToIgnoreCase(name) == 0) { return "The candy with " + name + " name is " + candy.toString(); } } return "The candy with " + name + " name is not found."; }
public Object findCandy(String name, String type) { for (Candy candy : candy1) { try { if ((candy.getName().compareToIgnoreCase(name) == 0) && (candy.getType().compareToIgnoreCase(type) == 0)) { return "The candy with " + name + " name and " + type + " type is " + candy.toString(); } } catch (InvalidCandyTypeException e) { e.printStackTrace(); } } return "The candy with " + name + " name and " + type + " type is not found."; }
public Object findCandy(String name, int weight) { for (Candy candy : candy1) { try { if ((candy.getName().compareToIgnoreCase(name) == 0) && (candy.getWeight() == weight)) { return "The candy with " + name + " name and " + weight + " weight is " + candy.toString(); } } catch (InvalidWeightException e) { e.printStackTrace(); } } return "The candy with " + name + " name and " + weight + " weight is not found."; }
public static void printArray(Candy[] array) { for (Candy candy : array) System.out.println(candy.toString()); System.out.println(); }