0

Edit 1: Changed == in the Override methods to .equals(). It did not change anything. :-(

Edit 2: Yes, I know whether it has thorns or a smell is boolean, but it's not required right now. I can do it later.

I will try to post the least amount of code possible to keep it concise. I have a Driver class, a Parent class, and three child classes. I am taking user input to build an ArrayList of Strings, then I have to be able to display, remove, filter, and search through the array. I am able to add elements, and display them, however, I have reached an impasse with my remove method. The user entered string is not equalling the strings in the ArrayList, even when I tried an Override equals method. I will show the Driver class, Parent class, and one child class since the other two child classes are irrelevant right now.

Note: I am giving the user an option to pick which plant the user wants to add/remove, thus the multiple methods. I was just trying to create something extra, and I broke my code.

The problem is that it still prints that it was not found in the inventory, and after displaying them again, the element has not been removed. I have not completed the other methods yet, since I am stuck on removing elements. Any help/suggestions would be greatly appreciated. I've tried finding similar problems in case this is a duplicate question, but I'm having a problem that is unique to others since I have multiple classes. I used similar code in another program that only had a Driver and Flower class. It worked fine.

I have not officially learned Override methods, so I tried it on my own to no avail.

Parent class:

    public class Plant {

public String ID;
public String name;

public Plant(String ID, String name) {
    this.ID = ID;
    this.name = name;
}
public void setID(String ID) {this.ID = ID;}
public void setName(String name) {this.name = name;}

public String getID() {return ID;}
public String getName() {return name;}

public String toString() {
    return "ID: " + this.ID + ", Name: " + this.name;
}

@Override
public boolean equals(Object obj) {
    if(!(obj instanceof Plant)) {
        return false;
    }
    Plant plant = (Plant) obj;
    return this.ID.equals(plant.ID) && this.name.equals(plant.name);

}


} 

One of the three child classes:

    public class Flower extends Plant {


public String color;
public String smell;
public String hasThorns;



public Flower(String ID, String name, String color, String smell, String hasThorns) {

    super(ID, name);
    this.color = color;
    this.smell = smell;
    this.hasThorns = hasThorns;
}

public void setColor(String color) {
    this.color = color;
}

public void setSmell(String smell) {
    this.smell = smell;
}

public void setThorns(String hasThorns) {
    this.hasThorns = hasThorns;
}

public String getColor() {
    return color;
}

public String getSmell() {
    return smell;
}

public String getHasThorns() {
    return hasThorns;
}
public String toString() {
    return super.toString() + ", Color: " + this.color + ", Scent? " + this.smell + ", Thorns? " + this.hasThorns;
}
@Override
public boolean equals(Object obj) {
    if(!(obj instanceof Flower)) {
        return false;
    }
    Flower flower = (Flower) obj;
    return this.ID.equals(flower.ID) && this.name.equals(flower.name) && this.color.equals(flower.color) && this.smell.equals(flower.smell) && this.hasThorns.equals(flower.hasThorns);
}

}

The Driver class:

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

    public class Driver {
    public String ID = new String();
    public String name = new String();
    public String color = new String();
    public String smell = new String();
    public String hasThorns = new String();
    public String isPoisonous = new String();
    public String isMedicine = new String();
    public String isEdible = new String();


public static void main(String[] args) {new Driver();}

public Driver() {

    Scanner input = new Scanner(System.in);
    ArrayList<Plant> plantPack = new ArrayList<Plant>();

    System.out.println("Welcome to the Plant Pack interface.");
    System.out.println("Please select a number from the options below.");
    System.out.println("");

    while (true) {

        System.out.println("1: Add a plant to the pack.");
        System.out.println("2: Remove a plant from the pack.");
        System.out.println("3: Search for a specific plant.");
        System.out.println("4: Display your plant pack.");
        System.out.println("5: Filter the plant pack by incomplete name.");
        System.out.println("0: Exit the plant pack interface.");

        // Get the user input
        int userChoice = input.nextInt();

        switch (userChoice) {
            case 1:
                typeSwitch(plantPack);
                break;
            case 2:
                removePlant(plantPack);
                break;
            case 3:
                searchPlants(plantPack);
                break;
            case 4:
                displayPlants(plantPack);
                break;
            case 5:
                filterPlants(plantPack);
                break;
            case 0:
                exitInterface();
                break;
            default:
                System.out.println("Invalid entry. \nPlease choose between 1-5, or 0: ");
                break;
        }
    }
}
public void typeSwitch(ArrayList<Plant> plantPack) {
    Scanner input = new Scanner(System.in);

    System.out.println("Please enter what type of plant you are adding: ");
    System.out.println("1: Flower");
    System.out.println("2: Fungus");
    System.out.println("3: Weed");

    int type = input.nextInt();

    if (type >= 1 && type <= 3) {

        switch (type) {
            case 1:
                addFlower(plantPack);
                break;
            case 2:
                addFungus(plantPack);
                break;
            case 3:
                addWeed(plantPack);
                break;
            }
        }
    else
    {
        System.out.println("Invalid Entry. Please choose between 1-3: ");
        typeSwitch(plantPack);
    }
}
private void addFlower(ArrayList<Plant> plantPack) {
    Flower newFlower = new Flower(ID, name, color, smell, hasThorns);

    Scanner input = new Scanner(System.in);
    if(plantPack.size() < 25)
    {
        System.out.println("Enter the ID number of the flower you wish to add (E.g. F1, F2, etc): ");
        newFlower.setID(input.nextLine());
        System.out.println("Enter the name of the flower you wish to add: ");
        newFlower.setName(input.nextLine());
        System.out.println("Enter the color of the flower you wish to add: ");
        newFlower.setColor(input.nextLine());
        System.out.println("Does the flower have a scent? Yes or No: ");
        newFlower.setSmell(input.nextLine());
        System.out.println("Does the flower have thorns? Yes or No: ");
        newFlower.setThorns(input.nextLine());

        plantPack.add(newFlower);
    }
    else
    {
        System.out.println("You may only hold 25 items in your plant pack. Please remove one before adding another.");
    }
}
private void addFungus(ArrayList<Plant> plantPack) {
    Fungus newFungus = new Fungus(ID, name, color, isPoisonous);

    Scanner input = new Scanner(System.in);
    if(plantPack.size() < 25)
    {
        System.out.println("Enter the ID number of the fungus you wish to add: ");
        newFungus.setID(input.nextLine());
        System.out.println("Enter the name of the fungus you wish to add: ");
        newFungus.setName(input.nextLine());
        System.out.println("Enter the color of the fungus you wish to add: ");
        newFungus.setColor(input.nextLine());
        System.out.println("Is this particular fungus poisonous? Yes or No: ");
        newFungus.setIsPoisonous(input.nextLine());

        plantPack.add(newFungus);
    }
    else
    {
        System.out.println("You may only hold 25 items in your plant pack. Please remove one before adding another.");
    }
}
private void addWeed(ArrayList<Plant> plantPack) {
    Weed newWeed = new Weed(ID, name, color, isEdible, isPoisonous);

    Scanner input = new Scanner(System.in);
    if(plantPack.size() < 25)
    {
        System.out.println("Enter the ID number of the weed you wish to add: ");
        newWeed.setID(input.nextLine());
        System.out.println("Enter the name of the weed you wish to add: ");
        newWeed.setName(input.nextLine());
        System.out.println("Enter the color of the weed you wish to add: ");
        newWeed.setColor(input.nextLine());
        System.out.println("Is this particular weed edible? Yes or No: ");
        newWeed.setIsEdible(input.nextLine());
        System.out.println("Is this particular weed medicinal? Yes or No: ");
        newWeed.setIsMedicine(input.nextLine());

        plantPack.add(newWeed);
    }
    else
    {
        System.out.println("You may only hold 25 items in your plant pack. Please remove one before adding another.");
    }
}
private void removePlant(ArrayList<Plant> plantPack) {
    Scanner input = new Scanner(System.in);

    System.out.println("Which type of plant do you wish to remove?");
    System.out.println("1: Flower");
    System.out.println("2: Fungus");
    System.out.println("3: Weed");

    int type = input.nextInt();
    switch (type) {

        case 1:

            System.out.println("Enter the ID number of the flower you want to remove: ");
            String deleteFlowerID = input.nextLine();
            input.nextLine();
            System.out.println("Enter the name of the flower you wish to remove: ");
            String deleteName = input.nextLine();


            System.out.println("Enter the color of the flower you wish to remove: ");
            String deleteColor = input.nextLine();
            System.out.println("Is this a flower with a scent? Yes or No: ");
            String deleteSmell = input.nextLine();
            System.out.println("Is this a flower with thorns? Yes or No: ");
            String deleteThorns = input.nextLine();


            boolean found = false;


            for (int i = 0; i < plantPack.size(); i++) {
                Flower flower=(Flower)plantPack.get(i);
                if (flower.getID().equals(deleteFlowerID) && flower.getName().equals(deleteName) && flower.getColor().equals(deleteColor) && flower.getSmell().equals(deleteSmell) && flower.getHasThorns().equals(deleteThorns)) {
                    plantPack.remove(i);
                    found = true;
                    break;
                }
            }
            if (found)
            {
                System.out.println("That flower was successfully removed from your inventory.");
            } else
            {
                System.out.println("That flower was not found in your inventory.");
            }
            break;
        case 2:
            break;
        case 3:
            break;
    }

}

private void searchPlants(ArrayList<Plant> plantPack) {

}
private void displayPlants(ArrayList<Plant> plantPack) {

    for(Plant plants : plantPack) {
        System.out.println(plants);
    }

}
private void filterPlants(ArrayList<Plant> plantPack) {

}
private void exitInterface() {

}
}
DevOpsSauce
  • 1,319
  • 1
  • 20
  • 52
  • 1
    Whether a flower has thorns is a boolean value, not a string. – chrylis -cautiouslyoptimistic- Oct 24 '15 at 18:30
  • No, this is NOT a duplicate of the above mentioned link. I even said that in my question. I've already been around the interwebs trying to find it so people wouldn't troll me with downvotes. As I stated in the description, I had a program exactly like this with a Driver and ONE class that worked fine using the .equals() method. I know the difference between == and .equals. My problem stems from polymorphism, but I don't know where. – DevOpsSauce Oct 24 '15 at 18:32
  • I don't have to use a boolean if I don't want to. I understand it is a "yes" or "no"/"true" or "false" scenario, but it's not required for this. – DevOpsSauce Oct 24 '15 at 18:33
  • 2
    You don't *have* to use a boolean, but it's foolish to use something more cumbersome. And this is absolutely a duplicate; you're using the wrong comparison in your method. – chrylis -cautiouslyoptimistic- Oct 24 '15 at 18:35
  • You see how you're using `==` to compare strings? You need to use `.equals()` to compare strings. This is the case with all non-primitive types. – wadda_wadda Oct 24 '15 at 18:39
  • @wadda_wadda Where did I use == to compare strings? – DevOpsSauce Oct 24 '15 at 18:43
  • @csheridan just about every one of your equals() methods -- In your parent class, for example, you are comparing `this.id` with `plant.id`, and both of them are String objects. You're doing this in every single one of your `equals()` methods. Instead, you should be comparing them like so: `plant.id.equals(this.id)`. – wadda_wadda Oct 24 '15 at 18:46
  • OH! I am so sorry. I see where you're talking about that now. Forgive my brain fart. – DevOpsSauce Oct 24 '15 at 18:48
  • But that still didn't work. :-( – DevOpsSauce Oct 24 '15 at 18:52

0 Answers0