В выводе все ок, но четвертая строка выдается с ошибкой - как будто бы программа третье условие for просто игнорирует. Не понимаю, как это можно поправить, помогите (
package com.javarush.task.task06.task0621;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Родственные связи кошек
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String grandpaName = reader.readLine();
Cat catGrandpa = new Cat(grandpaName);
String grandmaName = reader.readLine();
Cat catGrandma = new Cat(grandmaName);
String dadName = reader.readLine();
Cat catDad = new Cat(dadName, catGrandpa);
String motherName = reader.readLine();
Cat catMother = new Cat(motherName, catGrandma);
String sonName = reader.readLine();
Cat catSon = new Cat(sonName, catDad, catMother);
String daughterName = reader.readLine();
Cat catDaughter = new Cat(daughterName, catDad, catMother);
System.out.println(catGrandpa);
System.out.println(catGrandma);
System.out.println(catDad);
System.out.println(catMother);
System.out.println(catSon);
System.out.println(catDaughter);
}
public static class Cat {
private String name;
private Cat dad;
private Cat mother;
Cat(String name) {
this.name = name;
}
Cat(String name, Cat dad) {
this.name = name;
this.dad = dad;
}
Cat(String name, Cat dad, Cat mother) {
this.name = name;
this.dad = dad;
this.mother = mother;
}
@Override
public String toString() {
if (mother == null && dad == null )
return "The cat's name is " + name + ", no mother, no father ";
if (mother == null)
return "The cat's name is " + name + ", no mother, father is " + dad.name;
if (dad == null)
return "The cat's name is " + name + ", mother is " + mother.name + ", no father";
else
return "The cat's name is " + name + ", mother is " + mother.name + ", father is " + dad.name;
}
}
}