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.
Nurseryinstead ofnursery). 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.