After a long and tedious journey of helping Alexander and Elizabeth we feel we’v
ID: 3805010 • Letter: A
Question
After a long and tedious journey of helping Alexander and Elizabeth we feel we’ve 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 Alexander’s 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). Each subclass shares certain qualities (ID and Name) Flower traits include (Color, Thorns, and Smell) Fungus traits include (Color and Poisonous) 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. Submit 5 files: Mid.java, flower.java, fungus.java, plant.java and weed.java
Explanation / Answer
created a plant class that has three child classes (flower, fungus and weed). Each subclass shares certain qualities (ID and Name) Flower traits include (Color, Thorns, and Smell) Fungus traits include (Color and Poisonous) 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
*******************)mid.java*************************
import java.io.*;
import java.util.*;
import javax.swing.*;
public class mid { //mid class
public static void main(String args) throws IOException {
Scanner input = new Scanner(System.in);
ArrayList<plant> plantPack = new ArrayList<plant>();
System.out.println("Welcume to my plant pack application.");
System.out.println("Please select the options below ");
System.out.println("");
while (true) {
System.out.println("1:Add .");
System.out.println("2:Remove.");
System.out.println("3:Search .");
System.out.println("4:Display .");
System.out.println("5:Filter .");
System.out.println("6:Add/Load a plant file. ");
System.out.println("0:Exit .");
System.out.print("Enter the option: ");
int userChoice = input.nextInt();
switch (userChoice) {
case 1:
add(plantPack);
break;
case 2:
remove(plantPack);
break;
case 3:
search(plantPack);
break;
case 4:
display(plantPack);
break;
case 5:
filter(plantPack);
break;
case 6:
saveLoad(plantPack);
break;
case 0:
System.out.println("Thannk you for using the flower
pack interface. See you again soon!");
System.exit(0);
}
}
}
/***
*
* **param plantPack***
*/
public static void add(ArrayList<plant> plantPack) {
String iD;
String name;
String color;
String smell;
String thorns;
String poisonous; String
String
String
String edible;
medicinal;
flavor;
seasonal; Scanner scan = new Scanner(System.in);
plant test = new plant();
System.out.println("Helo please make a selection. ");
System.out.println("Option 1:Add flower ");
System.out.println("Option 2:Add Fungus ");
System.out.println("Option 3:Add Weed ");
System.out.print("Answer:");
int ans = scan.nextInt(); flower."); switch (ans) {
case 1:
System.out.print("Please enter your ID number for youriD = scan.next();
System.out.print("Please enter the name of your flower. ");
name = scan.next();
System.out.print("Please enter the color of your color. ");
color = scan.next();
System.out.print("Is there any thorns? (Yes/No) ");
thorns = scan.next();
System.out.print("Is there a smell? (Yes/No) ");
smell = scan.next();
flower newFlower = new flower(iD, name, color, thorns, smell);
plantPack.add(newFlower);
System.out.println();
break;
case 2:
System.out.print("Please enter your ID number for your flower.");
iD = scan.next();
System.out.print("Please enter the name of your Fungus. ");
name = scan.next();
System.out.print("Please enter the color of your Fungus. ");
color = scan.next();
System.out.print("Is it poisonous? (Yes/No) ");
poisonous = scan.next();
fungus newFungus = new fungus(iD, name, color, poisonous);
plantPack.add(newFungus);
System.out.println();
break;
case 3:
System.out.print("Please enter your ID number for your ID.");
iD = scan.next();System.out.print(" Please enter the name of your weed. ");
name = scan.next();
System.out.print("Please enter the color of your weed. ");
color = scan.next();
System.out.print("Is it poisonous? (Yes/No) ");
poisonous = scan.next();
System.out.print("Is it edible? (Yes/No) ");
edible = scan.next();
System.out.print("Is it used for medicinal? (Yes/No) ");
medicinal = scan.next();
weed newWeed = new weed(iD, name, color, poisonous, edible,
medicinal);
plantPack.add(newWeed);
System.out.println();
break;
case 4:
System.out.print("Please enter your ID number for your ID.");
iD = scan.next();
System.out.print("Please enter the name of your herb. ");
name = scan.next();
System.out.print("Please enter the color of your herb. ");
color = scan.next();
System.out.print("Flavour : ");
flavor = scan.next();
System.out.print("Is it used for medicinal? (Yes/No) ");
medicinal = scan.next();
System.out.print(" Is it
seasonal = scan.next(); seasonal? (Yes/No) "); herb h = new herb(iD, name, color, flavor, medicinal,
seasonal);
plantPack.add(h);
System.out.println();
} break; }
private static void remove(ArrayList<plant> plantPack) {
Scanner scan = new Scanner(System.in);
String s;
System.out
remove. "); .println("Please enter plant name you would like to
System.out.print(" Ans:");
s = scan.next(); for (int i = 0; i < plantPack.size(); i++) {
plant remove = plantPack.get(i);
if (remove.getName().equalsIgnoreCase(s)) {
plantPack.remove(i);
break;
}
}
}
private static void display(ArrayList<plant> plantPack) {
System.out.println();
for (int i = 0; i < plantPack.size(); i++) {
System.out.println(plantPack.get(i)); } }
System.out.println(); public static void search(ArrayList<plant> plantPack) {
Scanner scan = new Scanner(System.in);
System.out.println("Please search for plant you would to
search for. ");
System.out.print(" Ans:");
String stringAns = scan.next();
boolean FlagAns = false;
for (int i = 0; i < plantPack.size(); i++) {
plant newPlantPack = plantPack.get(i);
if (newPlantPack.getName().equalsIgnoreCase(stringAns)) {
FlagAns = true;
break;
}
}
if (FlagAns) {
System.out.println(" Yes! " + stringAns
+ " is in the pack of plants.");
} else {
System.out.println(" Sorry! " + stringAns
+ " is not in the pack of plants.");
}
}
public static void filter(ArrayList<plant> plantPack) {
Scanner scan = new Scanner(System.in);
String keyWord;
boolean checkAns = false;
System.out
like to search for.");
System.out .println("Please enter character or word your would .println("This application will search through the list
for your result. ");
System.out.print(" Ans:");
keyWord = scan.nextLine();
for (int i = 0; i < plantPack.size(); i++) {
plant newPlantPack = plantPack.get(i);
if (newPlantPack.getName().contains(keyWord)) {
System.out.println();
System.out.println(" " + newPlantPack.getName());
checkAns = true;
}
}
if (checkAns == false) {
System.out
.println("Sorry user,but are database could not
any results..");
}
if (checkAns == true) {
System.out.println("Above is the queer of results we
located."); }
System.out.println(""); }
public static void saveLoad(ArrayList<plant> plantPack) throws IOException
{ Scanner scan = new Scanner(System.in);
System.out.println(" Option 1: Would like to SAVE your file? ");
System.out.println(" Option 2: Would like to LOAD a file? ");
System.out.print(" answer:");
int i = scan.nextInt();
plant testjr = new plant();
if (i == 1) {
File file = new File("plantFile.txt ");
FileOutputStream outFileStream = new FileOutputStream(file);
PrintWriter outStream = new PrintWriter(outFileStream);
for (int x = 0; x < plantPack.size(); x++) {
outStream.println(plantPack.get(x));
}
System.out.println();
System.out .println(" File created....PLEASE check your
desktop for plantFile.txt.");
System.out.println();
outStream.close();
}
if (i == 2) { medicinal; String iD, name, color, smell, thorns, poisonous, edible, JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(null) ==
JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
Scanner input = new Scanner(file);
while (input.hasNext()) {
String test = input.nextLine();
StringTokenizer st = new StringTokenizer(test);
int count = st.countTokens();
if (count == 4)//the fungus object
{
iD = st.nextToken();
name = st.nextToken();
color = st.nextToken();
poisonous = st.nextToken();
fungus newFungus = new fungus(iD, name,
color, poisonous);
plantPack.add(newFungus);
}
if (count == 5)// the flower object
{
iD = st.nextToken();
name = st.nextToken();
color = st.nextToken();
thorns = st.nextToken();
smell = st.nextToken();
flower newFlower = new flower(iD, name, color, thorns,
} smell);
plantPack.add(newFlower); if (count == 6) {//the weed object
iD = st.nextToken();
name = st.nextToken();
color = st.nextToken();
poisonous = st.nextToken();
edible = st.nextToken();
medicinal = st.nextToken();
weed newWeed = new weed(iD, name, color, poisonous, edible, medicinal);
plantPack.add(newWeed);
}
}
System.out.println("File has beeen uploaded.");
System.out.println();
input.close();
}
System.out.println(); }
******************fllower.java********************
public class fllower extends plant {
private String thorns;
private String smell;
private String id;
private String name;
public fllower() {
}
public fllower(String ID,String name,String color,String thorns,
String smell) {
super(ID, name, color);
this.thorns = thorns;
this.smell = smell;
id = ID;
this.name = name;
} public void setThorns(String thorns) {
this.thorns = thorns;
}
public void setSmell(String smell) {
this.smell = smell;
}
public String getThorns() {
return thorns;
}
public String getSmell() {
return smell;
}
public String toString() {
return id + " " + name + " " + getColor() + " " + thorns + " " +
smell;
}
}
********************fuungus.java**********************
public class fuungus extends plant {
private String poisonous;
private String id;
private String name;
public fuungus() {
}
/*
*
* @parram ID
* @param name
* @param color
* @param poisonous
*/
public fuungus(String ID, String name, String color, String poisonous) {
super(ID, name,color);
this.poisonous = poisonous;
id = ID;
this.name = name;
}
/**
*
* @params poisonous
*/
public void setPoisonous(String poisonous) {
this.poisonous = poisonous;
}
public String getPoisonous() {
return poisonous;
}
public String toString() {
return id + " " + name + " " + getColor() + " " + poisonous;
}
}
*******************************herrb.java***********************
public class herrb extends plant{
private String flavor;
private String medicinal;
private String seasonal;
herb(){
}
herrb(String ID, String name, String color, String f, String m, String s){
super(ID, name, color);
this.flavor =f;
this.medicinal =m;
this.seasonal=s;
}
public void setFlavor(String flavor) {
this.flavor = flavor;
}
public String getFlavor() {
return flavor;
}
public void setMedicinal(String medicinal) {
this.medicinal = medicinal;
}
public String getMedicinal() {
return medicinal;
}
public void setSeasonal(String seasonal) {
this.seasonal = seasonal;
}
public String getSeasonal() {
return seasonal;
}
}
*****************pllant.java*******************
public class pllant {
private String ID;
private String name;
private String color;
public pllant() {
}
public pllant(String id, String n,String c) {
ID = id;
name = n;
setColor(c);
}
public void setId(String id) {
ID = id;
}
public String getId() {
return ID;
}
public void setName(String n) {
name = n;
}
public String getName() {
return name;
}
public void setColor(String color) {
this.color = color;
}
public String getColor() {
return color;
}
}
*******************weedd.java**********************
public class weedd extends plant
{
private
private
private
private
private String
String
String
String
String poisonous;
edible;
medicinal;
id;
name; public weedd()
{
} public weed(String ID, String name, String color, String poisonous, String
edible, String medicinal)
{
super(ID,name,color);
this.poisonous = poisonous;
this.edible = edible;
this.medicinal = medicinal;
id = ID;
this.name = name;
} public void setPoisonous(String poisonous)
{
this.poisonous = poisonous;
}
public void setEdible(String edible) { this.edible = edible; } public void setmedicinal(String medicinal)
{
this.medicinal = medicinal;
} public String getPoisonous()
{
return poisonous;
}
public String getEdible() {
} return edible;
public String getMedicinal() {
} return medicinal; public String toString()
{
return id + " " + name + " " +
edible + " " + medicinal;
}
} getColor() + " " + poisonous + " " +
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.