in Java FX Create an interface for the user to display, add, remove, sort, and f
ID: 3822756 • Letter: I
Question
in Java FX Create an interface for the user to display, add, remove, sort, and filter our flower pack.You should use all of the following at least once in your interface
Stage
Button
Label
TextField
CheckBox
Layout (you can use any layout found in the lecture or book)
Menu
MenuItem
Load and Save file is not required for this assignment
The flower pack should hold flowers, weeds, fungus, and now herbs. All should be a subclass of a plant class.
Each subclass shares certain qualities (ID, Name, and Color)
Flower traits include (Thorns and Smell)
Fungus traits include (Poisonous)
Weed traits include (Poisonous, Edible and Medicinal)
Herb traits include (Flavor, Medicinal, Seasonal)
Submit 6 files: Assignment5.java, flower.java, fungus.java, plant.java, herb.java and weed.java
-------------------------------------------------------------------------------------------------------------------------------
Below are my base codes I have been using
base code
import java.util.*;
public class midterm{
static LinkedList <Plant>plants;
public static Plant SearchPlant(int id)
{
for(Plant p:plants){
if (p.GetID() == id){
return p;
}
}
return null;
}
public static void main(String[]args){
plants=new LinkedList<Plant>();
boolean continueChoice=false;
Scanner scanner=new Scanner(System.in);
int choice=0;
do{
try{
System.out.println("Choose Plant:1. Flower 2.Fungus 3.Weed Enter Option:");
int plantCh=scanner.nextInt();
if ( plantCh < 1 || plantCh > 3 )
{
System.out.println("Wrong Choice Entered! Try Again");
continue;
}
System.out.println("Choose Operation:1.Add 2.Remove 3. Search 4. Show Enter Option:");
int op=scanner.nextInt();
switch(op)
{
case 1:
int id;
System.out.println("Enter ID:");
id=scanner.nextInt();
scanner.nextLine();
System.out.println("Enter Name:");
String name=scanner.nextLine();
System.out.println("Enter Color:");
String color=scanner.nextLine();
Plant p=null;
if (plantCh==1)
{
p=new Flower();
System.out.println("Enter thorns:");
String thorns=scanner.nextLine();
((Flower)p).SetThorns(thorns);
System.out.println("Enter Smell:");
String smell=scanner.nextLine();
((Flower)p).SetSmell(smell);
}
else if(plantCh==2)
{p=new Fungus();
System.out.println("Enter poisnous:");
boolean poison=scanner.nextBoolean();
((Fungus)p).SetPoisonous(poison);
}
else{
p=new Weed();
System.out.println("Enter Medicinal;");
String medicinal=scanner.nextLine();
((Weed)p).SetMedicinal(medicinal);
}
p.SetID(id);
p.SetName(name);
p.SetColor(color);
plants.add(p);
break;
case 2:
{
System.out.println("Enter Plant ID:");
id=scanner.nextInt();
Plant plantOBJ=SearchPlant(id);
if (plantOBJ !=null){
plants.remove(plantOBJ);
System.out.println("Flower ID: "+id+"removed");
}
else
{
System.out.println("Flower ID: "+id+"not found");
}
}
break;
case 3:
System.out.println("Enter Plant ID:");
id=scanner.nextInt();
if (SearchPlant(id) != null){
System.out.println("Flower ID: "+id+"not found");
}
break;
case 4:
{
System.out.println("Enter Plant ID:");
id=scanner.nextInt();
Plant plantObj=SearchPlant(id);
if (SearchPlant(id) !=null){
plantObj.Show();
}
else
{
System.out.println("Flower ID: "+id+"not found");
}
}
break;
default:
System.out.println("Wrong choice");
}
System.out.print("Do you want to continue (true or false): ");
continueChoice=scanner.nextBoolean();
}
catch(Exception e)
{
e.printStackTrace();
}
}
while(continueChoice);
}
}
==========================
Plant code
import java.util.*;
class Plant{
String name;
int id;
String color;
Plant(){
}
void SetID(int id)
{
this.id=id;
}
int GetID()
{
return this.id;
}
void SetName(String name)
{
this.name=name;
}
void SetColor(String color)
{
this.color=name;
}
public void Show(){
System.out.println("ID:"+id+" Name:"+name+" Color:"+color);
}
}
=====================
Fungus code
class Fungus extends Plant{
boolean poisonous;
void SetPoisonous(boolean poisonous)
{
this.poisonous=poisonous;
}
public void Show(){
super.Show();
System.out.println("poisonous:"+poisonous);
}
}
===============================
Weed code
class Weed extends Plant{
boolean poisonous;
String edible;
String medicinal;
void SetPoisonous(boolean poisonous)
{
this.poisonous=poisonous;
}
void SetEdible(String edible)
{
this.edible=edible;
}
void SetMedicinal(String medicinal)
{
this.medicinal=medicinal;
}
public void Show(){
super.Show();
System.out.println("poisnous:"+poisonous+"edible:"+edible+"medicinal:"+medicinal);
}
}
========================================
Flower code
class Flower extends Plant{
String thorns;
String smell;
void SetSmell(String smell)
{
this.smell=smell;
}
void SetThorns(String thorn)
{
this.thorns=thorn;
}
public void Show(){
super.Show();
System.out.println("Thorns:"+thorns+"Smell:"+smell);
}
}
Explanation / Answer
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class FlowerPack
*/
@WebServlet("/FlowerPack")
public class FlowerPack extends HttpServlet {
private static final long serialVersionUID = 1L;
static ArrayList<Plant> lst=new ArrayList<Plant>();
/**
* @see HttpServlet#HttpServlet()
*/
public FlowerPack() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String operation = (String)request.getParameter("op");
if(operation.equals("add")){
Plant p=null;
String type=request.getParameter("ptype");
String name=request.getParameter("name");
String color=request.getParameter("color");
String poisonous=request.getParameter("poisonous");
String edible=request.getParameter("edible");
String medicinal=request.getParameter("medicinal");
String thorns=request.getParameter("thorns");
String smell=request.getParameter("smell");
String flavor=request.getParameter("flavor");
String seasonal=request.getParameter("seasonal");
if(type.equals("flower")){
p=new Flower(name,color,type,thorns,smell);
}else if(type.equals("fungus")){
p=new Fungus(name,color,type,poisonous);
}else if(type.equals("weeds")){
p=new Weeds(name,color,type,poisonous,edible,medicinal);
}else if(type.equals("herb")){
p=new Herb(name,color,type,flavor,medicinal,seasonal);
}
lst.add(p);
}else if(operation.equals("remove")){
String name=request.getParameter("name");
for(Plant p:lst){
if(p.getName().equals(name))
lst.remove(p);
}
}else if(operation.equals("display")){
request.setAttribute("displayres", lst);
getServletConfig().getServletContext().getRequestDispatcher("/result.jsp").forward(request,response);
}
else if(operation.equals("filter")){
String filterAll=request.getParameter("filterBy");
ArrayList<Plant> result=new ArrayList<Plant>();
if(filterAll.equals("flower")){
for(Plant p:lst){
if(p.getType().equals("flower"))
result.add(p);
}
}else if(filterAll.equals("weeds")){
for(Plant p:lst){
if(p.getType().equals("weeds"))
result.add(p);
}
}
else if(filterAll.equals("fungus")){
for(Plant p:lst){
if(p.getType().equals("fungus"))
result.add(p);
}
}
else if(filterAll.equals("herb")){
for(Plant p:lst){
if(p.getType().equals("herb"))
result.add(p);
}
}
request.setAttribute("displayres", result);
getServletConfig().getServletContext().getRequestDispatcher("/result.jsp").forward(request,response);
}else if(operation.equals("sort")){
ArrayList<Plant> flowerlist=new ArrayList<Plant>();
ArrayList<Plant> weedlist=new ArrayList<Plant>();
ArrayList<Plant> funguslist=new ArrayList<Plant>();
ArrayList<Plant> herblist=new ArrayList<Plant>();
ArrayList<Plant> result=new ArrayList<Plant>();
for(Plant p:lst){
if(p.getType().equals("flower")){
flowerlist.add(p);
}else if(p.getType().equals("weed")){
weedlist.add(p);
}
else if(p.getType().equals("fungus")){
funguslist.add(p);
}
else if(p.getType().equals("herb")){
herblist.add(p);
}
}
result.addAll(flowerlist);
result.addAll(weedlist);
result.addAll(funguslist);
result.addAll(herblist);
request.setAttribute("displayres", result);
getServletConfig().getServletContext().getRequestDispatcher("/result.jsp").forward(request,response);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.