麻烦.equals()方法删除arraylist中的String元素 - Java

时间:2015-10-24 18:26:39

标签: java arraylist equals

编辑1:将覆盖方法中的==更改为.equals()。它没有改变任何东西。 :-(

编辑2:是的,我知道它是否有荆棘或气味是布尔值,但现在不需要它。我可以稍后再做。

我会尝试发布尽可能少的代码以保持简洁。我有一个Driver类,一个Parent类和三个子类。我正在接受用户输入来构建字符串的ArrayList,然后我必须能够显示,删除,过滤和搜索数组。我可以添加元素并显示它们,但是,我已经使用我的remove方法陷入了僵局。即使我尝试使用Override equals方法,用户输入的字符串也不等于ArrayList中的字符串。我将展示Driver类,Parent类和一个子类,因为其他两个子类现在不相关。

注意:我正在为用户提供一个选项,用于选择用户想要添加/删除的工厂,从而选择多种方法。我只是想创造一些额外的东西,我打破了我的代码。

问题是它仍会打印出在库存中找不到的内容,再次显示后,该元素尚未删除。我还没有完成其他方法,因为我坚持删除元素。 任何帮助/建议将不胜感激。我试过找到类似的问题,如果这是一个重复的问题,但我有一个独特的问题,因为我有多个类。我在另一个只有Driver和Flower类的程序中使用了类似的代码。它工作正常。

我还没有正式学习Override方法,所以我自己尝试了无效。

家长班:

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

}


} 

三个子课程中的一个:

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

}

Driver类:

    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() {

}
}

0 个答案:

没有答案