Мне трудно понять, почему, когда я вызываю конкретный c объект из моего arrayList, он не возвращает желаемый результат? Меня просят создать 3 новых объекта и добавить их в массив, используя метод add, вызвать недопустимый параметр с get на 4 и действительный, равный 2, отобразить детали, затем удалить 4 и 2. Вывод, который я получаю мой основной метод -
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
Я не понимаю, почему я получаю только «2» в качестве результата вместо имени кота «Джон», в котором должно храниться значение 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();
}
}
Класс 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");
}
}
}