\"Using our existing flower pack we must add the following feature: Using recurs
ID: 3834739 • Letter: #
Question
"Using our existing flower pack we must add the following feature:
Using recursion (no loops!) you must count the number of times a specific character, or set of characters appears in the name of each plant in your pack. We will call this 'analyzing' for the sake of future terminology. Examples are provided on the last page.
The following features are also required but should only be slightly changed from previous requirements.
You must have a graphical user interface implemented. This GUI MUST be unique but can be the same as you've previously used.
You must be able to add, remove, sort, filter, save, load and now analyze information from our flower pack.
The flower pack should hold flowers, weeds, fungus, and 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)
Analysis
"ar"
"ne"
"um"
"ll"
"r"
"u"
"dragon"
Name
Amaranthus
1
0
0
0
1
1
0
Anemone
0
2
0
0
0
0
0
Buplerum
0
0
1
0
1
2
0
Calla Lilly
0
0
0
2
0
0
0
Poinsettia
0
0
0
0
0
0
0
Ranunculus
0
0
0
0
0
3
0
Snapdragon
0
0
0
0
1
0
1
Analysis can be case sensitive, if you so desire. Remember - you MUST use recursion to solve this problem. That means that not a single loop should be called when doing these calculations. You CAN use a loop when you want to move from analyzing one flower to the next, but your loop CANNOT be used when analyzing a specific flower."
My existing GUI program, with faulty analyze method:
package Labs;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Font;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PlantDriverGUI extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
JRadioButton rdoBtnPlant, rdoBtnFlower, rdoBtnHerb, rdoBtnWeed, rdoBtnFungus;
JCheckBox isPoisonous, isEdible, isMedicinal, hasFlavor, isSeasonal, hasThorns, isAromatic;
JButton btnAdd, btnRemove, btnFilter, btnAnalyze, btnClear, btnSort, btnDisplayPlants;
JTextField txtPlantID, txtColor, txtName;
JScrollPane scrollPane;
JTextArea plantSummary;
JMenuBar appMenu;
JMenu fileMenu;
JMenuItem open, save;
ArrayList plantList = new ArrayList();
public PlantDriverGUI() {
try {
this.setTitle("Plant Pack Interface");
setLayout(new FlowLayout());
JPanel plantPanel1 = new JPanel();
JPanel plantPanel2 = new JPanel();
JPanel plantPanel3 = new JPanel();
JPanel plantPanel4 = new JPanel(new BorderLayout());
rdoBtnPlant = new JRadioButton("Plant");
rdoBtnFlower = new JRadioButton("Flower");
rdoBtnWeed = new JRadioButton("Weed");
rdoBtnHerb = new JRadioButton("Herb");
rdoBtnFungus = new JRadioButton("Fungus");
rdoBtnPlant.setSelected(true);
ButtonGroup plantSelection = new ButtonGroup();
plantSelection.add(rdoBtnPlant);
plantSelection.add(rdoBtnFlower);
plantSelection.add(rdoBtnHerb);
plantSelection.add(rdoBtnWeed);
plantSelection.add(rdoBtnFungus);
JLabel plantSelectLabel = new JLabel("Please make a selection: ");
plantSelectLabel.setFont(new Font("Arial", 20, 20));
add(plantSelectLabel);
add(rdoBtnPlant);
add(rdoBtnFlower);
add(rdoBtnWeed);
add(rdoBtnHerb);
add(rdoBtnFungus);
rdoBtnPlant.addActionListener(new RadioHandler());
rdoBtnFlower.addActionListener(new RadioHandler());
rdoBtnHerb.addActionListener(new RadioHandler());
rdoBtnWeed.addActionListener(new RadioHandler());
rdoBtnFungus.addActionListener(new RadioHandler());
JLabel plantIdentity = new JLabel("Enter Name:");
txtName = new JTextField("", 15);
JLabel idLabel = new JLabel("Enter ID:");
txtPlantID = new JTextField("", 15);
JLabel clrLabel = new JLabel("Enter Color:");
txtColor = new JTextField("", 15);
plantPanel1.add(plantIdentity);
plantPanel1.add(txtName);
plantPanel1.add(idLabel);
plantPanel1.add(txtPlantID);
plantPanel1.add(clrLabel);
plantPanel1.add(txtColor);
add(plantPanel1);
JLabel traits = new JLabel("Traits:");
isPoisonous = new JCheckBox("Poisonous");
isEdible = new JCheckBox("Edible");
isMedicinal = new JCheckBox("Medicinal");
hasFlavor = new JCheckBox("Flavorful");
isSeasonal = new JCheckBox("Seasonal");
hasThorns = new JCheckBox("Thorny");
isAromatic = new JCheckBox("Aromatic");
isPoisonous.setEnabled(false);
isEdible.setEnabled(false);
isMedicinal.setEnabled(false);
hasFlavor.setEnabled(false);
isSeasonal.setEnabled(false);
hasThorns.setEnabled(false);
isAromatic.setEnabled(false);
plantPanel2.add(traits);
plantPanel2.add(isPoisonous);
plantPanel2.add(isEdible);
plantPanel2.add(isMedicinal);
plantPanel2.add(hasFlavor);
plantPanel2.add(isSeasonal);
plantPanel2.add(hasThorns);
plantPanel2.add(isAromatic);
add(plantPanel2);
btnAdd = new JButton("Add");
btnRemove = new JButton("Remove");
btnSort = new JButton("Sort");
btnDisplayPlants = new JButton("Display Plant Pack");
btnFilter = new JButton("Filter");
btnAnalyze = new JButton("Analyze");
btnClear = new JButton("Clear");
btnAdd.addActionListener(new ButtonHandlerAdd());
btnRemove.addActionListener(new ButtonHandlerRemove());
btnDisplayPlants.addActionListener(new ButtonHandlerDisplay());
btnClear.addActionListener(new ButtonHandlerClear());
btnSort.addActionListener(new ButtonHandlerSort());
btnFilter.addActionListener(new ButtonHandlerFilter());
btnAnalyze.addActionListener(new ButtonHandlerAnalyze());
plantPanel3.add(btnAdd);
plantPanel3.add(btnRemove);
plantPanel3.add(btnSort);
plantPanel3.add(btnDisplayPlants);
plantPanel3.add(btnFilter);
plantPanel3.add(btnAnalyze);
plantPanel3.add(btnClear);
plantSummary = new JTextArea(10, 60);
plantSummary.setBackground(Color.YELLOW);
plantSummary.setFont(new Font("Arial", Font.PLAIN, 14));
plantSummary.setEditable(false);
plantSummary.setVisible(true);
scrollPane = new JScrollPane(plantSummary);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
plantPanel4.add(scrollPane, BorderLayout.SOUTH);
appMenu = new JMenuBar();
fileMenu = new JMenu("File");
open = new JMenuItem("Open");
save = new JMenuItem("Save");
fileMenu.add(open);
fileMenu.add(save);
appMenu.add(fileMenu);
setJMenuBar(appMenu);
save.addActionListener(new FileMenuSaveFilter());
open.addActionListener(new FileMenuOpenFilter());
add(plantPanel3);
add(plantPanel4);
setSize(900, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
} catch (Exception e) {
System.out.println(e);
}
}
public static void main(String[] args) {
new PlantDriverGUI();
}
private void addPlant() {
String plantName = txtName.getText();
String plantColor = txtColor.getText();
String plantID = txtPlantID.getText();
Plant thePlant = new Plant(plantColor, plantID, plantName);
plantList.add(thePlant);
}
private void addFlower() {
String flowerName;
String flowerColor;
String flowerID;
boolean isItAromatic;
boolean isItThorny;
flowerName = txtName.getText();
flowerColor = txtColor.getText();
flowerID = txtPlantID.getText();
isItThorny = hasThorns.isSelected();
isItAromatic = isAromatic.isSelected();
Flower theFlower = new Flower(flowerColor, flowerID, flowerName, isItAromatic, isItThorny);
plantList.add(theFlower);
}
private void addFungus() {
String fungusName;
String fungusColor;
String fungusID;
boolean isItPoisonous;
fungusName = txtName.getText();
fungusColor = txtColor.getText();
fungusID = txtPlantID.getText();
isItPoisonous = isPoisonous.isSelected();
Fungus newFungus = new Fungus(fungusColor, fungusID, fungusName, isItPoisonous);
plantList.add(newFungus);
}
private void addWeed() {
String weedName;
String weedColor;
String weedID;
boolean isItEdible;
boolean isItMedicinal;
boolean isItPoisonous;
weedName = txtName.getText();
weedColor = txtColor.getText();
weedID = txtPlantID.getText();
isItEdible = isEdible.isSelected();
isItMedicinal = isMedicinal.isSelected();
isItPoisonous = isPoisonous.isSelected();
Weed theWeed = new Weed(weedColor, weedID, weedName, isItEdible, isItMedicinal, isItPoisonous);
plantList.add(theWeed);
}
private void addHerb() {
String herbName;
String herbColor;
String herbID;
boolean doesItHaveFlavor;
boolean isItMedicinal;
boolean isItSeasonal;
herbName = txtName.getText();
herbColor = txtColor.getText();
herbID = txtPlantID.getText();
isItMedicinal = isMedicinal.isSelected();
isItSeasonal = isSeasonal.isSelected();
doesItHaveFlavor = hasFlavor.isSelected();
Herb theHerb = new Herb(herbColor, herbID, herbName, doesItHaveFlavor, isItMedicinal, isItSeasonal);
plantList.add(theHerb);
}
private void sortPlants() {
plantSummary.setText("");
Collections.sort(plantList, new Comparator() {
@Override
public int compare(Plant plant2, Plant plant1) {
return plant2.getName().compareTo(plant1.getName());
}
});
for (Plant thePlant : plantList) {
plantSummary.append(thePlant.toString() + " ");
}
}
private void filterPlants() {
plantSummary.setText("");
String plantName = JOptionPane.showInputDialog(null, "Enter the partial search term.");
for (Plant thePlant : plantList) {
if (thePlant.getName().charAt(0) == plantName.charAt(0)) {
plantSummary.append(thePlant.toString() + " ");
JOptionPane.showMessageDialog(null, "Review the results of your partial search in the text box.");
}
}
}
private void analyzePlants() {
//this is the section that needs work
String[] keyWords = { "ar", "ne", "um", "ll", "r", "u", "dragon" };
Integer[] result;
plantList.size();
plantSummary.removeAll();
JTextArea txtResult = new JTextArea();
ArrayList arr = new ArrayList();
for (int i = 0; i < plantList.size(); i++) {
result = new Integer[keyWords.length];
for (int j = 0; j < keyWords.length; j++) {
result[j] = analyze(plantList.get(i).getName().trim(), keyWords[j].trim(), 0);
}
// analyse( plantList.get(i).getName().trim() , 0, 0);
arr.add(result.clone());
}
// printing result
for (int i = 0; i < plantList.size(); i++) {
txtResult.append(plantList.get(i).getName());
result = arr.get(i);
for (int j = 0; j < result.length; j++) {
txtResult.append(" " + String.valueOf(result[j]));
}
txtResult.append(" ");
}
plantSummary.add(txtResult);
}
private int analyze(String plantName, String keyWord, int count) {
if (plantName.length() == 0) {
return count;
}
if (plantName.length() < keyWord.length()) {
return count;
}
if (plantName.contains(keyWord)) {
count++;
plantName = plantName.replaceFirst(keyWord, "");
return analyze(plantName, keyWord, count);
}
return count;
}
private void savePlantsToFile() throws IOException {
File plantFile = new File("plantFile.txt");
FileOutputStream plantStream = new FileOutputStream(plantFile);
PrintWriter plantOutStream = new PrintWriter(plantStream);
for (Plant thePlant : plantList) {
plantOutStream.println(thePlant.toString());
}
plantOutStream.close();
}
private void readPlantsFromFile() throws FileNotFoundException {
Scanner plantInput = new Scanner(new File("plantInputData.txt"));
try {
while (plantInput.hasNext()) {
Plant newPlant = new Plant(plantInput.next(), plantInput.next(), plantInput.next());
plantSummary.append(newPlant.toString() + " ");
}
plantInput.close();
} catch (Exception e) {
e.printStackTrace();
}
}
class RadioHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Plant")) {
isPoisonous.setEnabled(false);
isEdible.setEnabled(false);
isMedicinal.setEnabled(false);
hasFlavor.setEnabled(false);
isSeasonal.setEnabled(false);
hasThorns.setEnabled(false);
isAromatic.setEnabled(false);
} else if (e.getActionCommand().equals("Fungus")) {
isPoisonous.setEnabled(true);
isEdible.setEnabled(false);
isMedicinal.setEnabled(false);
hasFlavor.setEnabled(false);
isSeasonal.setEnabled(false);
hasThorns.setEnabled(false);
isAromatic.setEnabled(false);
} else if (e.getActionCommand().equals("Weed")) {
isPoisonous.setEnabled(true);
isEdible.setEnabled(true);
isMedicinal.setEnabled(true);
hasFlavor.setEnabled(false);
isSeasonal.setEnabled(false);
hasThorns.setEnabled(false);
isAromatic.setEnabled(false);
} else if (e.getActionCommand().equals("Flower")) {
hasThorns.setEnabled(true);
isAromatic.setEnabled(true);
isPoisonous.setEnabled(false);
isMedicinal.setEnabled(false);
isSeasonal.setEnabled(false);
hasFlavor.setEnabled(false);
isEdible.setEnabled(false);
} else if (e.getActionCommand().equals("Herb")) {
isPoisonous.setEnabled(false);
isEdible.setEnabled(false);
isMedicinal.setEnabled(true);
hasFlavor.setEnabled(true);
isSeasonal.setEnabled(true);
hasThorns.setEnabled(false);
isAromatic.setEnabled(false);
}
}
}
class ButtonHandlerAdd implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("The plant has been added to the Plant Pack.");
if (e.getActionCommand().equals("Add") && rdoBtnPlant.isSelected()) {
addPlant();
} else if (e.getActionCommand().equals("Add") && rdoBtnFlower.isSelected()) {
addFlower();
} else if (e.getActionCommand().equals("Add") && rdoBtnFungus.isSelected()) {
addFungus();
} else if (e.getActionCommand().equals("Add") && rdoBtnWeed.isSelected()) {
addWeed();
} else if (e.getActionCommand().equals("Add") && rdoBtnHerb.isSelected()) {
addHerb();
}
}
}
class ButtonHandlerRemove implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("You have chosen to remove a plant from the Plant Pack.");
if (e.getActionCommand().equals("Remove")) {
String plantToRemove = JOptionPane.showInputDialog(null,
"Enter the ID of the plant you wish to remove:");
for (Plant thePlant : plantList) {
if (thePlant.getID().equals(plantToRemove)) {
plantList.remove(thePlant);
JOptionPane.showMessageDialog(null,
"The plant with ID: " + thePlant.getID() + " has been removed from the Plant Pack.");
}
}
}
}
}
class ButtonHandlerDisplay implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
plantSummary.setText("");
for (Plant thePlant : plantList) {
plantSummary.append(thePlant.toString() + " ");
}
}
}
class ButtonHandlerClear implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
plantSummary.setText("");
}
}
class ButtonHandlerSort implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
sortPlants();
}
}
class ButtonHandlerFilter implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
filterPlants();
}
}
class ButtonHandlerAnalyze implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
analyzePlants();
}
}
class FileMenuSaveFilter implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
try {
savePlantsToFile();
} catch (IOException ex) {
Logger.getLogger(PlantDriverGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
class FileMenuOpenFilter implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
try {
readPlantsFromFile();
} catch (FileNotFoundException ex) {
Logger.getLogger(PlantDriverGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
The flower pack (actualy called plant pack within the program) is an arraylist that holds flowers, fungus, herbs, and weeds inputted by the user, each with variou traits. All of these classes are a sub-class of the plant class. This arraylist can be updated, i.e., plants can be added and removed from the pack. The program also allows the contents of the arraylist to be sorted, filtered, and displayed.
Plant class:
package Labs;
import java.util.*;
public class Plant {
private String id;
private String name;
private String color;
public Plant() {
this.id = "";
this.name = "";
this.color = "";
}
public Plant(String plantColor, String plantID, String plantName) {
this.id = plantID;
this.color = plantColor;
this.name = plantName;
}
public void setID(String plantID) {
this.id = plantID;
}
public void setColor(String plantColor) {
this.color = plantColor;
}
public void setName(String plantName) {
this.name = plantName;
}
public String getName() {
return name;
}
public String getColor() {
return color;
}
public String getID() {
return id;
}
public String toString() {
return "Plant name: " + this.getName() + "; Plant ID: " + this.getID() + "; Plant color: " + this.getColor() + ".";
}
@Override
public boolean equals(Object otherPlant) {
if (otherPlant == null) {
return false;
}
if (!Plant.class.isAssignableFrom(otherPlant.getClass())) {
return false;
}
final Plant other = (Plant) otherPlant;
if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
return false;
}
if (!(this.color.equals(other.color))) {
return false;
}
if (!(this.id.equals(other.id))) {
return false;
}
return true;
}
}
Flower class:
package Labs;
public class Flower extends Plant {
private boolean hasThorns, isAromatic;
public Flower(String flowerColor, String flowerID, String flowerName, boolean isAromatic, boolean hasThorns) {
super(flowerColor, flowerID, flowerName);
this.hasThorns = hasThorns;
this.isAromatic = isAromatic;
}
public void setHasThorns(boolean hasThorns) {
this.hasThorns = hasThorns;
}
public boolean getHasThorns() {
return hasThorns;
}
public void setIsAromatic(boolean isAromatic) {
this.isAromatic = isAromatic;
}
public boolean isAromatic() {
return isAromatic;
}
public String toString() {
return "Flower name: " + this.getName() + "; Flower ID: " + this.getID() + "; Flower color: " + this.getColor()
+ "; Flower has thorns = " + this.getHasThorns() + "; Flower is aromatic = " + this.isAromatic() + ".";
}
@Override
public boolean equals(Object otherFlower) {
if (otherFlower == null) {
return false;
}
if (!Flower.class.isAssignableFrom(otherFlower.getClass())) {
return false;
}
Flower other = (Flower) otherFlower;
if (!(this.getColor().equals(other.getColor()))) {
return false;
}
if (!(this.getID().equals(other.getID()))) {
return false;
}
if (!(this.getName().equals(other.getName()))) {
return false;
}
if (this.hasThorns != other.hasThorns) {
return false;
}
if (this.isAromatic != other.isAromatic) {
return false;
}
return true;
}
}
Weed class:
package Labs;
import java.util.*;
public class Weed extends Plant {
private boolean isEdible, isMedicinal, isPoisonous;
public Weed(String weedColor, String weedID, String weedName, boolean isEdible, boolean isMedicinal,
boolean isPoisonous) {
super(weedColor, weedID, weedName);
this.isEdible = isEdible;
this.isMedicinal = isMedicinal;
this.isPoisonous = isPoisonous;
}
public void setIsEdible(boolean isEdible) {
this.isEdible = isEdible;
}
public boolean getIsEdible() {
return isEdible;
}
public void setIsMedicinal(boolean isMedicinal) {
this.isMedicinal = isMedicinal;
}
public boolean getIsMedicinal() {
return isMedicinal;
}
public void setIsPoisonous(boolean isPoisonous) {
this.isPoisonous = isPoisonous;
}
public boolean getIsPoisonous() {
return isPoisonous;
}
public String toString() {
return "Weed name: " + this.getName() + "; Weed ID: "
+ this.getID() + "; Weed color: " + this.getColor() + "; Weed is edible = " + this.getIsEdible() + "; Weed is Poisonous = "
+ this.getIsPoisonous() + "; Weed is medicinal = " + this.getIsMedicinal() + ".";
}
@Override
public boolean equals(Object otherWeed) {
if (otherWeed == null) {
return false;
}
if (!(Weed.class.isAssignableFrom(otherWeed.getClass()))) {
return false;
}
Weed other = (Weed) otherWeed;
if (!(this.getID().equals(other.getID()))) {
return false;
}
if (!(this.getName().equals(other.getName()))) {
return false;
}
if (!(this.getColor().equals(other.getColor()))) {
return false;
}
if (this.isEdible != other.isEdible) {
return false;
}
if (this.isMedicinal != other.isMedicinal) {
return false;
}
if (this.isPoisonous != other.isPoisonous) {
return false;
}
return true;
}
}
Fungus class:
package Labs;
public class Fungus extends Plant {
private boolean isPoisonous;
public Fungus(String fungusColor, String fungusID, String fungusName, boolean isPoisonous) {
super(fungusColor, fungusID, fungusName);
this.isPoisonous = isPoisonous;
}
public void setIsPoisonous(boolean isPoisonous) {
this.isPoisonous = isPoisonous;
}
public boolean getIsPoisonous() {
return isPoisonous;
}
public String toString() {
return "Fungus ID: " + this.getID() + "; Fungus name: " + this.getName() + "; Fungus color: " + this.getColor()
+ "; Fungus is poisonous = " + this.getIsPoisonous() + ".";
}
@Override
public boolean equals(Object otherFungus) {
if (otherFungus == null) {
return false;
}
if (!Fungus.class.isAssignableFrom(otherFungus.getClass())) {
return false;
}
Fungus other = (Fungus) otherFungus;
if (!(this.getColor().equals(other.getColor()))) {
return false;
}
if (!(this.getID().equals(other.getID()))) {
return false;
}
if (!(this.getName().equals(other.getName()))) {
return false;
}
if (this.isPoisonous != other.isPoisonous) {
return false;
}
return true;
}
}
Herb class:
package Labs;
public class Herb extends Plant {
private boolean hasFlavor, isMedicinal, isSeasonal;
public Herb(String herbColor, String herbID, String herbName, boolean hasFlavor, boolean isMedicinal,
boolean isSeasonal) {
super(herbColor, herbID, herbName);
this.hasFlavor = hasFlavor;
this.isMedicinal = isMedicinal;
this.isSeasonal = isSeasonal;
}
public void setHasFlavor(boolean hasFlavor) {
this.hasFlavor = hasFlavor;
}
public boolean getHasFlavor() {
return hasFlavor;
}
public void setIsMedicinal(boolean isMedicinal) {
this.isMedicinal = isMedicinal;
}
public boolean getIsMedicinal() {
return isMedicinal;
}
public void setIsSeasonal(boolean isSeasonal) {
this.isSeasonal = isSeasonal;
}
public boolean getIsSeasonal() {
return isSeasonal;
}
public String toString() {
return "Herb name: " + this.getName() + "; Herb ID: " + this.getID() + "; Herb color: " + this.getColor()
+ "; Herb has flavor = " + this.getHasFlavor() + "; Herb is medicinal = " + this.getIsMedicinal()
+ "; Herb is seasonal = " + this.getIsSeasonal() + ".";
}
@Override
public boolean equals(Object otherHerb) {
if (otherHerb == null) {
return false;
}
if (!(Herb.class.isAssignableFrom(otherHerb.getClass()))) {
return false;
}
Herb other = (Herb) otherHerb;
if (!(this.getID().equals(other.getID()))) {
return false;
}
if (!(this.getName().equals(other.getName()))) {
return false;
}
if (!(this.getColor().equals(other.getColor()))) {
return false;
}
if (this.hasFlavor != other.hasFlavor) {
return false;
}
if (this.isMedicinal != other.isMedicinal) {
return false;
}
if (this.isSeasonal != other.isSeasonal) {
return false;
}
return true;
}
}
Analysis
"ar"
"ne"
"um"
"ll"
"r"
"u"
"dragon"
Name
Amaranthus
1
0
0
0
1
1
0
Anemone
0
2
0
0
0
0
0
Buplerum
0
0
1
0
1
2
0
Calla Lilly
0
0
0
2
0
0
0
Poinsettia
0
0
0
0
0
0
0
Ranunculus
0
0
0
0
0
3
0
Snapdragon
0
0
0
0
1
0
1
Explanation / Answer
ANSWER::
import javax.swing.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color;
import java.text.SimpleDateFormat;
public class FlowerShop extends MouseAdapter implements ActionListener{
Flower inventory;
JButton [] flowerButton;
JButton transactionOver;
JFrame fProduct, fReceipt;
JPanel buttonPanel;
JPanel textPanel;
JPanel receiptPanel;
JPanel backgroundPanel;
JTextField flowerPurchased, unitPrice, quantity;
JTextField purchasePrice, netCostField;
JTextArea receipt;
JScrollPane scrollPane;
int flowerCount=0;
int maxFlowers=100;
double totalCost=0;
Flower [] flower=new Flower[maxFlowers];
String inventoryLocation="FlowerPictures";
public FlowerShop(){
}
public void getFlowerdata(String fileName) throws Exception
{
File file = new File(fileName);
Scanner in = new Scanner(file);
flowerButton=new JButton[maxFlowers] ;
while (in.hasNext()){
String name=in.next();
double unitPrice=in.nextDouble();
String location=inventoryLocation+"/"+name+".jpg";
Flower f=new Flower(name, location, unitPrice);
flower[flowerCount]=f;
flowerCount++;
System.out.println(flowerCount+" "+name +" "+unitPrice);
}
}
public void createButtons()
{
for(int i=0; i<flowerCount; i++){
flowerButton[i]=new JButton(flower[i].getIcon());
flowerButton[i].addActionListener(this);
flowerButton[i].addMouseListener(this);
}
transactionOver=new JButton("Checkout");
transactionOver.addActionListener(this);
}
public void createTextBoxes(){
final int TEXTFIELD_WIDTH=10;
flowerPurchased=new JTextField("None", TEXTFIELD_WIDTH);
unitPrice=new JTextField("$0.0", TEXTFIELD_WIDTH);
quantity=new JTextField("0", TEXTFIELD_WIDTH);
quantity.addActionListener(this);
purchasePrice=new JTextField("$0.0", TEXTFIELD_WIDTH);
netCostField=new JTextField("$0.0", TEXTFIELD_WIDTH);
receipt=new JTextArea("Your receipt ", 20, 40);
scrollPane = new JScrollPane(receipt);
}
public void createPanels()
{
textPanel=new JPanel(new GridLayout(2,5));
textPanel.add(new JLabel("Flower"));
textPanel.add(new JLabel("Quantity"));
textPanel.add(new JLabel("Unit Price"));
textPanel.add(new JLabel("You pay"));
textPanel.add(new JLabel("Total Price"));
textPanel.add(flowerPurchased);
textPanel.add(quantity);
textPanel.add(unitPrice);
textPanel.add(purchasePrice);
textPanel.add(netCostField);
buttonPanel=new JPanel(new GridLayout(5,2));
for(int i=0; i<flowerCount; i++){
buttonPanel.add(flowerButton[i]);
}
buttonPanel.add(transactionOver);
receiptPanel=new JPanel();
receiptPanel.setBackground(Color.BLUE);
receiptPanel.add(scrollPane);
backgroundPanel=new JPanel();
backgroundPanel.setBackground(Color.YELLOW);
backgroundPanel.add(textPanel);
backgroundPanel.add(buttonPanel);
backgroundPanel.add(receiptPanel);
}
public void createFrame()
{
JFrame.setDefaultLookAndFeelDecorated(true);
fProduct=new JFrame("CS 180 Flower Shop");
fProduct.setSize(700, 500);
fProduct.add(backgroundPanel);
fProduct.setVisible(true);
Point p=fProduct.getLocationOnScreen();
Dimension d=fProduct.getSize();
fReceipt=new JFrame("CS 180 Flower Shop: Receipt");
fReceipt.setSize(600, 400);
fReceipt.add(receiptPanel);
fReceipt.setLocation(p.x, p.y+d.height+5);
fReceipt.setVisible(true);
}
public void newTransaction()
{
}
public String getDate(){
Date today;
String output;
SimpleDateFormat formatter;
String pattern="yyyy.MMMMM.dd GGG hh:mm aaa";
formatter = new SimpleDateFormat(pattern);
today = new Date();
output = formatter.format(today);
return(output);
}
public void actionPerformed(ActionEvent e)
{
Object ob=e.getSource();
if(ob==transactionOver)
{
if(totalCost>0){
Date d;
receipt.append(" Your Total: "+String.format("$%.2f ", totalCost));
receipt.append(" Thank you. Please come again. ");
receipt.append(getDate());
}
return;
}
boolean objectLocated=false;
int nextObject=0;
while(nextObject<flowerCount &&!objectLocated){
if(ob==flowerButton[nextObject]){
objectLocated=true;
}else{
nextObject++;
}
}
if(objectLocated)
{
String fPurchased=flower[nextObject].getKind();
flowerPurchased.setText(fPurchased);
double uPrice=flower[nextObject].getUnitPrice();
String formattedUPrice="$"+Double.toString(uPrice);
unitPrice.setText(formattedUPrice);
String q=quantity.getText();
double quantDouble=Double.parseDouble(q);
if(quantDouble>0){
double price=quantDouble*uPrice;
String formattedPrice="$"+String.format("%.2f",price);
purchasePrice.setText(formattedPrice);
totalCost=totalCost+price;
String fTotalCost=String.format("$%.2f ", totalCost);
netCostField.setText("$"+fTotalCost);
String r=fPurchased+" "+q+" "+formattedUPrice+
" "+formattedPrice+" "+fTotalCost+" ";
receipt.append(r);
}
}
}
public void mouseEntered(MouseEvent e)
{
Object ob=e.getSource();
boolean buttonLocated=false;
int nextObject=0;
while(nextObject<flowerCount &&!buttonLocated){
if(ob==flowerButton[nextObject]){
buttonLocated=true;
}else{
nextObject++;
}
}
if(buttonLocated){
flowerPurchased.setText(flower[nextObject].getKind());
unitPrice.setText("$"+Double.toString(flower[nextObject].getUnitPrice()));
}
}
public static void main(String [] args)throws Exception{
String fileName="flowerInventory.txt";
FlowerShop flowerShop=new FlowerShop();
flowerShop.getFlowerdata(fileName);
flowerShop.createButtons();
flowerShop.createTextBoxes();
flowerShop.createPanels();
flowerShop.createFrame();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.