lundi 4 septembre 2017

If statements running simultaneously, if two objects in ArrayList

Below is the code. I cannot figure out why in class Database my else if state runs simultaneously with my if statement, IF I have entered a second bird into the ArrayList. please any help would be appreciated!

public class Main {

public static void main(String[] args) {


    Scanner input = new Scanner(System.in);
    Database d1 = new Database();




    while (true) {
        System.out.println("What do you want to do?");
        String answer = input.nextLine();

        if(answer.equalsIgnoreCase("Add")){
            System.out.println("Name: ");
            String name = input.nextLine();
            System.out.println("Latin name: ");
            String lName = input.nextLine();

            d1.addBird(name, lName);
        } else if (answer.equalsIgnoreCase("O")) {
            System.out.println("What was observed?");
            String observed = input.nextLine();
            d1.Observation(observed);
        } else if (answer.equalsIgnoreCase("stats")) {
            d1.showBirds(); //Displays all with observations.
        } else if (answer.equalsIgnoreCase("show")) {
            System.out.println("What?");
            String search = input.nextLine();
            d1.searchBird(search);
        } else if (answer.equalsIgnoreCase("quit")){
            break;
        }



    }

}

}

public class Bird {

private final String name;
private final String latinName;
private int count;


public Bird (String name, String latinName) {
    this.name = name;
    this.latinName = latinName;
    this.count = count;
}

public String getName () {
    return this.name;
}

public String getLatin() {
    return this.latinName;
}

public String add () {
    return this.name + " " + "(" +this.latinName + ")"+ " " + this.count + " observation(s)";
}

public void increaseCount () {
    this.count++;
}

}

import java.util.ArrayList;

public class Database {

private final ArrayList<Bird> birdList;


public Database() {
    this.birdList = new ArrayList<Bird>();
}

public void addBird (String name, String lname) {
    this.birdList.add(new Bird(name, lname));

}

public void Observation (String observed) {
    for (Bird x : getBirds()) { // this has to be a method
        if (x.getName() != null && x.getLatin() != null && x.getName().contains(observed) || x.getLatin().contains(observed)) {
            System.out.println("Done");
            System.out.println("");
            x.increaseCount();      
        } else if (x.getName() != observed || x.getLatin() != observed) {
            System.out.println("Not a bird");
        }

    }

}

public void showBirds () {
    for (Bird x : this.birdList) {
        System.out.println(x.add());
    }
}

public ArrayList<Bird> getBirds() {
    return this.birdList;
}

public void searchBird(String search) {
    for (Bird x : getBirds()) {
        if (x.getName().contains(search)) {
            System.out.println(x.add());
        }
    }
}

}

Aucun commentaire:

Enregistrer un commentaire