Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By jim829

Thread: Editing an ArrayList object

  1. #1
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Editing an ArrayList object

    So I'm Half way done with this assignment and all I need to is edit and sort my directory. What I've been trying to do as of now is edit my directory. I've tried to use the set function in the Array List, Iterator List etc but I just don't know how implement them mainly because I keep thinking "how can check which variable in the directory the user wants to change(Name, cost etc etc)?". If you also have any suggestions on how to make this better, please add that to your comment as well.

    Main class
    Java Code:
    package plantnursery;
    import java.util.Scanner;
    import java.util.ArrayList;
    
    public class PlantNursery 
    {
        private ArrayList<Plant> plantDirectory = new ArrayList<>();
        private static Scanner read = new Scanner(System.in);
        
        public static void main(String[] args) 
        {
            PlantNursery process = new PlantNursery();
            int option;
            
            do
            {
                System.out.println("\nEnter the choice that you would like to do.\n");
                System.out.println("1.) Add plants to the directory.");
                System.out.println("2.) View your directory.");
                System.out.println("3.) Edit your directory.");
                System.out.println("4.) Quit the program.");
                option = read.nextInt();
                
                switch(option)
                {
                    case 1:
                        process.createPlant();
                        break;
                    case 2:
                        process.printDirectory();
                        break; 
                }
            }
            while(option != 4);
        }
        
        public void createPlant()
        {
            
            int height; 
            String comName, sciName;
            double cost;
            boolean caution;
            
            System.out.println("\nWhat's the maximum height of the plant?");
            height = read.nextInt();
            read.nextLine();
            
            System.out.println("\nWhat's the common name for this plant?");
            comName = read.nextLine();
            
            System.out.println("\nWhat's the scientific name for this plant?");
            sciName = read.nextLine();
            
            System.out.println("\nHow much does this plant cost?");
            cost = read.nextDouble();
            
            System.out.println("\nTrue or false, Is the plant easily breakable?");
            caution = read.nextBoolean();
            
            Plant plant = new Plant(height, comName, sciName, cost, caution);
            plantDirectory.add(plant);
        }
        
        public void printDirectory()
        {
            for(Plant plant : plantDirectory) 
            {
                System.out.println(plant);
            }
        }   
    }


    Plant Class
    Java Code:
    package plantnursery;
    
    public class Plant 
    {
        private int maxHeight;
        private String commonName;
        private String scientificName;
        private double price;
        private boolean fragile;
        
        public Plant(int height, String comName, String sciName, double cost, boolean caution)
        {
            maxHeight = height;
            commonName = comName;
            scientificName = sciName;
            price = cost;
            fragile = caution;
        }
     
        @Override
        public String toString()
        {
            return "\n" + maxHeight + "\n" + commonName + "\n" + scientificName + "\n" + price + "\n" + fragile;
        }
    }
    Last edited by Deathslice; 01-30-2015 at 06:05 PM.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Editing an ArrayList object

    You could make menu option 3: Edit plant. This should present a list of Plants in the plantDirectory. To iterate over the plantDirectory, multiple options exist:

    Java Code:
    for (int i = 0 ; i < plantDirectory.size(); i++ )  {
       Plant plant = plantDirectory.get(i);
       //print i + plant
    }
    Advantage: you want to present a number to the user to select. You can use "i" to display it.

    Java Code:
    int i =0;
    for( Plant plant : plantDirectory) { 
       //print i + plant
       i++;
    }
    Shorter, but you have to create a number yourself, or use plantDirectory.indexOf(plant), but that's kinda expensive (and ultimately you would need a proper equals(Object) and hashcode() for that).

    Java Code:
    int i = 0;
    for (Iterator it = plantDirectory.iterator(); iterator.hasNext(); ) { 
       Plant plant = it.next();
       //print i + plant
       i++;
    }
    The only advantage this last construction has, is that you can call it.remove() to remove a Plant from the list without throwing a ConcurrentModificationException.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Editing an ArrayList object

    Quote Originally Posted by SurfMan View Post
    You could make menu option 3: Edit plant. This should present a list of Plants in the plantDirectory.
    But option 2) should already show that list. Perhaps this edit plant function should only be shown when you view the list of plants first and not in the main menu.

    Me, I'm missing an option. "Search". Listings are well and good but a proper directory tends to have hundreds of thousands of entries. Generally you'll want to find a specific one. Or maybe a family of plants, for which this directory has no support at all.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Editing an ArrayList object

    Sometimes, folks tend to forget that main() is simply a static method. So instead of having a while loop checking to see if option != 4, simply have a case 4 which has a return instead of a break. I also recommend a default case to alert the user if they have not entered a valid choice.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Editing an ArrayList object

    This is what I was able to come up with but it doesn't work possibly because getCommonName returns nothing.

    Java Code:
    public void editDirectory()
        {
            Plant current_plant = null;
            String plantName;
            
            System.out.println("\nEnter the common name of the plant you which to edit.");
            read.next();
            plantName = read.nextLine();
         
            for(int i=0; i<plantDirectory.size(); i++)
            {
                current_plant = plantDirectory.get(i);
                
                if(current_plant.getCommonName().equals(plantName))
                {
                  // This is not the only thing that is going to get edited. This is just a test to see if it works and that is why I'm hard coding it.
                  current_plant.setHeight(100);
                }
                else
                {
                    System.out.println("\nThis name doesn't exist in my database.");
                    System.out.println("Returning to the main menu");
                }
            }
        }






    Here is the full code

    Main class
    Java Code:
    package plantnursery;
    import java.util.Scanner;
    import java.util.ArrayList;
    
    public class PlantNursery 
    {
        private ArrayList<Plant> plantDirectory = new ArrayList<>();
        private static Scanner read = new Scanner(System.in);
        
        public static void main(String[] args) 
        {
            PlantNursery process = new PlantNursery();
            int option;
            
            do
            {
                System.out.println("\nEnter the choice that you would like to do.\n");
                System.out.println("1.) Add plants to the directory.");
                System.out.println("2.) View your directory.");
                System.out.println("3.) Edit your directory.");
                System.out.println("4.) Quit the program.");
                option = read.nextInt();
                
                switch(option)
                {
                    case 1:
                        process.createPlant();
                        break;
                    case 2:
                        process.printDirectory();
                        break; 
                    case 3:
                        process.editDirectory();
                        break;
                }
            }
            while(option != 4);
        }
        
        public void createPlant()
        {
            
            int height; 
            String comName, sciName;
            double cost;
            boolean caution;
            Plant plant = new Plant();
            
            System.out.println("\nWhat's the maximum height of the plant?");
            height = read.nextInt();
            plant.height(height);
            read.nextLine();
            
            System.out.println("\nWhat's the common name for this plant?");
            comName = read.nextLine();
            
            System.out.println("\nWhat's the scientific name for this plant?");
            sciName = read.nextLine();
            
            System.out.println("\nHow much does this plant cost?");
            cost = read.nextDouble();
            
            System.out.println("\nTrue or false, Is the plant easily breakable?");
            caution = read.nextBoolean();
            plant.fragile(caution);
            
            plant = new Plant(height, comName, sciName, cost, caution);
            plantDirectory.add(plant);
        }
        
        public void printDirectory()
        {
            for(Plant plant : plantDirectory) 
            {
                System.out.println(plant);
            }
        }   
        
        public void editDirectory()
        {
            Plant current_plant = null;
            String plantName;
            
            System.out.println("\nEnter the common name of the plant you which to edit.");
            read.next();
            plantName = read.nextLine();
         
            for(int i=0; i<plantDirectory.size(); i++)
            {
                current_plant = plantDirectory.get(i);
                
                if(current_plant.getCommonName().equals(plantName))
                {
                  current_plant.setHeight(100);
                }
                else
                {
                    System.out.println("\nThis name doesn't exist in my database.");
                    System.out.println("Returning to the main menu");
                }
            }
        }
    }

    Plant Class
    Java Code:
    package plantnursery;
    
    public class Plant 
    {
        private int maxHeight;
        private String commonName;
        private String scientificName;
        private double price;
        private boolean fragile;
        
        public Plant(int height, String comName, String sciName, double cost, boolean caution)
        {
            maxHeight = height;
            commonName = comName;
            scientificName = sciName;
            price = cost;
            fragile = caution;
        }
        
        public Plant()
        {
            
        }
        
        @Override
        public String toString()
        {
            return "\n" + maxHeight + "\n" + commonName + "\n" + scientificName + "\n" + price + "\n" + fragile;
        }
        
        public void fragile(boolean carefulness)
        {
            if(carefulness == true)
            {
                System.out.println("\nPlease be careful with this plant.");
            }
            else
                System.out.println("\nDon't worry, this plant can take a beating.");
        }
        
        public void height(int height)
        {
            if(height > 100)
            {
                System.out.println("\nThis plant is one of the tallest plants in the world");
            }
            else if(height <= 10)
            {
                System.out.println("\nI didn't know this even existed seeing that it's so short.");
            }
            else
                System.out.println("\nA fine plant this is.");
        }
        
        public String getCommonName()
        { 
            return commonName;
        }
        
        public void setHeight(int maxHeight)
        {
           this.maxHeight = maxHeight;
        }
    
        public void setPrice(double price)
        {
           this.price = price;
        }
        
        public void setCommonName(String commonName)
        {
           this.commonName = commonName;
        }
        
        public void setScientificName(String scientificName)
        {
           this.scientificName = scientificName;
        }
        
        public void setFragile(boolean fragile)
        {
           this.fragile= fragile;
        }
    }
    Last edited by Deathslice; 02-01-2015 at 07:36 PM.

  6. #6
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Editing an ArrayList object

    Anybody has ideas on how I could make what I just created work(look at the 5th post and the first bits of code)?

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Editing an ArrayList object

    At lines 7 and 8 you have a read.next() and a read.nextLine(). The first reads a token which you don't store anywhere. The last reads the rest of the line which you store in plantName. I doubt that is what you want. You need to learn to sprinkle print statements throughout your code to help debug it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Editing an ArrayList object

    The reason why there is a read.next() is because my buffer gets eaten up so this serve like a dummy variable. I think you should read my full code and then at the code that I want you to specifically look at.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Editing an ArrayList object

    Then you need to put in some print statements. Make certain that the string you enter is correct. You should even assign something from the read.next() command to see what it contains.

    Example:

    Java Code:
    String token = read.next();
    System.out.println("token = [" + token + "]");
    //
    Make certain that all variables contain the values they are expected to contain. The brackets are used to show leading and trailing white space, if any exists. All experienced programmers use print statements everywhere as a first attempt at debugging.

    Regards,
    Jim
    Deathslice likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Editing an ArrayList object

    Thank you. With your hint, I decided to print out plantName to see if it actually had the value that I expected and it didn't print it out(matter of fact it was empty). Now the plan is, how do make the program not skip my prompt. If I get rid of the read.next(), it will skip the prompt and revert back to main menu.

  11. #11
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Editing an ArrayList object

    Actually, never mind. I was able to fix it by using a read.nextLine(); and it actually printed out the content. I guess that is one fix

  12. #12
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Editing an ArrayList object

    Yes I was finally able to get this work(I can finally prompt the user to edit the arrayList. Man it took me all day but it feels good to finally be able to finish this assignment). Thanks Jim, I 'll be posting my solution soon(I'm too tired at the moment) and Thanks for that insightful hint. I'll definitely be using that technique from now on.
    Last edited by Deathslice; 02-02-2015 at 03:29 AM.

  13. #13
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

    Default Re: Editing an ArrayList object

    Hey Jim, one last thing. Since I'm done with the editing part of the arraylist and the next thing that I have to do is sort the array by name or cost, how would you go about sorting an arraylist.

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

    Default Re: Editing an ArrayList object

    Quote Originally Posted by Deathslice View Post
    Hey Jim, one last thing. Since I'm done with the editing part of the arraylist and the next thing that I have to do is sort the array by name or cost, how would you go about sorting an arraylist.
    You can use the Collections.sort() method. But you must implement the Comparable interface so the sort method knows the criteria on which to sort. You can read about sorting here --> Object Ordering (The Java Tutorials > Collections > Interfaces). My recommendation when trying something new (in this case sorting) is to write a separate program with a simple class to just get familiar with how the sorting process works. Then you can apply what you learned to your main project.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    Deathslice is offline Member
    Join Date
    Jan 2015
    Location
    Miami, FL
    Posts
    86
    Rep Power
    0

Similar Threads

  1. Getting value of Object[] in an ArrayList
    By adela in forum New To Java
    Replies: 4
    Last Post: 01-28-2011, 08:13 AM
  2. Replies: 2
    Last Post: 11-16-2010, 05:51 PM
  3. Replies: 3
    Last Post: 03-15-2010, 08:44 PM
  4. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  5. object instantiation and arrayList
    By lockmac in forum New To Java
    Replies: 5
    Last Post: 08-09-2007, 06:25 PM

Tags for this Thread

Posting Permissions

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