Question: this is the problem/ someone tried to help me with this already but sent it back with even more errors/ using bluejay After a long

this is the problem/ someone tried to help me with this already but sent it back with even more errors/ using bluejay

After a long and tedious journey of helping Alexander and Elizabeth we feel weve done our part in helping and plan to get on the road in the morning. Our bags were packed and set at the edge of the bed waiting for us to awake, but when we wake up our bags are missing! It seems thieves came in through the open window last night and stole our belongings. Before we can get back on the road we need to gather some money in order to purchase new equipment. To help raise money we have talked to a local shop owner and he mentioned that he liked Alexanders flower pack, but would like it to hold more than just flowers. The new flower pack should hold plants which can be flowers, fungus or weed. Create a plant class that has three child classes (flower, fungus and weed). o Each subclass shares certain qualities (ID and Name) The ID should be something the program creates such as a sequential number; the user should not be asked to indicate the ID o Flower traits include (Color, Thorns, and Smell) o Fungus traits include (Color and Poisonous) o Weed traits include (Color, Poisonous, Edible and Medicinal) All items should be able to be displayed. Be able to add, remove, search and filter these plants by name. For traits that are Yes or No such as Is the Weed Poisonous? consider using Booleans. Dont use a string for all traits. Submit 5 files: Final.java, Flower.java, Fungus.java, Plant.java and Weed.java

THIS IS MY CODE, I HAVE ONE ERROR AND I DONT KNOW WHAT IS WRONG, PLEASE HELP MY PROBLEM IS IN MY FINAL CLASS

FINAL CLASS********************

