I am to create this package as a LinkedList. My main hang up is where to start i
ID: 3859420 • Letter: I
Question
I am to create this package as a LinkedList. My main hang up is where to start in creating the Node class. Ignore the GUI codes for now in the Final.java class. I would like to see if there is an easy (less tedious) way to simply create the Node class and reverse back to customize the Final class. I must submit all classes as posted below. The GUI is not required and will be edited out.
Node.java
public class Node
{
//I have no idea where to start.
}
Final.java
public class Final extends JFrame
{
JButton bAdd, bRemove, bDisplay, bSort, bFilter, bAnalyze;
JRadioButton rbFlower, rbFungus, rbWeed, rbHerb;
JCheckBox chThorns, chSmell, chPoisonous, chPoisonous2, chEdible, chMedicinal, chMedicinal2, chFlavor, chSeasonal;
JTextField tName, tName2, tName3, tName4, tID, tID2, tID3, tID4, tColor, tColor2, tColor3, tColor4, delName, delID, filName;
JLabel labHome, labPlant, labName, labNam2, labNam3, labNam4, labID, labID2, labID3, labID4, labColor, labColor2;
JLabel labColor3, labColor4, labCheck1, labCheck2, labCheck3, labCheck4, labRemN, labRemI;
JTextArea plantDisplay, plantDisplay2, plantDisplay3;
JMenuBar menu;
JMenu filemenu;
JMenuItem exit;
String hOME = new String ("<html><center><bold><font size = 15>WELCOME TO THE PLANT INTERFACE<br/></font></font size = 13>CHOOSE AN ACTION</center></font></html>");
String baDD = new String ("<html><center><bold><font size = 5>ADD<br/></font><font size = 2>A PLANT</center></font></html>");
String brEM = new String ("<html><center><bold><font size = 5>REMOVE<br/></font><font size = 2>A PLANT</center></font></html>");
String bdIS = new String ("<html><center><bold><font size = 5>DISPLAY<br/></font><font size = 2>ALL PLANTS</center></font></html>");
String bsOR = new String ("<html><center><bold><font size = 5>SORT<br/></font><font size = 2>YOUR PLANTS</center></font></html>");
String bfIL = new String ("<html><center><bold><font size = 5>FILTER<br/></font><font size = 2>Search for plant</center></font></html>");
String baNA = new String ("<html><center><bold><font size = 5>ANALYZE<br/></font><font size = 2>ALL PLANTS</center></font></html>");
JPanel mPanelOne = new JPanel();
JPanel mPanelTwo = new JPanel();
JPanel mPanelThr = new JPanel();
JPanel mPanelFou = new JPanel();
JPanel mPanelFiv = new JPanel();
JPanel mPanelSix = new JPanel();
JPanel mPanelSev = new JPanel();
JPanel mPanelEig = new JPanel();
JPanel mPanelNin = new JPanel();
JPanel mPanelTen = new JPanel();
JPanel mPanelEle = new JPanel();
JPanel mPanelTwe = new JPanel();
ArrayList<Plant> flowerPack = new ArrayList();
public static void main(String[] args)
{
new Final();
}
public Final()
{
try
{
//Panel One and Panel Two for Main Interface (HOME)
this.setTitle("Assignment 6 Plant Interface");
setLayout(new FlowLayout());
labHome = new JLabel(hOME);
mPanelOne.add(labHome);
bAdd = new JButton(baDD);
bAdd.addActionListener(new BtnAddHndlr());
bRemove = new JButton(brEM);
bRemove.addActionListener(new BtnRemHndlr());
bDisplay = new JButton(bdIS);
bDisplay.addActionListener(new BtnDisHndlr());
bSort = new JButton(bsOR);
bSort.addActionListener(new BtnSorHndlr());
bFilter = new JButton(bfIL);
bFilter.addActionListener(new BtnFilHndlr());
bAnalyze = new JButton(baNA);
bAnalyze.addActionListener(new BtnAnaHndlr());
mPanelTwo.add(bAdd);
mPanelTwo.add(bRemove);
mPanelTwo.add(bDisplay);
mPanelTwo.add(bSort);
mPanelTwo.add(bFilter);
mPanelTwo.add(bAnalyze);
menu = new JMenuBar();
filemenu = new JMenu("FILE");
exit = new JMenuItem("EXIT");
filemenu.add(exit);
menu.add(filemenu);
setJMenuBar(menu);
exit.addActionListener(new ExitHandler());
add(mPanelOne);
add(mPanelTwo);
setSize(750,300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
//Panel Three for Add a Plant starting window
labPlant = new JLabel("<html>ADD A PLANT<br/></font>Choose a plant type</font><br/></font>Click CANCEL to exit.</font></html>");
mPanelThr.add(labPlant);
rbFlower = new JRadioButton("Flower");
rbFungus = new JRadioButton("Fungus");
rbWeed = new JRadioButton("Weed");
rbHerb = new JRadioButton("Herb");
ButtonGroup plantBG = new ButtonGroup();
plantBG.add(rbFlower);
plantBG.add(rbFungus);
plantBG.add(rbWeed);
plantBG.add(rbHerb);
mPanelThr.add(rbFlower);
mPanelThr.add(rbFungus);
mPanelThr.add(rbWeed);
mPanelThr.add(rbHerb);
rbFlower.addActionListener(new AddHandler());
rbFungus.addActionListener(new AddHandler());
rbWeed.addActionListener(new AddHandler());
rbHerb.addActionListener(new AddHandler());
//Panel Four to Add a Flower window
labName = new JLabel("Name: ");
labID = new JLabel("ID: ");
labColor = new JLabel("Color: ");
tName = new JTextField("",20);
tID = new JTextField("",20);
tColor = new JTextField("",20);
labCheck1 = new JLabel("Check traits that are present: ");
chThorns = new JCheckBox("Thorns");
chSmell = new JCheckBox("Smell");
mPanelFou.add(labName);
mPanelFou.add(tName);
mPanelFou.add(labID);
mPanelFou.add(tID);
mPanelFou.add(labColor);
mPanelFou.add(tColor);
mPanelFou.add(labCheck1);
mPanelFou.add(chThorns);
mPanelFou.add(chSmell);
//Panel Five to Add a Fungus window
labNam2 = new JLabel("Name: ");
labID2 = new JLabel("ID: ");
labColor2 = new JLabel("Color: ");
tName2 = new JTextField("",20);
tID2 = new JTextField("",20);
tColor2 = new JTextField("",20);
labCheck1 = new JLabel("Check traits that are present: ");
chPoisonous = new JCheckBox("Poisonous");
mPanelFiv.add(labNam2);
mPanelFiv.add(tName2);
mPanelFiv.add(labID2);
mPanelFiv.add(tID2);
mPanelFiv.add(labColor2);
mPanelFiv.add(tColor2);
mPanelFiv.add(labCheck1);
mPanelFiv.add(chPoisonous);
//Panel Six to Add a Weed window
labNam3 = new JLabel("Name: ");
labID3 = new JLabel("ID: ");
labColor3 = new JLabel("Color: ");
tName3 = new JTextField("",20);
tID3 = new JTextField("",20);
tColor3 = new JTextField("",20);
labCheck1 = new JLabel("Check traits that are present: ");
chPoisonous2 = new JCheckBox("Poisonous");
chEdible = new JCheckBox("Edible");
chMedicinal = new JCheckBox("Medicinal");
mPanelSix.add(labNam3);
mPanelSix.add(tName3);
mPanelSix.add(labID3);
mPanelSix.add(tID3);
mPanelSix.add(labColor3);
mPanelSix.add(tColor3);
mPanelSix.add(labCheck1);
mPanelSix.add(chPoisonous2);
mPanelSix.add(chEdible);
mPanelSix.add(chMedicinal);
//Panel Seven to Add an Herb window
labNam4 = new JLabel("Name: ");
labID4 = new JLabel("ID: ");
labColor4 = new JLabel("Color: ");
tName4 = new JTextField("",20);
tID4 = new JTextField("",20);
tColor4 = new JTextField("",20);
labCheck1 = new JLabel("Check traits that are present: ");
chFlavor = new JCheckBox("Flavor");
chMedicinal2 = new JCheckBox("Medicinal");
chSeasonal = new JCheckBox("Seasonal");
mPanelSev.add(labNam4);
mPanelSev.add(tName4);
mPanelSev.add(labID4);
mPanelSev.add(tID4);
mPanelSev.add(labColor4);
mPanelSev.add(tColor4);
mPanelSev.add(labCheck1);
mPanelSev.add(chFlavor);
mPanelSev.add(chMedicinal2);
mPanelSev.add(chSeasonal);
//Panel Eight to REMOVE a plant window
delName = new JTextField("",20);
delID = new JTextField("",20);
labCheck2 = new JLabel("Enter the NAME or ID of plant to remove: ");
labRemN = new JLabel("Name: ");
labRemI = new JLabel("ID: ");
mPanelEig.add(labCheck2);
mPanelEig.add(labRemN);
mPanelEig.add(delName);
mPanelEig.add(labRemI);
mPanelEig.add(delID);
//Panel Nine to Display all plants window
labCheck3 = new JLabel("Displaying all flowers contained in the pack...");
plantDisplay = new JTextArea (50, 50);
plantDisplay.setBackground(Color.WHITE);
plantDisplay.setFont(new Font("Arial", Font.PLAIN, 14));
plantDisplay.setEditable(false);
JScrollPane pDispScroll = new JScrollPane(plantDisplay);
pDispScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
mPanelNin.add(labCheck3);
mPanelNin.add(pDispScroll, BorderLayout.CENTER);
//Panel Ten to Filter Search for a plant window
labCheck4 = new JLabel("Enter what you know of the plant name to search: ");
filName = new JTextField("",20);
mPanelTen.add(labCheck4);
mPanelTen.add(filName);
//Panel Eleven to Display Search results window
plantDisplay2 = new JTextArea (50, 50);
plantDisplay2.setBackground(Color.WHITE);
plantDisplay2.setFont(new Font("Arial", Font.PLAIN, 14));
plantDisplay2.setEditable(false);
JScrollPane pDispScroll2 = new JScrollPane(plantDisplay2);
pDispScroll2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
mPanelEle.add(pDispScroll2, BorderLayout.CENTER);
//Panel Twelve to Display Analyze results window
plantDisplay3 = new JTextArea (70, 70);
plantDisplay3.setBackground(Color.WHITE);
plantDisplay3.setFont(new Font("Arial", Font.PLAIN, 14));
plantDisplay3.setEditable(false);
JScrollPane pDispScroll3 = new JScrollPane(plantDisplay3);
pDispScroll3.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
mPanelTwe.add(pDispScroll3, BorderLayout.CENTER);
}
catch (Exception e)
{
System.out.println(e);
}
}
class BtnAddHndlr implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals(baDD))
{
JOptionPane.showOptionDialog(null, mPanelThr, "Add a plant", JOptionPane.CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,null,null,null);
}
}
}
class BtnRemHndlr implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals(brEM))
{
removePlant();
}
}
}
class BtnDisHndlr implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals(bdIS))
{
displayPlant();
}
}
}
class BtnSorHndlr implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals(bsOR))
{
sortPlant();
}
}
}
class BtnFilHndlr implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals(bfIL))
{
filterPlant();
}
}
}
class BtnAnaHndlr implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals(baNA))
{
analyzePlant();
}
}
}
class ExitHandler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
class AddHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand().equals("Flower"))
{
addFlower();
}
else if (e.getActionCommand().equals("Fungus"))
{
addFungus();
}
else if (e.getActionCommand().equals("Weed"))
{
addWeed();
}
else if (e.getActionCommand().equals("Herb"))
{
addHerb();
}
}
}
private void addPlant()
{
JOptionPane.showMessageDialog(null, "Double Hooray!");
}
private void addFlower()
{
JOptionPane.showMessageDialog(null, mPanelFou, "Add a flower", JOptionPane.PLAIN_MESSAGE);
String name, Color, ID;
boolean thorns, smell;
name = tName.getText();
ID = tID.getText();
Color = tColor.getText();
thorns = chThorns.isSelected();
smell = chSmell.isSelected();
flowerPack.add(new Flower(name, ID, Color, thorns, smell));
JOptionPane.showMessageDialog(null, "Flower " + name + " " + ID + " added to pack.");
}
private void addFungus()
{
JOptionPane.showMessageDialog(null, mPanelFiv, "Add a fungus", JOptionPane.PLAIN_MESSAGE);
String name, Color, ID;
boolean poisonous;
name = tName2.getText();
ID = tID2.getText();
Color = tColor2.getText();
poisonous = chPoisonous.isSelected();
flowerPack.add(new Fungus(name, ID, Color, poisonous));
JOptionPane.showMessageDialog(null, "Fungus " + name + " " + ID + " added to pack.");
}
private void addWeed()
{
JOptionPane.showMessageDialog(null, mPanelSix, "Add a weed", JOptionPane.PLAIN_MESSAGE);
String name, Color, ID;
boolean poisonous, edible, medicinal;
name = tName3.getText();
ID = tID3.getText();
Color = tColor3.getText();
poisonous = chPoisonous2.isSelected();
edible = chEdible.isSelected();
medicinal = chMedicinal.isSelected();
flowerPack.add(new Weed(name, ID, Color, poisonous, edible, medicinal));
JOptionPane.showMessageDialog(null, "Weed " + name + " " + ID + " added to pack.");
}
private void addHerb()
{
JOptionPane.showMessageDialog(null, mPanelSev, "Add an herb", JOptionPane.PLAIN_MESSAGE);
String name, Color, ID;
boolean flavor, medicinal, seasonal;
name = tName4.getText();
ID = tID4.getText();
Color = tColor4.getText();
flavor = chFlavor.isSelected();
medicinal = chMedicinal2.isSelected();
seasonal = chSeasonal.isSelected();
flowerPack.add(new Herb(name, ID, Color, flavor, medicinal, seasonal));
JOptionPane.showMessageDialog(null, "Herb " + name + " " + ID + " added to pack.");
}
private void removePlant()
{
if (flowerPack.size() > 0)
{
JOptionPane.showOptionDialog(null, mPanelEig, "Remove a plant", JOptionPane.CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,null,null,null);
String name, ID;
name = delName.getText().trim();
ID = delID.getText().trim();
boolean found = false;
Iterator<Plant> itr = flowerPack.iterator();
if(name != null || ID != null)
{
while(itr.hasNext())
{
Plant flowers = itr.next();
if(flowers.getID().equals(ID) || flowers.getName().equals(name))
{
itr.remove();
found = true;
break;
}
}
if (found)
{
JOptionPane.showMessageDialog(null, "Plant successfully removed from pack.");
}
else
{
JOptionPane.showMessageDialog(null, "Plant not found. Please try again.");
}
}
}
else
{
JOptionPane.showMessageDialog(null, "There are no plants in your pack.");
}
}
private void displayPlant()
{
if (flowerPack.size() > 0)
{
plantDisplay.setText("");
for(int i=0; i<flowerPack.size(); i++)
{
Plant f = flowerPack.get(i);
plantDisplay.append(" " + (i + 1) + " - " + "Name: " + f.getName() + " Plant ID: " + f.getID() + " Color: "
+ f.getColor() + " Thorns: " + f.getThorns() + " Smell: " + f.getSmell() + " Poisonous: "
+ f.getPoisonous() + " Edible: " + f.getEdible() + " Medicinal: " + f.getMedicinal() + " Flavor: " + f.getFlavor() + " Seasonal: " +
f.getSeasonal());
}
JOptionPane.showMessageDialog(null, mPanelNin, "Display all plants", JOptionPane.PLAIN_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "There are no plants in your pack.");
}
}
private void sortPlant()
{
if (flowerPack.size() > 0)
{
Collections.sort(flowerPack, Plant.plantNameComparator);
for(Plant plort: flowerPack)
{
}
JOptionPane.showMessageDialog(null, "<html>Pack successfully sorted in alphabetical order by name.<br/><br/></font>Please click 'DISPLAY' from main interface.</font></html>");
}
else
{
JOptionPane.showMessageDialog(null, "There are no plants in your pack.");
}
}
private void filterPlant()
{
if (flowerPack.size() > 0)
{
JOptionPane.showOptionDialog(null, mPanelTen, "Filtered Search for a plant", JOptionPane.CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,null,null,null);
String name;
name = filName.getText().trim();
boolean found = false;
plantDisplay2.setText("");
for (int i=0; i < flowerPack.size(); i++)
{
Plant f = flowerPack.get(i);
if (f.getName().contains(name))
{
plantDisplay2.append(" Plant search of '" + name + "' produced the following result: Name: " + f.getName() + " PlantID: " + f.getID() +
" ...in position " + (i + 1));
found = true;
}
}
JOptionPane.showMessageDialog(null, mPanelEle, "Display results", JOptionPane.PLAIN_MESSAGE);
if (found == false)
{
JOptionPane.showMessageDialog(null, "Your search entry of '" + name + "' yielded no results. **Plant not present.** ");
}
}
else
{
JOptionPane.showMessageDialog(null, "There are no plants in your pack.");
}
}
private void analyzePlant()
{
//JOptionPane.showMessageDialog(null, "Double Hooray!");
int size = flowerPack.size();
String ar = new String ("ar");
String r = new String ("r");
String s = new String ("s");
if (size == 0)
{
JOptionPane.showMessageDialog(null, "There are no plants in your pack.");
}
else
{
for (int i=0; i < flowerPack.size(); i++)
{
Plant f = flowerPack.get(i);
int n1 = numInstances(f.getName(), ar);
int n2 = numInstances(f.getName(), r);
int n3 = numInstances(f.getName(), s);
plantDisplay3.append(" String " + ar + " " + r + " " + s + " " + f.getName() + " " + n1 + " " + n2 + " " + n3);
}
JOptionPane.showMessageDialog(null, mPanelTwe, "Display results", JOptionPane.PLAIN_MESSAGE);
}
}
public int numInstances(String str, String substr)
{
if(str.length() < substr.length())
{
return 0;
}
if (str.substring(0, substr.length()).equals(substr))
{
return 1 + numInstances(str.substring(substr.length()),substr);
}
return numInstances(str.substring(1), substr);
}
}
Plant.java
public class Plant
{
String name;
String ID;
String Color;
boolean thorns;
boolean smell;
boolean Poisonous;
boolean Edible;
boolean Medicinal;
boolean Flavor;
boolean Seasonal;
Plant(String name,String ID,String Color)
{
this.name=name;
this.ID=ID;
this.Color=Color;
}
public String getName()
{
return name;
}
public String getID()
{
return ID;
}
public String getColor()
{
return Color;
}
public boolean getThorns()
{
return thorns;
}
public boolean getSmell()
{
return smell;
}
public boolean getPoisonous()
{
return Poisonous;
}
public boolean getEdible()
{
return Edible;
}
public boolean getMedicinal()
{
return Medicinal;
}
public boolean getSeasonal()
{
return Seasonal;
}
public boolean getFlavor()
{
return Flavor;
}
public static Comparator<Plant> plantNameComparator = new Comparator<Plant>()
{
public int compare(Plant p1, Plant p2)
{
String PlantName1 = p1.getName().toUpperCase();
String PlantName2 = p2.getName().toUpperCase();
return PlantName1.compareTo(PlantName2);
}
};
//@Override
public String toString()
{
return "";
}
}
Flower.java
public 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, Color);
this.Color=Color;
this.thorns=thorns;
this.smell=smell;
}
public String getColor()
{
return Color;
}
public boolean getThorns()
{
return thorns;
}
public boolean getSmell()
{
return smell;
}
}
Fungus.java
public class Fungus extends Plant
{
String Color;
boolean Poisonous;
public Fungus(String name, String ID,String Color,boolean Poisonous)
{
super(name, ID, Color);
this.Color=Color;
this.Poisonous=Poisonous;
}
public String getColor()
{
return Color;
}
public boolean getPoisonous()
{
return Poisonous;
}
}
Weed.java
public 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, Color);
this.Color=Color;
this.Poisonous=Poisonous;
this.Edible=Edible;
this.Medicinal=Medicinal;
}
public String getColor()
{
return Color;
}
public boolean getPoisonous()
{
return Poisonous;
}
public boolean getEdible()
{
return Edible;
}
public boolean getMedicinal()
{
return Medicinal;
}
}
Herb.java
*/
public class Herb extends Plant
{
String Color;
boolean Flavor;
boolean Seasonal;
boolean Medicinal;
public Herb(String name, String ID, String Color, boolean Flavor, boolean Seasonal, boolean Medicinal)
{
super(name, ID, Color);
this.Color=Color;
this.Flavor=Flavor;
this.Seasonal=Seasonal;
this.Medicinal=Medicinal;
}
public String getColor()
{
return Color;
}
public boolean getFlavor()
{
return Flavor;
}
public boolean getSeasonal()
{
return Seasonal;
}
public boolean getMedicinal()
{
return Medicinal;
}
}
Explanation / Answer
Hi,
Please have a look on the below program which is editible by me
public class Node
{
public static void main(String[] args)
{
new Final();
}
public Final()
{
try
{
//Panel One and Panel Two for Main Interface (HOME)
this.setTitle("Assignment 6 Plant Interface");
setLayout(new FlowLayout());
labHome = new JLabel(hOME);
mPanelOne.add(labHome);
bAdd = new JButton(baDD);
bAdd.addActionListener(new BtnAddHndlr());
bRemove = new JButton(brEM);
bRemove.addActionListener(new BtnRemHndlr());
bDisplay = new JButton(bdIS);
bDisplay.addActionListener(new BtnDisHndlr());
bSort = new JButton(bsOR);
bSort.addActionListener(new BtnSorHndlr());
bFilter = new JButton(bfIL);
bFilter.addActionListener(new BtnFilHndlr());
bAnalyze = new JButton(baNA);
bAnalyze.addActionListener(new BtnAnaHndlr());
mPanelTwo.add(bAdd);
mPanelTwo.add(bRemove);
mPanelTwo.add(bDisplay);
mPanelTwo.add(bSort);
mPanelTwo.add(bFilter);
mPanelTwo.add(bAnalyze);
menu = new JMenuBar();
filemenu = new JMenu("FILE");
exit = new JMenuItem("EXIT");
filemenu.add(exit);
menu.add(filemenu);
setJMenuBar(menu);
exit.addActionListener(new ExitHandler());
add(mPanelOne);
add(mPanelTwo);
setSize(750,300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
//Panel Three for Add a Plant starting window
labPlant = new JLabel("<html>ADD A PLANT<br/></font>Choose a plant type</font><br/></font>Click CANCEL to exit.</font></html>");
mPanelThr.add(labPlant);
rbFlower = new JRadioButton("Flower");
rbFungus = new JRadioButton("Fungus");
rbWeed = new JRadioButton("Weed");
rbHerb = new JRadioButton("Herb");
ButtonGroup plantBG = new ButtonGroup();
plantBG.add(rbFlower);
plantBG.add(rbFungus);
plantBG.add(rbWeed);
plantBG.add(rbHerb);
mPanelThr.add(rbFlower);
mPanelThr.add(rbFungus);
mPanelThr.add(rbWeed);
mPanelThr.add(rbHerb);
rbFlower.addActionListener(new AddHandler());
rbFungus.addActionListener(new AddHandler());
rbWeed.addActionListener(new AddHandler());
rbHerb.addActionListener(new AddHandler());
//Panel Four to Add a Flower window
labName = new JLabel("Name: ");
labID = new JLabel("ID: ");
labColor = new JLabel("Color: ");
tName = new JTextField("",20);
tID = new JTextField("",20);
tColor = new JTextField("",20);
labCheck1 = new JLabel("Check traits that are present: ");
chThorns = new JCheckBox("Thorns");
chSmell = new JCheckBox("Smell");
mPanelFou.add(labName);
mPanelFou.add(tName);
mPanelFou.add(labID);
mPanelFou.add(tID);
mPanelFou.add(labColor);
mPanelFou.add(tColor);
mPanelFou.add(labCheck1);
mPanelFou.add(chThorns);
mPanelFou.add(chSmell);
//Panel Five to Add a Fungus window
labNam2 = new JLabel("Name: ");
labID2 = new JLabel("ID: ");
labColor2 = new JLabel("Color: ");
tName2 = new JTextField("",20);
tID2 = new JTextField("",20);
tColor2 = new JTextField("",20);
labCheck1 = new JLabel("Check traits that are present: ");
chPoisonous = new JCheckBox("Poisonous");
mPanelFiv.add(labNam2);
mPanelFiv.add(tName2);
mPanelFiv.add(labID2);
mPanelFiv.add(tID2);
mPanelFiv.add(labColor2);
mPanelFiv.add(tColor2);
mPanelFiv.add(labCheck1);
mPanelFiv.add(chPoisonous);
//Panel Six to Add a Weed window
labNam3 = new JLabel("Name: ");
labID3 = new JLabel("ID: ");
labColor3 = new JLabel("Color: ");
tName3 = new JTextField("",20);
tID3 = new JTextField("",20);
tColor3 = new JTextField("",20);
labCheck1 = new JLabel("Check traits that are present: ");
chPoisonous2 = new JCheckBox("Poisonous");
chEdible = new JCheckBox("Edible");
chMedicinal = new JCheckBox("Medicinal");
mPanelSix.add(labNam3);
mPanelSix.add(tName3);
mPanelSix.add(labID3);
mPanelSix.add(tID3);
mPanelSix.add(labColor3);
mPanelSix.add(tColor3);
mPanelSix.add(labCheck1);
mPanelSix.add(chPoisonous2);
mPanelSix.add(chEdible);
mPanelSix.add(chMedicinal);
//Panel Seven to Add an Herb window
labNam4 = new JLabel("Name: ");
labID4 = new JLabel("ID: ");
labColor4 = new JLabel("Color: ");
tName4 = new JTextField("",20);
tID4 = new JTextField("",20);
tColor4 = new JTextField("",20);
labCheck1 = new JLabel("Check traits that are present: ");
chFlavor = new JCheckBox("Flavor");
chMedicinal2 = new JCheckBox("Medicinal");
chSeasonal = new JCheckBox("Seasonal");
mPanelSev.add(labNam4);
mPanelSev.add(tName4);
mPanelSev.add(labID4);
mPanelSev.add(tID4);
mPanelSev.add(labColor4);
mPanelSev.add(tColor4);
mPanelSev.add(labCheck1);
mPanelSev.add(chFlavor);
mPanelSev.add(chMedicinal2);
mPanelSev.add(chSeasonal);
//Panel Eight to REMOVE a plant window
delName = new JTextField("",20);
delID = new JTextField("",20);
labCheck2 = new JLabel("Enter the NAME or ID of plant to remove: ");
labRemN = new JLabel("Name: ");
labRemI = new JLabel("ID: ");
mPanelEig.add(labCheck2);
mPanelEig.add(labRemN);
mPanelEig.add(delName);
mPanelEig.add(labRemI);
mPanelEig.add(delID);
//Panel Nine to Display all plants window
labCheck3 = new JLabel("Displaying all flowers contained in the pack...");
plantDisplay = new JTextArea (50, 50);
plantDisplay.setBackground(Color.WHITE);
plantDisplay.setFont(new Font("Arial", Font.PLAIN, 14));
plantDisplay.setEditable(false);
JScrollPane pDispScroll = new JScrollPane(plantDisplay);
pDispScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
mPanelNin.add(labCheck3);
mPanelNin.add(pDispScroll, BorderLayout.CENTER);
//Panel Ten to Filter Search for a plant window
labCheck4 = new JLabel("Enter what you know of the plant name to search: ");
filName = new JTextField("",20);
mPanelTen.add(labCheck4);
mPanelTen.add(filName);
//Panel Eleven to Display Search results window
plantDisplay2 = new JTextArea (50, 50);
plantDisplay2.setBackground(Color.WHITE);
plantDisplay2.setFont(new Font("Arial", Font.PLAIN, 14));
plantDisplay2.setEditable(false);
JScrollPane pDispScroll2 = new JScrollPane(plantDisplay2);
pDispScroll2.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
mPanelEle.add(pDispScroll2, BorderLayout.CENTER);
//Panel Twelve to Display Analyze results window
Displayofplant3 = new JTextArea (70, 70);
Displayofplant3.setBackground(Color.WHITE);
Displayofplant3.setFont(new Font("Arial", Font.PLAIN, 14));
Displayofplant3.setEditable(false);
JScrollPane pDispScroll3 = new JScrollPane(Displayofplant3);
pDispScroll3.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
mPanelTwe.add(pDispScroll3, BorderLayout.CENTER);
}
catch (Exception e)
{
System.out.println(e);
}
}
}
public class Final extends JFrame
{
JButton bAdd, bRemove, bDisplay, bSort, bFilter, bAnalyze;
JRadioButton rbFlower, rbFungus, rbWeed, rbHerb;
JCheckBox chThorns, chSmell, chPoisonous, chPoisonous2, chEdible, chMedicinal, chMedicinal2, chFlavor, chSeasonal;
JTextField tName, tName2, tName3, tName4, tID, tID2, tID3, tID4, tColor, tColor2, tColor3, tColor4, delName, delID, filName;
JLabel labHome, labPlant, labName, labNam2, labNam3, labNam4, labID, labID2, labID3, labID4, labColor, labColor2;
JLabel labColor3, labColor4, labCheck1, labCheck2, labCheck3, labCheck4, labRemN, labRemI;
JTextArea plantDisplay, plantDisplay2, Displayofplant3;
JMenuBar menu;
JMenu filemenu;
JMenuItem exit;
String hOME = new String ("<html><center><bold><font size = 15>WELCOME TO THE PLANT INTERFACE<br/></font></font size = 13>CHOOSE AN ACTION</center></font></html>");
String baDD = new String ("<html><center><bold><font size = 5>ADD<br/></font><font size = 2>A PLANT</center></font></html>");
String brEM = new String ("<html><center><bold><font size = 5>REMOVE<br/></font><font size = 2>A PLANT</center></font></html>");
String bdIS = new String ("<html><center><bold><font size = 5>DISPLAY<br/></font><font size = 2>ALL PLANTS</center></font></html>");
String bsOR = new String ("<html><center><bold><font size = 5>SORT<br/></font><font size = 2>YOUR PLANTS</center></font></html>");
String bfIL = new String ("<html><center><bold><font size = 5>FILTER<br/></font><font size = 2>Search for plant</center></font></html>");
String baNA = new String ("<html><center><bold><font size = 5>ANALYZE<br/></font><font size = 2>ALL PLANTS</center></font></html>");
JPanel mPanelOne = new JPanel();
JPanel mPanelTwo = new JPanel();
JPanel mPanelThr = new JPanel();
JPanel mPanelFou = new JPanel();
JPanel mPanelFiv = new JPanel();
JPanel mPanelSix = new JPanel();
JPanel mPanelSev = new JPanel();
JPanel mPanelEig = new JPanel();
JPanel mPanelNin = new JPanel();
JPanel mPanelTen = new JPanel();
JPanel mPanelEle = new JPanel();
JPanel mPanelTwe = new JPanel();
ArrayList<Plant> flowerPack = new ArrayList();
class BtnAddHndlr implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals(baDD))
{
JOptionPane.showOptionDialog(null, mPanelThr, "Add a plant", JOptionPane.CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,null,null,null);
}
}
}
class BtnRemHndlr implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals(brEM))
{
removePlant();
}
}
}
class BtnDisHndlr implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals(bdIS))
{
displayPlant();
}
}
}
class BtnSorHndlr implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals(bsOR))
{
sortPlant();
}
}
}
class BtnFilHndlr implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals(bfIL))
{
filterPlant();
}
}
}
class BtnAnaHndlr implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals(baNA))
{
analyzePlant();
}
}
}
class ExitHandler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
class AddHandler implements ActionListener
{
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand().equals("Flower"))
{
addFlower();
}
else if (e.getActionCommand().equals("Fungus"))
{
addFungus();
}
else if (e.getActionCommand().equals("Weed"))
{
addWeed();
}
else if (e.getActionCommand().equals("Herb"))
{
addHerb();
}
}
}
private void addPlant()
{
JOptionPane.showMessageDialog(null, "Double Hooray!");
}
private void addFlower()
{
JOptionPane.showMessageDialog(null, mPanelFou, "Add a flower", JOptionPane.PLAIN_MESSAGE);
String name, Color, ID;
boolean thorns, smell;
name = tName.getText();
ID = tID.getText();
Color = tColor.getText();
thorns = chThorns.isSelected();
smell = chSmell.isSelected();
flowerPack.add(new Flower(name, ID, Color, thorns, smell));
JOptionPane.showMessageDialog(null, "Flower " + name + " " + ID + " added to pack.");
}
private void addFungus()
{
JOptionPane.showMessageDialog(null, mPanelFiv, "Add a fungus", JOptionPane.PLAIN_MESSAGE);
String name, Color, ID;
boolean poisonous;
name = tName2.getText();
ID = tID2.getText();
Color = tColor2.getText();
poisonous = chPoisonous.isSelected();
flowerPack.add(new Fungus(name, ID, Color, poisonous));
JOptionPane.showMessageDialog(null, "Fungus " + name + " " + ID + " added to pack.");
}
private void addWeed()
{
JOptionPane.showMessageDialog(null, mPanelSix, "Add a weed", JOptionPane.PLAIN_MESSAGE);
String name, Color, ID;
boolean poisonous, edible, medicinal;
name = tName3.getText();
ID = tID3.getText();
Color = tColor3.getText();
poisonous = chPoisonous2.isSelected();
edible = chEdible.isSelected();
medicinal = chMedicinal.isSelected();
flowerPack.add(new Weed(name, ID, Color, poisonous, edible, medicinal));
JOptionPane.showMessageDialog(null, "Weed " + name + " " + ID + " added to pack.");
}
private void addHerb()
{
JOptionPane.showMessageDialog(null, mPanelSev, "Add an herb", JOptionPane.PLAIN_MESSAGE);
String name, Color, ID;
boolean flavor, medicinal, seasonal;
name = tName4.getText();
ID = tID4.getText();
Color = tColor4.getText();
flavor = chFlavor.isSelected();
medicinal = chMedicinal2.isSelected();
seasonal = chSeasonal.isSelected();
flowerPack.add(new Herb(name, ID, Color, flavor, medicinal, seasonal));
JOptionPane.showMessageDialog(null, "Herb " + name + " " + ID + " added to pack.");
}
private void removePlant()
{
if (flowerPack.size() > 0)
{
JOptionPane.showOptionDialog(null, mPanelEig, "Remove a plant", JOptionPane.CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,null,null,null);
String name, ID;
name = delName.getText().trim();
ID = delID.getText().trim();
boolean found = false;
Iterator<Plant> itr = flowerPack.iterator();
if(name != null || ID != null)
{
while(itr.hasNext())
{
Plant flowers = itr.next();
if(flowers.getID().equals(ID) || flowers.getName().equals(name))
{
itr.remove();
found = true;
break;
}
}
if (found)
{
JOptionPane.showMessageDialog(null, "Plant successfully removed from pack.");
}
else
{
JOptionPane.showMessageDialog(null, "Plant not found. Please try again.");
}
}
}
else
{
JOptionPane.showMessageDialog(null, "There are no plants in your pack.");
}
}
private void displayPlant()
{
if (flowerPack.size() > 0)
{
plantDisplay.setText("");
for(int i=0; i<flowerPack.size(); i++)
{
Plant f = flowerPack.get(i);
plantDisplay.append(" " + (i + 1) + " - " + "Name: " + f.getName() + " Plant ID: " + f.getID() + " Color: "
+ f.getColor() + " Thorns: " + f.getThorns() + " Smell: " + f.getSmell() + " Poisonous: "
+ f.getPoisonous() + " Edible: " + f.getEdible() + " Medicinal: " + f.getMedicinal() + " Flavor: " + f.getFlavor() + " Seasonal: " +
f.getSeasonal());
}
JOptionPane.showMessageDialog(null, mPanelNin, "Display all plants", JOptionPane.PLAIN_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null, "There are no plants in your pack.");
}
}
private void sortPlant()
{
if (flowerPack.size() > 0)
{
Collections.sort(flowerPack, Plant.plantNameComparator);
for(Plant plort: flowerPack)
{
}
JOptionPane.showMessageDialog(null, "<html>Pack successfully sorted in alphabetical order by name.<br/><br/></font>Please click 'DISPLAY' from main interface.</font></html>");
}
else
{
JOptionPane.showMessageDialog(null, "There are no plants in your pack.");
}
}
private void filterPlant()
{
if (flowerPack.size() > 0)
{
JOptionPane.showOptionDialog(null, mPanelTen, "Filtered Search for a plant", JOptionPane.CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE,null,null,null);
String name;
name = filName.getText().trim();
boolean found = false;
plantDisplay2.setText("");
for (int i=0; i < flowerPack.size(); i++)
{
Plant f = flowerPack.get(i);
if (f.getName().contains(name))
{
plantDisplay2.append(" Plant search of '" + name + "' produced the following result: Name: " + f.getName() + " PlantID: " + f.getID() +
" ...in position " + (i + 1));
found = true;
}
}
JOptionPane.showMessageDialog(null, mPanelEle, "Display results", JOptionPane.PLAIN_MESSAGE);
if (found == false)
{
JOptionPane.showMessageDialog(null, "Your search entry of '" + name + "' yielded no results. **Plant not present.** ");
}
}
else
{
JOptionPane.showMessageDialog(null, "There are no plants in your pack.");
}
}
private void analyzePlant()
{
//JOptionPane.showMessageDialog(null, "Double Hooray!");
int size = flowerPack.size();
String ar = new String ("ar");
String r = new String ("r");
String s = new String ("s");
if (size == 0)
{
JOptionPane.showMessageDialog(null, "There are no plants in your pack.");
}
else
{
for (int i=0; i < flowerPack.size(); i++)
{
Plant f = flowerPack.get(i);
int n1 = numInstances(f.getName(), ar);
int n2 = numInstances(f.getName(), r);
int n3 = numInstances(f.getName(), s);
Displayofplant3.append(" String " + ar + " " + r + " " + s + " " + f.getName() + " " + n1 + " " + n2 + " " + n3);
}
JOptionPane.showMessageDialog(null, mPanelTwe, "Display results", JOptionPane.PLAIN_MESSAGE);
}
}
public int numInstances(String str, String substr)
{
if(str.length() < substr.length())
{
return 0;
}
if (str.substring(0, substr.length()).equals(substr))
{
return 1 + numInstances(str.substring(substr.length()),substr);
}
return numInstances(str.substring(1), substr);
}
}
Plant.java
public class Plant
{
String name;
String ID;
String Color;
boolean thorns;
boolean smell;
boolean Poisonous;
boolean Edible;
boolean Medicinal;
boolean Flavor;
boolean Seasonal;
Plant(String name,String ID,String Color)
{
this.name=name;
this.ID=ID;
this.Color=Color;
}
public String getName()
{
return name;
}
public String getID()
{
return ID;
}
public String getColor()
{
return Color;
}
public boolean getThorns()
{
return thorns;
}
public boolean getSmell()
{
return smell;
}
public boolean getPoisonous()
{
return Poisonous;
}
public boolean getEdible()
{
return Edible;
}
public boolean getMedicinal()
{
return Medicinal;
}
public boolean getSeasonal()
{
return Seasonal;
}
public boolean getFlavor()
{
return Flavor;
}
public static Comparator<Plant> plantNameComparator = new Comparator<Plant>()
{
public int compare(Plant p1, Plant p2)
{
String PlantName1 = p1.getName().toUpperCase();
String PlantName2 = p2.getName().toUpperCase();
return PlantName1.compareTo(PlantName2);
}
};
//@Override
public String toString()
{
return "";
}
}
Flower.java
public 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, Color);
this.Color=Color;
this.thorns=thorns;
this.smell=smell;
}
public String getColor()
{
return Color;
}
public boolean getThorns()
{
return thorns;
}
public boolean getSmell()
{
return smell;
}
}
Fungus.java
public class Fungus extends Plant
{
String Color;
boolean Poisonous;
public Fungus(String name, String ID,String Color,boolean Poisonous)
{
super(name, ID, Color);
this.Color=Color;
this.Poisonous=Poisonous;
}
public String getColor()
{
return Color;
}
public boolean getPoisonous()
{
return Poisonous;
}
}
Weed.java
public 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, Color);
this.Color=Color;
this.Poisonous=Poisonous;
this.Edible=Edible;
this.Medicinal=Medicinal;
}
public String getColor()
{
return Color;
}
public boolean getPoisonous()
{
return Poisonous;
}
public boolean getEdible()
{
return Edible;
}
public boolean getMedicinal()
{
return Medicinal;
}
}
Herb.java
*/
public class Herb extends Plant
{
String Color;
boolean Flavor;
boolean Seasonal;
boolean Medicinal;
public Herb(String name, String ID, String Color, boolean Flavor, boolean Seasonal, boolean Medicinal)
{
super(name, ID, Color);
this.Color=Color;
this.Flavor=Flavor;
this.Seasonal=Seasonal;
this.Medicinal=Medicinal;
}
public String getColor()
{
return Color;
}
public boolean getFlavor()
{
return Flavor;
}
public boolean getSeasonal()
{
return Seasonal;
}
public boolean getMedicinal()
{
return Medicinal;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.