Home > Back-end >  Using the ArrayList<Object> feature to store Array from Method
Using the ArrayList<Object> feature to store Array from Method

Time:11-14

I am about 95% done I believe, the only thing that is stopping me is that I have to print the results from printInfo() into an element.

The program is gathering input, and the input is based on whether or not the item is a plant. After the input is entered and the setters are ran, I am suppose to access the printInfo() from plant for example, and store it as an element in the myGarden array.

Then I will call a method that prints the elements of the object array. Those elements will be the information from printInfo() from the plant, and the flower class.

package labpackage;

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(ArrayList<Object> objList) {
    
    int i;

    for (i = 0; i < objList.size();   i) {
       System.out.println(objList.get(i));
    }
    
}
   
   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 <Object> myGarden = new ArrayList();
      
   // TODO: Declare variables - plantName, plantCost, colorOfFlowers, isAnnual
      
      String plantName;
      String plantCost;
      String colorOffFlowers;
      boolean isAnnual;
      
      
      
      input = scnr.next();
      while(!input.equals("-1")){
         // TODO: Check if input is a plant or flower
          
          if (input.contains("plant")) { 
              
              Plant plant = new Plant();
          
              plantName = scnr.next();
              
              plantCost = scnr.next();
             
              plant.setPlantName(plantName);
              plant.setPlantCost(plantCost);
              
              myGarden.addAll(myGarden);
              
              
              
             
              // Store as a plant object or flower object
        
          }
          
          if (!input.contains("plant")) { 
              
              Flower flower = new Flower();
              
              plantName = scnr.next();
              plantCost = scnr.next();
              isAnnual = scnr.nextBoolean();
              colorOffFlowers = scnr.next();
              // missing code to add print result as an element 
              
              
              flower.setPlantName(plantName);
              flower.setPlantCost(plantCost);
              flower.setPlantType(isAnnual);
              flower.setColorOfFlowers(colorOffFlowers);
              
              
    
             
              // missing code to add print result as an element 
              
              
              
              // Add to the ArrayList myGarden
          }
          
          
         
         //      
      
         input = scnr.next();
      }
      
      // TODO: Call the method printArrayList to print myGarden
      
      printArrayList(myGarden);
      
   }
}


package labpackage;

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 Information: ");
          System.out.println("   Plant name: "   plantName);
          System.out.println("   Cost: "   plantCost);
       }
    }

package labpackage;

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 Information: ");
          System.out.println("   Plant name: "   plantName);
          System.out.println("   Cost: "   plantCost);
          System.out.println("   Annual: "   isAnnual);
          System.out.println("   Color of flowers: "   colorOfFlowers);
       }
    }

CodePudding user response:

To print the plant or flower info update printArrayList method like this:

public static void printArrayList(ArrayList<Plant> objList) {
    for (i = 0; i < objList.size();   i) {
       System.out.println(objList.get(i).printInfo());
    }
}

CodePudding user response:

Instead of using Object as type of elements in your collection (which is most generic), you have to narrow down range of possible types. Since you have good hierarchy here you can use base class. Replace

ArrayList <Object> myGarden = new ArrayList();

with

List<Plant> myGarden = new ArrayList<>();

And you will be able to call printInfo() method, because all objects in collection will definitely be at least Plant or any subclass of that class which means they all will have printInfo() method.

public static void printArrayList(Collection<Plant> plants) {
    for (Plant p : plants) {
        System.out.println(p.printInfo());  
    }
}

Or shorter using Java Stream API

myGarden.stream().forEach(Plant::printInfo);

Hope it helps!

  • Related