import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Scanner; public class Final {

private Scanner input = new Scanner(System.in); protected ArrayList myBag = new ArrayList(); public static void main (String[] args) { new Bag(); } public Bag(){ System.out.println("Plant inventory program, select an option"); while(true) { System.out.println("1: Add plant"); System.out.println("2: Remove plant"); System.out.println("3: Search plant by name"); System.out.println("4: Filter/Print plant by name"); System.out.println("5: Save bag to file: "); System.out.println("6: Load bag from file: "); System.out.println("7: Exit"); int menu = input.nextInt(); switch(menu) { case 1: addPlant(); break; case 2: removePlant(); break; case 3: searchPlant(); break; case 4: filterPlant(); break; case 5: savePlantFile(myBag); break; case 6: loadPlantFile(); break; case 7: System.exit(0); } } } public void addPlant () { System.out.println("Select type of plant"); System.out.println("1: Flower"); System.out.println("2: Fungus"); System.out.println("3: Weed"); int id = input.nextInt(); switch(id) { case 1: this.plantBuilder(new Flower(), myBag, id); break; case 2: this.plantBuilder(new Fungus(), myBag, id); break; case 3: this.plantBuilder(new Weed(), myBag, id); break; } } public void removePlant () { System.out.print("To delete a plant enter it's name: "); String deletePlant = input.next().toUpperCase(); // User selection int toDeleteHash = deletePlant.hashCode(); // generate hashcode from user word for (int index = 0 ; index < myBag.size() ; index++) {// start loop through arrayList int myBagHash = myBag.get(index).getPlantName().hashCode(); if (myBagHash == toDeleteHash) { myBag.remove(index); break; } else// equal not found System.out.println("Sorry, no plant by the name " + deletePlant + " was found, Try again. "); } }

public void searchPlant () { System.out.print("To search a plant enter it's name: "); String findPlant = input.next().toUpperCase(); int findHash = findPlant.hashCode(); for (int find = 0 ; find < myBag.size() ; find++) { int myBagHash = myBag.get(find).getPlantName().hashCode(); if (myBagHash == findHash) { System.out.println("Found a " + findPlant); } else System.out.println("A plant by the name, " + findPlant + " doesn't exist in the bag."); } }

public void filterPlant () { System.out.printf("FlowerID%4sFlower Name ",""); for (int i = 0 ; i < myBag.size() ; i++) { System.out.println(myBag.get(i).getPlantID() + "\t" + myBag.get(i).getPlantName() ); } }

public void plantBuilder (Object plant , ArrayList myBag, int id) {

if (plant instanceof Flower &! (plant instanceof Fungus || plant instanceof Weed)) { Flower flower = (Flower) plant; flower.setPlantID(id); flower.setPlantName(); flower.setPlantColor(); flower.setPlantThorns(); flower.setPlantSmell(); myBag.add(flower); }

if (plant instanceof Fungus &! (plant instanceof Weed)) { Fungus fungus = (Fungus) plant; fungus.setPlantID(id); fungus.setPlantName(); fungus.setPlantColor(); fungus.setIsPoisonous(); myBag.add(fungus); }

if (plant instanceof Weed) { Weed weed = (Weed) plant; weed.setPlantID(id); weed.setPlantName(); weed.setPlantColor(); weed.setIsEdible(); weed.setIsMedicinal(); myBag.add(weed); } } private File filePath; private String filename= "plantData.txt"; public void createPlantFile() { filePath = new File(filename); try { writeToFile = new PrintWriter(filePath); } catch (FileNotFoundException e) { System.out.println("Cannot create file."); } } private PrintWriter writeToFile; public void savePlantFile(ArrayList myBag ) { createPlantFile(); for (int f = 0 ; f < myBag.size() ; f++) { writeToFile.print(myBag.get(f).getPlantID() + " " + myBag.get(f).getPlantName() + " ");

Object plant = myBag.get(f); if(plant instanceof Flower &!(plant instanceof Fungus || plant instanceof Weed)) { Flower flower = (Flower) plant; writeToFile.println(flower.getPlantColor() + " " +flower.getPlantThorns() + " " +flower.getPlantThorns()); } else if(plant instanceof Fungus &!(plant instanceof Weed) ) { Fungus fungus = (Fungus) plant; writeToFile.println(fungus.getPlantColor() + " " +fungus.getIsPoisonous() ); } else { Weed weed = (Weed) plant; writeToFile.println(weed.getPlantColor()+ " " + weed.getIsEdible()+ " " + weed.getIsMedicinal() ); } } closePlantFile(); } public void closePlantFile() { writeToFile.close(); } public void loadPlantFile() { filePath = new File(filename); try{ Scanner input = new Scanner(filePath); readPlantFile(input); } catch (FileNotFoundException e) { System.out.println("Unable to access the file "); } }

public void readPlantFile(Scanner input) { try { while (input.hasNextLine()) {

String id = input.next().toUpperCase(); if (id.startsWith("FLOWER") ) { Flower flower = new Flower(); flower.setPlantID(id); flower.setPlantName(input.next()); flower.setPlantColor(input.next()); flower.setPlantThorns(input.next()); flower.setPlantSmell(input.next()); myBag.add(flower); }

if (id.startsWith("FUNGUS")) { Fungus fungus = new Fungus(); fungus.setPlantID(id); fungus.setPlantName(input.next()); fungus.setPlantColor(input.next()); fungus.setIsPoinsonous(input.next()); myBag.add(fungus); }

if (id.startsWith("WEED")) { Weed weed = new Weed(); weed.setPlantID(id); weed.setPlantName(input.next()); weed.setPlantColor(input.next()); weed.setIsEdible(input.next()); weed.setIsMedicinal(input.next()); myBag.add(weed); } } } catch (Exception e) {

System.out.println("End of File "); } } }

FLOWER CLASS******

public class Flower extends Plant { private String plantColor; private String plantSmell; private String plantThorns; public Flower () { this.getPlantColor(); this.getPlantThorns(); this.getPlantSmell(); } public Flower (String c, String t, String s) { plantColor= c; plantThorns= t; plantSmell = s; } public void setPlantColor() { System.out.print("Assign a color: "); this.plantColor = input.next().toUpperCase(); } public void setPlantColor(String color) { this.plantColor= color; } public String getPlantColor() { return plantColor; } @SuppressWarnings("static-access") public void setPlantThorns() { System.out.print("Are there thorns? Yes or No: "); this.plantThorns= input.next().valueOf("YES").toUpperCase(); } public void setPlantThorns(String t) { this.plantThorns= t; } public String getPlantThorns() { return plantThorns; } public void setPlantSmell() { System.out.print("How does the flower smell? "); this.plantSmell= input.next().toUpperCase(); } public void setPlantSmell(String s) { this.plantSmell = s; } public String getPlantSmell() { return plantSmell; } }

WEED CLASS************************************************************************* public class Weed extends Flower { private String isEdible; private String isMedicinal; public Weed () { isEdible = ""; isMedicinal = ""; } public Weed(String e, String m) { isEdible= e; isMedicinal= m; } @SuppressWarnings("static-access") public void setIsEdible() { System.out.print("Is the plant edible, Yes or No? "); this.isEdible = input.next().valueOf("YES").toUpperCase(); } public void setIsEdible(String e) { this.isEdible = e; } public String getIsEdible() { return isEdible; } @SuppressWarnings("static-access") public void setIsMedicinal() { System.out.print("Is there medicinal value, Yes or No? "); this.isMedicinal= input.next().valueOf("YES").toUpperCase(); } public void setIsMedicinal(String m) { this.isMedicinal = m; } public String getIsMedicinal() { return isMedicinal; } }

FUNGUS CLASS***********************************************

public class Fungus extends Flower { private String isPoisonous; public Fungus () { isPoisonous = ""; } public Fungus (String p) { isPoisonous = p; } @SuppressWarnings("static-access") public void setIsPoisonous() { System.out.print("Is the plant poisonous, Yes or NO? "); this.isPoisonous = input.next().valueOf("YES").toUpperCase(); } public void setIsPoinsonous(String p) { this.isPoisonous = p; } public String getIsPoisonous() { return isPoisonous; } }

PLANT CLASS******************************

import java.util.Scanner; public class Plant {

private String plantID; private String plantName; protected Scanner input = new Scanner(System.in); public void setPlantID(int type) { switch (type) { case 1: this.plantID = "FLOWER"; break; case 2: this.plantID = "FUNGUS"; break; case 3: this.plantID = "WEED"; break; } } public void setPlantID(String type) { plantID = type; } public String getPlantID() { return plantID; } public void setPlantName() { System.out.print("Assign a name: "); this.plantName = input.next().toUpperCase(); } public void setPlantName(String name) { plantName = name; } public String getPlantName() { return plantName; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!