Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA After a huge success selling our flower packs to local shops we’ve acquired

ID: 3812365 • Letter: J

Question

JAVA

After a huge success selling our flower packs to local shops we’ve acquired enough money to replace all of our items and had plenty left over to leave Alexander and Elizabeth so they can hire a doctor to travel in and hopefully help Elizabeth recover. Things have been happening very quickly and we haven’t had much time to process what we’ve accomplished, so here’s a little recap of everything we’ve done since we started our journey.

Created a basic flower pack that can add, display and remove (using arrays)        – Assignment 1

Searched through the flower pack.                                                                                          – Assignment 1

Sorted the flower pack.                                                                                                                                – Assignment 1

Created a flower object to hold attributes of each flower.                                            – Assignment 2

Upgraded our flower pack (to now use an ArrayList)                                                       – Assignment 3

Filter the flower pack based on a partial search.                                                                                 – Assignment 3

Upgraded our flower pack (to now hold multiple different types of plants)          – Midterm

Saved and Loaded information to/from a file.                                                                     – Assignment 4

*This list does not include information or tasks specific to labs

Several days after we’ve left and continued the journey we began so long ago we come to another small town that looks like a great place to stay for the night. The first thing we do, and have always done, is look for an inn. After we find what looks like the best inn we’ve ever seen and reserve our room we head to the tavern to socialize and hopefully find someone who catches our eye. After sitting at the bar for a bit a nice older lady comes up to us and starts a simple conversation. During this, we discover she is the owner of the small flower shop on the corner (what are the chances!). We politely tell her about the system we’ve developed, but sadly she isn’t interested. The bartender overhears our conversation and when the lady leaves he comes over to ask us more about our system.

The next morning we wake up in our room and look to our night stand where there is a candle that apparently burned the entire night and is hanging on to the last bit of light it can produce. Below the candle we notice a contract with what could only be construed as our signature on the bottom. What did we agree to last night??

After reading the contract it appears we’ve agreed to create a flower pack for the bartender to use. The bartender hopes to take our system, make copies and sell as many as he can to rebuild the home he lost in a fire a long time ago. There is a drawing on the first page with what appears to be some sort of interface, but it can’t be interpreted in any meaningful way.

Here’s our task for the week, since violating a contract results in death, we have no option but to finish.

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

Explanation / Answer

Explanation:

Add: adds a planet to list

Display: displays all list

Filter: filters flowers, or weeds and so on

Remove: removes one plant from list

Sort: sorts the list in the order flower, weed, fungus,herb

UserInterface.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Flower Pack</title>
</head>
<body>
<form action="/flowerPack" method="post">
<label for="add">What you want to add?</label><br>
<input type="hidden" name="op" value="add"/>
<input type="checkbox" name="ptype" value="flower">Flower</input>
<input type="checkbox" name="ptype" value="weeds">Weeds</input>
<input type="checkbox" name="ptype" value="fungus">Fungus</input>
<input type="checkbox" name="ptype" value="herb">Herbs</input><br>
Add the informations:<br>
Name=<input type="text">name</input><br>
color=<input type="text">color</input><br>
thorns=<input type="text">thorns</input><br>
smell=<input type="text">smell</input><br>
poisonous=<input type="text">poisonous</input><br>
edible=<input type="text">color</input><br>
medicinal=<input type="text">medicinal</input><br>
flaovor=<input type="text">flavor</input><br>
seasonal=<input type="text">seasonal</input><br>
<button type="submit" value="submit">submit</button><br>
<form>


<form action="/flowerPack" method="post">
<input type="hidden" name="op" value="display"/>
<label for="display">Display FlowerPack</label><br><br>
<button type="submit" value="submit">submit</button><br>
<form>

<form action="/flowerPack" method="post">
<input type="hidden" name="op" value="filter"/>
<label for="filter">Filter FlowerPack</label><br>
What to Filter?<input type="text" name= "filterby"></input> <br>
<button type="submit" value="submit">submit</button><br>
<form>

<form action="/flowerPack" method="post">
<input type="hidden" name="op" value="remove"/>
<label for="remove">Remove From FlowerPack</label><br>
Which flower/weeds/fungus/herbs you want to remove?<br>
Name:<input type="text" name="name"></input><br>
<button type="submit" value="submit">submit</button><br>
<form>

<form action="/flowerPack" method="post">
<input type="hidden" name="op" value="sort"/>
<label for="remove">Sort FlowerPack</label><br>
<button type="submit" value="submit">submit</button><br>
<form>


</body>
</html>

Assignment5.java renamed as FLowerPack.java

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);

                                   

                        }

              

                       

            }

}
Plant.java,Flower.java,Weed.java,Fungus.java,Herb.java


public class Plant {
   private static int count=0;
   private int id;
   private String name;
   private String color;
   private String type;
   public String getType() {
       return type;
   }
   public void setType(String type) {
       this.type = type;
   }
   public Plant(String name,String color,String type){
       this.name=name;
       this.color=color;
       this.type=type;
       this.id=count++;
   }
   public int getId() {
       return id;
   }
   public void setId(int id) {
       this.id = id;
   }
   public String getName() {
       return name;
   }
   public void setName(String name) {
       this.name = name;
   }
   public String getColor() {
       return color;
   }
   public void setColor(String color) {
       this.color = color;
   }
}
class Flower extends Plant{
   private String thorns;
   private String smell;
   public String getThorns() {
       return thorns;
   }
   public void setThorns(String thorns) {
       this.thorns = thorns;
   }
   public String getSmell() {
       return smell;
   }
   public void setSmell(String smell) {
       this.smell = smell;
   }
   public Flower(String name,String color,String type,String thorns,String smell){
       super(name,color,type);
       this.thorns=thorns;
       this.smell=smell;
   }
}
class Weeds extends Plant{
   private String poisonous;
   private String edible;
   private String medicinal;
   public Weeds(String name,String color,String type,String poisonous,String edible,String medicinal){
       super(name,color,type);
       this.poisonous=poisonous;
       this.edible=edible;
       this.medicinal=medicinal;
   }
   public String getPoisonous() {
       return poisonous;
   }
   public void setPoisonous(String poisonous) {
       this.poisonous = poisonous;
   }
   public String getEdible() {
       return edible;
   }
   public void setEdible(String edible) {
       this.edible = edible;
   }
   public String getMedicinal() {
       return medicinal;
   }
   public void setMedicinal(String medicinal) {
       this.medicinal = medicinal;
   }
}

class Fungus extends Plant{
   private String poisonous;
   public String getPoisonous() {
       return poisonous;
   }
   public void setPoisonous(String poisonous) {
       this.poisonous = poisonous;
   }
   public Fungus(String name,String color,String type,String poisonous){
       super(name,color,type);
       this.poisonous=poisonous;
      
   }
}
class Herb extends Plant{
   private String flavor;
   private String medicinal;
   private String seasonal;
   public Herb(String name,String color,String type,String flavor, String medicinal, String seasonal){
       super(name,color,type);
       this.flavor=flavor;
       this.medicinal=medicinal;
       this.seasonal=seasonal;
       }
}

result.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

<%@ page import="java.util.ArrayList" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Display Result</title>

</head>

<body>

<%

//creating arraylist object of type category class

ArrayList<Plant> list =(ArrayList<Plant>)request.getAttribute("displayres");

for(int i = 0; i < list.size(); i++) {

Plant p = list.get(i);

out.print( p.getName()+" ");

out.print(p.getColor()+" ");

out.print(p.getType()+" " );

out.println();

}

%>

</body>

</html>