Results 1 to 9 of 9
  1. #1
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    17
    Rep Power
    0

    Default Calling a method from main

    Hello guys,

    Here I am again. Learning Java.

    I have this assignment for school. The idea is to create a program to add plants and retrieve plants from a Nursery. I created a class with the properties of the plants and also there is the class an Array list to keep track of the plants entered ( they will have to be printed later) (I am not too sure if adding a public class there is the best option.
    The program will allow the user to pick and action and from there the code will do something. I don't want to have all the code inside 'main' The problem is in line 114.

    Can you guys guide me in the right direction?

    This is what I have so far.

    Java Code:
    package plant.nursery;
    
    import java.util.ArrayList;
    import java.util.Scanner;
    
    /**Class to create a plant for a nursery. 
     *
     * @author Alfredo
     */
    public class PlantNursery
    {
        //Variables for the class properties.
        int maxHeighFeet;
        String CommonName;
        String scientificName;
        double price;
        boolean isFragile;    
        
        /**
         * Public variable to add and retrieve data from the ArrayList.
         */
            public ArrayList<PlantNursery> plantList = new ArrayList<>();
        
        /** 
         * Default Constructor.
         */
        public PlantNursery()
        {
            this.maxHeighFeet = 0;
            this.CommonName = "Common Name";
            this.scientificName = "Scientific Name";
            this.price = 0.0;
            this.isFragile = true;
        }    
        
        /**Constructor with Parameters.
         * 
         * @param maxHeighFeet Maximum heigh the plant can grow.
         * @param CommonName Common name for the plant.
         * @param scientificName Scientific Name for the plant.
         * @param price Price for selling the plant.
         * @param isFragile If the plant is fragile or not if not provided it will be 'Fragile' by default.
         */
        public PlantNursery(int maxHeighFeet, String CommonName, String scientificName, double price, boolean isFragile)
        {
            this.maxHeighFeet = maxHeighFeet;
            this.CommonName = CommonName;
            this.scientificName = scientificName;
            this.price = price;
            this.isFragile = isFragile;
        }    
        
        /**
         * Method to add to the list of plants.
         */
        public void addPlant()
        {
            Scanner s = new Scanner(System.in);
            PlantNursery p = new PlantNursery();
            
            //Collect the data for the plant using the console.
            System.out.println("Please enter the Max Heigh.");
            p.maxHeighFeet= s.nextInt();
            System.out.println("Please enter the Common Name.");
            p.CommonName= s.nextLine();
            System.out.println("Please enter the Scientific Name.");
            p.scientificName= s.nextLine();
            System.out.println("Please enter if plant is fragile (yes/no)");
            if(s.nextLine().equalsIgnoreCase("no"))
            {
                p.isFragile=false;
            }else{
                p.isFragile=true;
            }
            
            // Add the new created plant to the list.
            plantList.add(p);
        }
            
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) 
        {
            // TODO code application logic here
           String MAIN_MENU="\n"+ "Type a task number below and press enter:" + "\n"+
                            "1  Add a new plant."+ "\n"+
                            "2  Edit plant."+ "\n"+
                            "3  Remove plant."+ "\n"+
                            "4  Print plants sorted by price."+ "\n" +
                            "5  Print plants sorted by scientific name."+ "\n"+
                            "6  Print plants sorted by common name."+ "\n"+
                            "7  Quit.";
           
           Scanner s = new Scanner(System.in);
           int selection;
           boolean exit=false;
           
           /** Infinite loop to prompt the user to perform an action all the time.
            *  One of these actions will be a break to exit the loop. 
            */       
            while(!exit) 
            {
                // Show the menu so the user can pick what activity want to perform.
                System.out.println(MAIN_MENU);
                
                // Read the input.
                selection = s.nextInt();
                
                // Select the appropriate action. 
                switch (selection) 
                {
                    case 1: addPlant(); // I cannot figure out this section here. All the others will be the same. 
                            break;
                    case 2: System.out.println("You picked "+ selection);
                            break;
                    case 3: System.out.println("You picked "+ selection);
                            break;
                    case 4: System.out.println("You picked "+ selection);
                            break;
                    case 5: System.out.println("You picked "+ selection);
                            break;
                    case 6: System.out.println("You picked "+ selection);
                            break;
                    case 7: exit=true;
                            break;
                    default: throw new AssertionError();                        
                }
            }       
        }    
    }
    Thanks a lot in advance.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: Calling a method from main

    The addPlant() method is a method of PlantNursery class. In order to get access to this method you'll need an instance of PlantNursery before you can access it. And also, you can actually create another class as your main program where you can separate the PlantNursery object with a main program (class with main() method) to execute it.

  3. #3
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    17
    Rep Power
    0

    Default Re: Calling a method from main

    Quote Originally Posted by wsaryada View Post
    ..you can actually create another class as your main program where you can separate the PlantNursery object with a main program (class with main() method) to execute it.
    That did the trick. I created a class for Plant apart from the main program.

    This is my final code

    Under a class Plant:

    Java Code:
    package plant.nursery;
    
    /**
     *
     * @author Alfredo
     */
    public class Plant
    {
       //Variables for the class properties.
        int maxHeighFeet;
        String CommonName;
        String scientificName;
        double price;
        boolean isFragile;    
        
        /** 
         * Default Constructor.
         */
        public Plant()
        {
            this.maxHeighFeet = 0;
            this.CommonName = "Common Name";
            this.scientificName = "Scientific Name";
            this.price = 0.0;
            this.isFragile = true;
        }    
        
        /**Constructor with Parameters.
         * 
         * @param maxHeighFeet Maximum heigh the plant can grow.
         * @param CommonName Common name for the plant.
         * @param scientificName Scientific Name for the plant.
         * @param price Price for selling the plant.
         * @param isFragile If the plant is fragile or not if not provided it will be 'Fragile' by default.
         */
        public Plant(int maxHeighFeet, String CommonName, String scientificName, double price, boolean isFragile)
        {
            this.maxHeighFeet = maxHeighFeet;
            this.CommonName = CommonName;
            this.scientificName = scientificName;
            this.price = price;
            this.isFragile = isFragile;
        }      
    }
    Code under the main program:
    Java Code:
    package plant.nursery;
    
    import java.util.ArrayList;
    import java.util.Scanner;
    
    
    /**Class to create a plant for a nursery. 
     *
     * @author Alfredo
     */
    public class PlantNursery
    {
        
        /**
         * Public variable to add and retrieve data from the ArrayList.
         */
        public static ArrayList<Plant> plantList = new ArrayList<>();
        
        
        /**
         * Method to add to the list of plants.
         */
        public void addPlant()
        {
            Scanner s = new Scanner(System.in);
            Plant p = new Plant();
            
            //Collect the data for the plant using the console.
            System.out.println("Please enter the Max Heigh.");
            p.maxHeighFeet= s.nextInt();
            System.out.println("Please enter the Common Name.");
            p.CommonName= s.nextLine();
            System.out.println("Please enter the Scientific Name.");
            p.scientificName= s.nextLine();
            System.out.println("Please enter if plant is fragile (yes/no)");
            p.isFragile = s.nextLine().equalsIgnoreCase("no");
            
            // Add the new created plant to the list.
            plantList.add(p);
        }        
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) 
        {
            // TODO code application logic here
           String MAIN_MENU="\n"+ "Type a task number below and press enter:" + "\n"+
                            "1  Add a new plant."+ "\n"+
                            "2  Edit plant."+ "\n"+
                            "3  Remove plant."+ "\n"+
                            "4  Print plants sorted by price."+ "\n" +
                            "5  Print plants sorted by scientific name."+ "\n"+
                            "6  Print plants sorted by common name."+ "\n"+
                            "7  Quit.";
           
           Scanner s = new Scanner(System.in);
           int selection;
           boolean exit=false;
           PlantNursery p = new PlantNursery();
           
           /** Infinite loop to prompt the user to perform an action all the time.
            *  One of these actions will be a break to exit the loop. 
            */       
            while(!exit) 
            {
                // Show the menu so the user can pick what activity want to perform.
                System.out.println(MAIN_MENU);
                
                // Read the input.
                selection = s.nextInt();
                
                // Select the appropriate action.
                switch (selection) 
                {
                    case 1: p.addPlant();
                            break;
                    case 2: System.out.println("You picked "+ selection);
                            break;
                    case 3: System.out.println("You picked "+ selection);
                            break;
                    case 4: System.out.println("You picked "+ selection);
                            break;
                    case 5: System.out.println("You picked "+ selection);
                            break;
                    case 6: System.out.println("You picked "+ selection);
                            break;
                    case 7: exit=true;
                            break;
                    default:  break;                        
                }
            }       
        }    
    }
    Thanks a lot for the help :)

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Calling a method from main

    Another approach is to do very little in main. So in main you could do this:

    Java Code:
    public static void main(String[] args) {
        new PlantNursery().start();
    }
    
    public void start() {
       // rest of code here
    }
    You may then access any instance fields or methods of the enclosing class without the need to qualify them with a reference.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    17
    Rep Power
    0

    Default Re: Calling a method from main

    Quote Originally Posted by jim829 View Post
    Another approach is to do very little in main. So in main you could do this:
    Thanks a lot Jim. I decided to go with wsaryada's suggestion because I already implemented it. I will take note though. :)

    by the way I am having some issues with the rest of the code. I am not sure if I should start a new thread for it, since it's closely related to this project. Admins can you please advise? Thanks.

    Basically I am getting an error "Malformed expression" in all of this lines that use get().

    Like in the edit method or the remove one.

    Java Code:
    plantList.get(index).maxHeighFeet= s.nextInt();
    Also is there an easy way to sort the items. I tried this sort and some advises from several website but no luck.
    Java Code:
    //        for (int i = 0; i < plantList.size(); i++) 
    //        {
    //            Plant temp;            
    //            if (plantList.get(i).commonName.compareToIgnoreCase(plantList.get(i+1).commonName)>0)
    //            {
    //                //Swap positions
    //                temp= plantList.get(i);
    //                plantList.get(i)= plantList.get(i+1);
    //                plantList.get(i+1)= temp;                
    //            }                
    //        }
    This is what I have until now

    Nursery Class
    Java Code:
    package plant.nursery;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Scanner;
    
    
    /**Class to create a plant for a nursery. 
     *
     * @author Alfredo
     */
    public class PlantNursery
    {    
        /**
         * Public variable to add and retrieve data from the ArrayList.
         */
        public static ArrayList<Plant> plantList = new ArrayList<>();
        
        
        /**
         * Method to add to the list of plants.
         */
        public void addPlant()
        {
            Scanner s = new Scanner(System.in);
            Plant p = new Plant();           
            
            //Collect the data for the plant using the console.
            System.out.println("Please enter the Max Heigh.");
            p.maxHeighFeet= s.nextInt();
            s.nextLine();
            System.out.println("Please enter the Common Name.");        
            p.commonName= s.nextLine();
            System.out.println("Please enter the Scientific Name.");
            p.scientificName= s.nextLine();
            System.out.println("Please enter the Price.");
            p.price= s.nextDouble();
            s.nextLine();
            System.out.println("Please enter if plant is fragile (true/false)");
    
            /* Get the boolean from the console. If it cannot be read, the program 
            *  will take that the plant is fragile.
            */
            try 
            {
                p.isFragile = s.nextBoolean();
            }catch (Exception e) {
                p.isFragile=true;
            }                
            // Add the new created plant to the list.
            plantList.add(p);
        }    
        
    
        /**
         * Edits a plant by searching by its name. 
         */
    
        public void editPlant()
        {
            Scanner s= new Scanner(System.in);
            String plantName;
            int index=0;
            
            // Prompt the user to enter the name of the plant.
            System.out.println("Please enter the name of the plant to edit.");
            plantName= s.nextLine();
            
            // Determine the position of the plant in the ArrayList.
            for (int i = 0; i < plantList.size(); i++) 
            {
                if ( (plantList.get(i).commonName).equalsIgnoreCase(plantName))
                    index=i;
            }
            
            // Propt the user to pick what they want to modify in the plant.
            String SUB_MENU="\n"+ "Type the number of the property to modify." + "\n"+
                            "1  Maximum Height."+ "\n"+
                            "2  Common Name."+ "\n"+
                            "3  Scientific Name."+ "\n"+
                            "4  Price."+ "\n" +
                            "5  Fragile."+ "\n"+
                            "6  Cancel"+ "\n";
            
            System.out.println(SUB_MENU);
            switch (s.nextInt())
            {
                case 1: System.out.println("Please enter the new Maximum Height:");
                        plantList.get(index).maxHeighFeet= s.nextInt();
                        s.nextLine();
                        break;
                case 2: System.out.println("Please enter the new Common Name:");
                        plantList.get(index).commonName= s.nextLine();
                        break;
                case 3: System.out.println("Please enter the new Scientific Name:");
                        s.nextLine();
                        plantList.get(index).scientificName= s.nextLine();
                        break;
                case 4: System.out.println("Please enter the new Price:");
                        plantList.get(index).price= s.nextDouble();
                        s.nextLine();
                        break;
                case 5: System.out.println("Please enter if plant is fragile (true/false):");
                        try 
                        {
                            plantList.get(index).isFragile = s.nextBoolean();
                        }catch (Exception e) {
                           // Do not modify anything if the value was not correct
                        }       
                        break;
                case 6: break;
                default:break;              
            }
        }
        
        /**
         * Procedure to remove the specified plant from the list.
         */
        public void removePlant()
        {
            String plantName;
            String confirmation;
            Scanner s = new Scanner(System.in);        
            
            System.out.println("Please enter the name of the plant to edit.");
            plantName= s.nextLine();
            
            for (int i = 0; i < plantList.size(); i++) 
            {
                if (plantList.get(i).commonName.equalsIgnoreCase(plantName))
                {
                    // Confirm that the user wants to remove the plant.
                    System.out.println("Are you sure you want to remove "+ plantList.get(i).commonName +"? Type 'yes/ no'\n"+
                                       "This operation can not be undone.");
                confirmation= s.nextLine();
                
                if (confirmation.equalsIgnoreCase("yes"))
                    plantList.remove(i);        
                }
            }        
        }
        
        public void sort()
        {
    //        for (int i = 0; i < plantList.size(); i++) 
    //        {
    //            Plant temp;            
    //            if (plantList.get(i).commonName.compareToIgnoreCase(plantList.get(i+1).commonName)>0)
    //            {
    //                //Swap positions
    //                temp= plantList.get(i);
    //                plantList.get(i)= plantList.get(i+1);
    //                plantList.get(i+1)= temp;                
    //            }                
    //        }        
            //Collections.sort(plantList);
        }
        
        
        /**
         * Code to print the array to the console.
         * @param sortType String that will define the variable for the tittle.
         *                 'price' Adds ' sorted by Price ' to the tittle.
         *                 'name' Adds ' sorted by Common Name ' to the tittle.
         *                 'scientific' Adds ' sorted by Scientific Name ' to the tittle.
         */         
        public void printList(String sortType)
        {
            String stringType;
            switch( sortType.toLowerCase())
            {
                case "price": stringType= " sorted by Price." ;
                    break;
                case "name":  stringType= " sorted by Common Name.";
                    break;
                case "scientific": stringType= " sorted by Scientific Name.";
                    break;
                default: stringType= ".";
                    break;
            }
            
            
            System.out.println("\n"+ "List of Plants"+ stringType);
            for (Plant plant : plantList) 
            {            
                System.out.println(plant.toString());
            }
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) 
        {
            // TODO code application logic here
           String MAIN_MENU="\n"+ "Type a task number below and press enter:" + "\n"+
                            "1  Add a new plant."+ "\n"+
                            "2  Edit plant."+ "\n"+
                            "3  Remove plant."+ "\n"+
                            "4  Print plants sorted by price."+ "\n" +
                            "5  Print plants sorted by scientific name."+ "\n"+
                            "6  Print plants sorted by common name."+ "\n"+
                            "7  Quit."+ "\n";
           
           Scanner s = new Scanner(System.in);
           int selection;
           boolean exit=false;
           PlantNursery pNursery = new PlantNursery();
           
           /** Infinite loop to prompt the user to perform an action all the time.
            *  One of these actions will be a break to exit the loop. 
            */       
            while(!exit) 
            {
                // Show the menu so the user can pick what activity want to perform.
                System.out.println(MAIN_MENU);
                
                // Read the input.
                selection = s.nextInt();
                
                // Select the appropriate action.
                switch (selection) 
                {
                    case 1: pNursery.addPlant();
                            break;
                    case 2: pNursery.editPlant();
                            break;
                    case 3: pNursery.removePlant();
                            break;
                    case 4: pNursery.printList("default");//Code for sorting goes here.
                            break;
                    case 5: System.out.println("You picked "+ selection);
                            break;
                    case 6: System.out.println("You picked "+ selection);
                            break;
                    case 7: exit=true;
                            break;
                    default:break;                        
                }
            }       
        }    
    }
    Plant Class
    Java Code:
    package plant.nursery;
    
    import java.util.Comparator;
    
    /**
     *
     * @author Alfredo
     */
    public class Plant
    {
       //Variables for the class properties.
        int maxHeighFeet;
        String commonName;
        String scientificName;
        double price;
        boolean isFragile;    
        
        /** 
         * Default Constructor.
         */
        public Plant()
        {
            this.maxHeighFeet = 0;
            this.commonName = "Common Name";
            this.scientificName = "Scientific Name";
            this.price = 0.0;
            this.isFragile = true;
        }    
        
        /**Constructor with Parameters.
         * 
         * @param maxHeighFeet Maximum heigh the plant can grow.
         * @param commonName Common name for the plant.
         * @param scientificName Scientific Name for the plant.
         * @param price Price for selling the plant.
         * @param isFragile If the plant is fragile or not if not provided it will be 'Fragile' by default.
         */
        public Plant(int maxHeighFeet, String commonName, String scientificName, double price, boolean isFragile)
        {
            this.maxHeighFeet = maxHeighFeet;
            this.commonName = commonName;
            this.scientificName = scientificName;
            this.price = price;
            this.isFragile = isFragile;
        }      
    
        public String getCommonName()
        {
            return commonName;
        }
    
        public void setCommonName(String commonName)
        {
            this.commonName = commonName;
        }
    
        public String getScientificName()
        {
            return scientificName;
        }
    
        public void setScientificName(String scientificName)
        {
            this.scientificName = scientificName;
        }
    
        public double getPrice()
        {
            return price;
        }
    
        public void setPrice(double price)
        {
            this.price = price;
        }
        
        
        @Override
        public String toString()
        {
            return "Plant{" + "maxHeighFeet=" + maxHeighFeet + ", commonName=" + commonName + ", scientificName=" + scientificName + ", price=" + price + ", isFragile=" + isFragile + '}';
        }
        
        public double compareTo(Plant comparePlant) 
        {
            double comparePrice = ((Plant) comparePlant).getPrice(); 
    
            //ascending order
            return this.price - comparePrice;
    
            //descending order
            //return compareQuantity - this.quantity;
        }
        
    }
    Thanks guys :)
    Last edited by fredlo2010; 01-26-2014 at 09:58 PM.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Calling a method from main

    In general, you should not have a method call on the left side of an equals (you can do it if references are involved but IMHO it's not very good technique).
    I could not locate your get() method so I don't know what you are returning.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    17
    Rep Power
    0

    Default Re: Calling a method from main

    Hi,

    I am sorry for not been more specific I will get the error which by the way will not cause the program to stop I will be able to see it only if I step into the code.

    For example in this method. I should b able to edit a specific Plant in the ArrayList. Once the index of that element has been found them modify it. I was talking about the ArrayList.get(i) method.


    Java Code:
     public void editPlant()
        {
            Scanner s= new Scanner(System.in);
            String plantName;
            int index=0;
            
            // Prompt the user to enter the name of the plant.
            System.out.println("Please enter the name of the plant to edit.");
            plantName= s.nextLine();
            
            // Determine the position of the plant in the ArrayList.
            for (int i = 0; i < plantList.size(); i++) 
            {
                if ( (plantList.get(i).commonName).equalsIgnoreCase(plantName))
                    index=i;
            }
            
            // Propt the user to pick what they want to modify in the plant.
            String SUB_MENU="\n"+ "Type the number of the property to modify." + "\n"+
                            "1  Maximum Height."+ "\n"+
                            "2  Common Name."+ "\n"+
                            "3  Scientific Name."+ "\n"+
                            "4  Price."+ "\n" +
                            "5  Fragile."+ "\n"+
                            "6  Cancel"+ "\n";
            
            System.out.println(SUB_MENU);
            switch (s.nextInt())
            {
                case 1: System.out.println("Please enter the new Maximum Height:");
                        plantList.get(index).maxHeighFeet= s.nextInt(); // error in this line 
                        s.nextLine();
                        break;
                case 2: System.out.println("Please enter the new Common Name:");
                        plantList.get(index).commonName= s.nextLine(); // error in this line 
                        break;
                case 3: System.out.println("Please enter the new Scientific Name:");
                        s.nextLine();
                        plantList.get(index).scientificName= s.nextLine(); // error in this line 
                        break;
                case 4: System.out.println("Please enter the new Price:");
                        plantList.get(index).price= s.nextDouble(); // error in this line 
                        s.nextLine();
                        break;
                case 5: System.out.println("Please enter if plant is fragile (true/false):");
                        try 
                        {
                            plantList.get(index).isFragile = s.nextBoolean(); // error in this line 
                        }catch (Exception e) {
                           // Do not modify anything if the value was not correct
                        }       
                        break;
                case 6: break;
                default:break;              
            }
        }
    Thanks a lot for all the help.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,602
    Rep Power
    5

    Default Re: Calling a method from main

    Silly me. I thought that plantList was an instance of the class, not ArrayList. So yes, get is fine but assigning it the way you are is incorrect. You would have to use a combination of sets and gets. But there is a better way to sort. Use the Collections.sort method. Pass an instance of Comparator of the types of classes you are going to sort. Then the sort method will use that method to sort the list for you. Check out the API for more detail.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    fredlo2010 is offline Member
    Join Date
    Jan 2014
    Posts
    17
    Rep Power
    0

    Default Re: Calling a method from main

    Quote Originally Posted by jim829 View Post
    Silly me.
    LOL you are genius man. Thanks for helping me with this. I have it all working now. I created a new instance of the Plant class and modified everything there and then modified the Plant item in the collection.

    As for the Sorting I followed this post and I got it working but I am not sure that's what my professor is expecting so I will check back with him. Everything works now though.

    Again thanks a lot.

    This is my editPlant Method fixed:

    Java Code:
        public void editPlant()
        {
            Scanner s= new Scanner(System.in);
            Plant tempPlant = new Plant();
            String plantName;
            int index=0;
            
            // Prompt the user to enter the name of the plant.
            System.out.println("Please enter the name of the plant to edit.");
            plantName= s.nextLine();
            
            // Determine the position of the plant in the ArrayList.
            for (int i = 0; i < plantList.size(); i++) 
            {
                if ( (plantList.get(i).commonName).equalsIgnoreCase(plantName))
                    index=i;
            }
            
            // Get the plant from the collection and assign it to the temporary plant.
            tempPlant= plantList.get(index);
            
            // Propt the user to pick what they want to modify in the plant.
            String SUB_MENU="\n"+ "Type the number of the property to modify." + "\n"+
                            "1  Maximum Height."+ "\n"+
                            "2  Common Name."+ "\n"+
                            "3  Scientific Name."+ "\n"+
                            "4  Price."+ "\n" +
                            "5  Fragile."+ "\n"+
                            "6  Cancel"+ "\n";
            
            System.out.println(SUB_MENU);
            switch (s.nextInt())
            {
                case 1: System.out.println("Please enter the new Maximum Height:");
                        tempPlant.maxHeighFeet= s.nextInt();
                        s.nextLine();
                        break;
                case 2: System.out.println("Please enter the new Common Name:");
                        tempPlant.commonName= s.nextLine();
                        break;
                case 3: System.out.println("Please enter the new Scientific Name:");
                        s.nextLine();
                        tempPlant.scientificName= s.nextLine();
                        break;
                case 4: System.out.println("Please enter the new Price:");
                        tempPlant.price= s.nextDouble();
                        
                        s.nextLine();
                        break;
                case 5: System.out.println("Please enter if plant is fragile (true/false):");
                        try 
                        {
                            tempPlant.isFragile = s.nextBoolean();
                        }catch (Exception e) {
                           // Do not modify anything if the value was not correct
                        }       
                        break;
                case 6: break;
                default:break;              
            }
            
            // Insert the plant back in its posssiiton in the ArrayList.
            plantList.set(index, tempPlant);
        }
    and two comparators for the Plant class that will allow me to sort.
    Java Code:
     static final Comparator<Plant> CommonNameOrder = new Comparator<Plant>() 
        {
            @Override
            public int compare(Plant p1, Plant p2) 
            {
                return p1.getCommonName().compareTo(p2.getCommonName());
            }
        };
      
         
        static final Comparator<Plant> PriceOrder = new Comparator<Plant>() 
        {
            @Override
            public int compare(Plant p1, Plant p2) 
            {
                return Double.compare(p1.getPrice(), p2.getPrice());
            }
        };
    Last edited by fredlo2010; 01-27-2014 at 05:18 AM.

Similar Threads

  1. Calling a method from main
    By speedbump164 in forum New To Java
    Replies: 12
    Last Post: 07-01-2013, 10:04 PM
  2. Calling args from main method
    By bogojevicka in forum New To Java
    Replies: 3
    Last Post: 01-16-2012, 09:34 PM
  3. Calling args from main method
    By bogojevicka in forum Suggestions & Feedback
    Replies: 2
    Last Post: 01-16-2012, 08:33 PM
  4. Calling into the main method
    By vcardell in forum New To Java
    Replies: 4
    Last Post: 11-02-2011, 12:16 AM
  5. Calling main method
    By eva in forum New To Java
    Replies: 7
    Last Post: 11-06-2009, 01:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •