Question: public boolean addPlant(String plantName, int row) Adds plant (based on the five options) to designated row. If plant needs meet row conditions, the plant will
public boolean addPlant(String plantName, int row)
Adds plant (based on the five options) to designated row. If plant needs meet row conditions, the plant will be added. If not, then the plant will not be added to the row.
Returns:
true if plant was successfully added, false if unsuccessful
Code given:
public boolean addPlant(String plantName, int row) { if(canPlant(plantName, searchRow(row))){ searchRow(row).setPlant(searchPlant(plantName)); System.out.println(plantName.toUpperCase() + " added to row " + row + "!"); return true; } else{ System.out.println(plantName.toUpperCase() + " was unable to be planted."); return false; } }
public boolean canPlant(String plantName, GardenRow row){ GardenPlant plant = searchPlant(plantName); if (plant == null || row == null) return false; boolean goodSun = row.getLight().equals(plant.getAmountOfSunshine()) || plant.getAmountOfSunshine().equals("any"); boolean correctPH = (plant.getpHLow() <= row.getPH()) && (row.getPH() <= plant.getpHHigh());
// This next part is a very *robust* version // and you will notice more drawn out, so you can see the steps I took GardenRow otherA = null, otherB = null; if (row.equals(one)) { otherA = two; otherB = three; } else if (row.equals(two)) { otherA = one; otherB = three; } else if (row.equals(three)) { otherA = one; otherB = two; }
boolean rowCheck = plant.getType().equals("VEG") || otherA == null || otherB == null || // the other rows are empty (otherA != null && otherA.getPlant() == null) || // the rows exist but no plants (otherB != null && otherB.getPlant() == null) || (otherA != null && otherA.getPlant() != null && otherA.getPlant().getType().equals("VEG"))|| (otherB != null && otherB.getPlant() != null && otherB.getPlant().getType().equals("VEG")); return goodSun && correctPH && rowCheck; }
public void buildOptions() { daisy = new GardenPlant("daisy", 6, 8, "any", "FLOWER"); carrot = new GardenPlant("carrot", 6, 7, "full sun", "VEG"); rose = new GardenPlant("rose", 6, 7, "full sun", "FLOWER"); swissChard = new GardenPlant("swiss chard", 6, 7, "partial sun", "VEG"); begonia = new GardenPlant("begonia", 5, 6, "partial sun", "FLOWER"); }
public GardenPlant searchPlant(String plantName){ if(plantName.equals(daisy.getName())){ return daisy; } else if(plantName.equals(carrot.getName())){ return carrot; } else if(plantName.equals(rose.getName())){ return rose; } else if(plantName.equals(swissChard.getName())){ return swissChard; } else{ return begonia; } }
public GardenRow searchRow(int row){ if(row == 1){ return one; } else if(row == 2){ return two; } else{ return three; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
