Beginner Java remove object in an ArrayList Java Issue

onesnapthanos :

I am having a hard time figuring out why it is, when I call a specific object from my arrayList, it does not return the output I desire? I am asked create 3 new objects and add them to the arraylist using the add method, call an invalid parameter with get at 4, and a valid one that is at 2, display the details then remove 4 and 2. The output I am getting from my main method is

Invalid index position
2
The current guests in Puss in Boots Cattery:
Garfield
Bob
John
Invalid index position
The current guests in Puss in Boots Cattery:
Garfield
John

I don't understand why I am getting just "2" as my output instead of the cats name "John" at which should be stored in 2?

package pkg4;

import java.util.ArrayList;

public class Cattery {  
    private ArrayList<Cat> catList;
    private String businessName;

    public Cattery(String businessName) {
        catList = new ArrayList<Cat>();
        this.businessName = businessName;
    }

    public void addCat(Cat newCat) {

        catList.add(newCat);
    }

    public void getCat(int index) {
        if ((index >= 0) && (index <= catList.size() - 1)) {
            Cat oneCat = catList.get(index);
            System.out.println(index);
        } else {
            System.out.println("Invalid index position");
        }
    }

    public void removeCat(int indexRemove) {
        if ((indexRemove >= 0) && (indexRemove <= catList.size() - 1)) {
            catList.remove(indexRemove);
        } else {
            System.out.println("Invalid index position");
        }
    }

    public void displayAllCats() {
        System.out.println("The current guests in Puss in Boots Cattery:");
        for (Cat catNames : catList) {
            System.out.println(catNames.getName());
        }
    }

    public static void main(String[] args) {
        Cattery allCats = new Cattery("Puss In Boots Cattery");
        Cat c1 = new Cat("Garfield", 2015, 10);
        Cat c2 = new Cat("Bob", 2020, 5);
        Cat c3 = new Cat("John", 2019, 9);
        allCats.addCat(c1);
        allCats.addCat(c2);
        allCats.addCat(c3);
        allCats.getCat(3);
        allCats.getCat(2);
        allCats.displayAllCats();
        allCats.removeCat(3);
        allCats.removeCat(1);
        allCats.displayAllCats();
    }
}

Class Cat

public class Cat {
    private String name;
    private int yearOfBirth;
    private int weightInKilos;

    public Cat(String inputName, int inputYearOfBirth, int inputWeigthInKilos) {
        setName(inputName);
        setYearOfBirth(inputYearOfBirth);
        setWeigthInKilos(inputWeigthInKilos);
    }

    public String getName() {
        return name;
    }

    public int getYearOfBirth() {
        return yearOfBirth;
    }

    public int getWeightInKilos() {
        return weightInKilos;
    }

    public void setName(String name) {
        if (name != null && !name.isEmpty()) {
            this.name = name;
        } else if (name == null) {
            throw new IllegalArgumentException("name cannot be null");
        } else if (name.isEmpty()) {
            throw new IllegalArgumentException("name cannot be an empty String");
        }
    }

    public void setYearOfBirth(int yearOfBirth) {
        if (yearOfBirth > 0) {
            this.yearOfBirth = yearOfBirth;
        } else {
            throw new IllegalArgumentException("year of birth cannot be negative");
        }
    }

    public void setWeigthInKilos(int weigthInKilos) {
        if (weigthInKilos > 0) {
            this.weightInKilos = weigthInKilos;
        } else {
            throw new IllegalArgumentException(" weight in kilos cannot be negative");
        }
    }
}
am0awad :

If you want to print the cat object, please override toString method in Cat class by the following:

    @Override
    public String toString() {
        return "Cat{" +
                "name='" + name + '\'' +
                ", yearOfBirth=" + yearOfBirth +
                ", weightInKilos=" + weightInKilos +
                '}';
    }

, And update this code to print cat object

public void getCat(int index) {
    if ((index >= 0) && (index < catList.size())) {
        Cat oneCat = catList.get(catList.get(index));                
        System.out.println(oneCat);
    }
    else{
        System.out.println("Invalid index position");
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=344663&siteId=1