Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Program -- need help -- search and filter by name Flower pack should hold plants that can be flowers, fungus or weed. - Create

JAVA Program -- need help -- search and filter by name

Flower pack should hold plants that 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)

- Be able to add, remove, search and filter these plants by name.

- Store your plants using an ArrayList.

package flowerpack;

import java.util.ArrayList; import java.util.Scanner;

public class FlowerPack {

public static void main(String[] args) { try{ ArrayList flowerPack= new ArrayList <>(); Scanner input=new Scanner(System.in); int choice; String name, color, ID; boolean poisonous,edible,medicinal,smell,thorns; while(true) { System.out.println("1. Add plants"); System.out.println("2. Remove plants"); System.out.println("3. Search plants"); System.out.println("4. Filter plants"); System.out.println("5. Quit"); System.out.println("Enter your choice: "); choice=input.nextInt(); switch(choice) { case 1: input.nextLine(); System.out.println("You want to add Flower, Fungus or Weed? "); String type=input.nextLine(); if(type.equalsIgnoreCase("Flower")) { System.out.println("Enter name : "); name=input.nextLine(); System.out.println("Enter ID : "); ID=input.nextLine(); System.out.println("Enter color : "); color=input.nextLine(); System.out.println("Has thorns? "); thorns=input.hasNextBoolean(); input.nextLine(); System.out.println("Has smell? "); smell=input.hasNextBoolean(); input.nextLine(); flowerPack.add(new Flower(name,ID,color,thorns,smell)); } else if(type.equalsIgnoreCase("Fungus")) { System.out.println("Enter name : "); name=input.nextLine(); System.out.println("Enter ID : "); ID=input.nextLine(); System.out.println("Enter color : "); color=input.nextLine(); System.out.println("Is it poisonous? "); poisonous=input.hasNextBoolean(); input.nextLine(); flowerPack.add(new Fungus(name,ID,color,poisonous)); } else if(type.equalsIgnoreCase("Weed")) { System.out.println("Enter name : "); name=input.nextLine(); System.out.println("Enter ID : "); ID=input.nextLine(); System.out.println("Enter color : "); color=input.nextLine(); System.out.println("Is it Poisonous? "); poisonous=input.hasNextBoolean(); input.nextLine(); System.out.println("Is it Edible? "); edible=input.hasNextBoolean(); input.nextLine(); System.out.println("Is it Medicinal? "); medicinal=input.hasNextBoolean(); input.nextLine(); flowerPack.add(new Weed(name,ID,color,poisonous,edible,medicinal)); } break; case 2:input.nextLine(); System.out.println("Enter the name of the plant you want to remove : "); name=input.nextLine(); int flag=0; for(Plant plant:flowerPack) { if(plant.getName().equalsIgnoreCase(name)) { System.out.println("plant removed sucessfully") ; flag=1; break; } } if(flag==0) { System.out.println("plant not found") ; } break; case 3: input.nextLine(); System.out.println("Enter the name of the plant you want to search : "); name=input.nextLine(); int f=1; for(Plant plant:flowerPack) { if(plant.getName().equalsIgnoreCase(name)) { System.out.println("plant found sucessfully") ; flag=1; break; } } if(f==0) { System.out.println("plant not found") ; } break; case 4:break; case 5: System.exit(0); } } }catch(Exception e) { System.out.println(e); } } }

**************************

package flowerpack;

public class Plant { String name; String ID; Plant(String name,String ID) { this.name=name; this.ID=ID; } public String getName() { return name; } } class Flower extends Plant { String color; boolean thorns; boolean smell; public Flower(String name, String ID,String color,boolean thorns,boolean smell) { super(name, ID); this.color=color; this.thorns=thorns; this.smell=smell; } }

class Fungus extends Plant { String Color; boolean Poisonous; public Fungus(String name, String ID,String Color,boolean Poisonous) { super(name, ID); this.Color=Color; this.Poisonous=Poisonous; } }

class Weed extends Plant { String Color; boolean Poisonous; boolean Edible; boolean Medicinal; public Weed(String name, String ID,String Color,boolean Poisonous,boolean Edible, boolean Medicinal) { super(name, ID); this.Color=Color; this.Poisonous=Poisonous; this.Edible=Edible; this.Medicinal=Medicinal; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Basics Computer EngineeringInformation Warehouse Basics From Science

Authors: Odiljon Jakbarov ,Anvarkhan Majidov

1st Edition

620675183X, 978-6206751830

More Books

Students also viewed these Databases questions

Question

Explain Intermediate term financing in detail.

Answered: 1 week ago

Question

Types of cultural maps ?

Answered: 1 week ago

Question

Discuss the various types of leasing.

Answered: 1 week ago

Question

Define the term "Leasing"

Answered: 1 week ago