Friday, January 30, 2015

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;
}
}
















No comments:

Post a Comment