import java.util.Scanner; import java.util.ArrayList; import java.util.StringTokenizer; public class PlantArrayListExample { // TODO: Define a printArrayList method that prints an ArrayList of plant (or flower) objects public static void printArrayList() { for(int i = 0; i < myGarden.size(); i++) { System.out.println(myGarden.get(i).printInfo()); } } public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String input; // TODO: Declare an ArrayList called myGarden that can hold object of type plant Arraylist myGarden = new Arraylist(); // TODO: Declare variables - plantName, plantCost, flowerName, flowerCost, colorOfFlowers, isAnnual String plantName; int plantCost; String flowerName; int flowerCost; String colorOfFlowers; boolean isAnnual; input = scnr.next(); while(!input.equals("-1")){ //String[] info = input.split(" "); StringTokenizer info = new StringTokenizer(info, " "); if(info[0].toLowerCase().equals("plant")) { Plant plantObject = new Plant(); plantObject.setPlantName(info[1]); plantObject.setPlantCost(info[2]); myGarden.add(plantObject); } else if(info[0].toLowerCase().equals("flower")) { Flower flowerObject = new Flower(); flowerObject.setFlowerName(info[1]); flowerObject.setFlowerCost(info[2]); flowerObject.setPlantType(Boolean.parseBoolean(info[3])); flowerObject.setColorOfFlowers(info[4]); myGarden.add(flowerObject); } else { continue; } // TODO: Check if input is a plant or flower // Store as a plant object or flower object // Add to the ArrayList myGarden input = scnr.next(); } // TODO: Call the method printArrayList to print myGarden printArrayList(myGarden); } } public class Plant { protected String plantName; protected String plantCost; public void setPlantName(String userPlantName) { plantName = userPlantName; } public String getPlantName() { return plantName; } public void setPlantCost(String userPlantCost) { plantCost = userPlantCost; } public String getPlantCost() { return plantCost; } public void printInfo() { System.out.println(" Plant name: " + plantName); System.out.println(" Cost: " + plantCost); } } public class Flower extends Plant { private boolean isAnnual; private String colorOfFlowers; public void setPlantType(boolean userIsAnnual) { isAnnual = userIsAnnual; } public boolean getPlantType(){ return isAnnual; } public void setColorOfFlowers(String userColorOfFlowers) { colorOfFlowers = userColorOfFlowers; } public String getColorOfFlowers(){ return colorOfFlowers; } @Override public void printInfo(){ System.out.println(" Plant name: " + plantName); System.out.println(" Cost: " + plantCost); System.out.println(" Annual: " + isAnnual); System.out.println(" Color of flowers: " + colorOfFlowers); } }