import java.util.Scanner; import java.util.ArrayList; public class MyGarden{ public static void main(String[] args){ Scanner scnr = new Scanner(System.in); ArrayList myGarden; myGarden = inputPlants(scnr); printArrayList(myGarden); } public static void printArrayList(ArrayList garden){ for (Plant plant:garden){ plant.printInfo(); } } public static ArrayList inputPlants(Scanner scnr){ String pName, pCost, pAnnual, pColor; String s; String[] splitString; boolean isInt; Plant currPlant; ArrayList garden = new ArrayList(); while(true){ //plant has name and cost 3 inputs //flower has all 5 inputs System.out.println("Please enter your plant: "); System.out.println("type name cost (annual) (color)"); s = scnr.nextLine(); splitString = s.split(" "); //Check for exit if (isInteger(splitString[0])){ if (Integer.parseInt(splitString[0]) == -1){break;} } //Check if too many words or too few words if (splitString.length > 5) { System.out.println("Too many words!!!"); continue; } else if ((splitString.length !=3) && (splitString.length !=5)){ System.out.println("Expecting 3 or 5 words!; -1 to quit"); continue; } //Set generic strings to vars for readability pName = splitString[1]; pCost = splitString[2]; // Plants take 3 if (splitString.length == 3){ currPlant = new Plant(pName, pCost); garden.add(currPlant); } // Flowers take 5 else{ //get remaining strings pAnnual = splitString[3]; pColor = splitString[4]; currPlant = new Flower(pName, pCost, pAnnual, pColor); garden.add(currPlant); } System.out.println(); } return garden; } public static boolean isInteger(String s){ boolean isInt = false; try{ Integer.parseInt(s); isInt = true; } catch (NumberFormatException ex){ } return isInt; } }