0

I have an assignment for my coding class, I've done most of it, but i don't know how to do the last part.

This is the assignment "Create a class Plant for a Plant Nursery that has five attributes related to plants:

Maximum Height in feet
Common name
Scientific name
Price
Whether or not it is fragile.

Create two methods of your own choosing for the class Plant. Allow the user to create a Plant object from the console. After the plant object is created, add the object to an ArrayList of Plants.

Allow the user to edit any information about the plant objects already entered.

...And for 10 Extra Credit points!! Allow the user to view the plants sorted by price (lowest to highest), scientific name (alphabetized by genus), or common name (alphabetized by first letter of first word). Assignment is individual."

my code

    class nursery{

    private int height;
    private String cName;
    private String sName;
    private int cost;
    private boolean fragile;

    public nursery(int height, String cName, String sName, int cost, boolean fragile)
    {
        this.height=height;
        this.cName=cName;
        this.sName=sName;
        this.cost=cost;
        this.fragile=fragile;
    }
}

public class Nursery {




    public static void main(String[] args) {

        ArrayList<nursery> plant = new ArrayList<>();

        Scanner s = new Scanner(System.in);

        while(true){
            //get the plant varibles
            System.out.println("Enter the common name of the plant: ");
            String cName = s.next();
            System.out.println("Enter the scientific name of the plant: ");
            String sName = s.next();
            System.out.println("Enter the height of the plant: ");
            int height = s.nextInt();
            System.out.println("Enter whether the plant is fragile or not: ");
            boolean fragile =s.nextBoolean();
            System.out.println("Enter the price of the plant: ");
            int cost=s.nextInt();
            //add to the arraylist
            nursery Plant = new nursery(height, cName, sName, cost, fragile);
            plant.add(Plant);
            System.out.println("If u would like to stop entering press q.");
            String quit = s.next();
            //quit out if wanted
            if(quit.equals("q")||quit.equals("Q"))
            break;
        }



    }

}

what I don't know how to do is "Allow the user to edit any information about the plant objects already entered." I've tried searching but I've been unable to get an answer.

3
  • 3
    Try to name your classes using java's naming convention (Nursery instead of nursery). To answer your question, you need to give the user the possibility to say that he wants to edit an already existing plant. So instead of asking him the comon name of the plant in the first place, try asking him something like 'What do you want to do ? 1 - Add 2 - Edit'. I'm sure you'll figure out what to do next. Commented Jan 30, 2015 at 15:44
  • 1
    Okay, what is your question? Please don't just ask "how do i do it". Attempt to solve this part yourself first and if you run into issues, detail the specific problems you encounter here and ask about that instead. Commented Jan 30, 2015 at 15:44
  • 1
    As @DeadlyJesus mentioned, have the user choose to add or edit. If they choose create, do what you're doing. If they choose edit, ask them for the plant name, search in the ArrayList for that plant and replace it with the new values. Commented Jan 30, 2015 at 15:47

1 Answer 1

1

You have preserved all the nursery objects (planet) to ArrayList<nursery> plant, so what you need to do is just find it from the list and reset its values.

A general example could be like:

nursery plant_to_update = null;
for (int i=0; i<plant.length; i++){
    current_plant = plant.get(i);
    // say user want to update planet with cName as 'planet 1'
    if(plan_to_update.cName == "planet 1"){ 
        plant_to_update = current_plant;
        break;
    }
}
if( plant_to_update != null){
    // update planet 1 with new value
    plant_to_update.setHeight(50);
    plant_to_update.setCost(60);
}

and, add setters in nursery class in order to update those private members

public void setHeight(int height){
    this.height = height;
}

public void setCost(int cost){
    this.cost = cost;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.