В чем ошибка?
Максим
Фрося
Вася
Мария
Петька
Машка
Выводит:
Cat name is Максим, no mother, no father
Cat name is Фрося, no mother, no father
Cat name is Вася, mother is Максим no father
Cat name is Мария, mother is Максим no father
Cat name is Петька, mother is Мария, father is Вася
Cat name is Машка, mother is Мария, father is Вася
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 grandFatherName = reader.readLine();
Cat grandFather = new Cat(grandFatherName);
String grandMotherName = reader.readLine();
Cat grandMother = new Cat(grandMotherName);
String fatherName = reader.readLine();
Cat catFather = new Cat(fatherName, grandFather);
String motherName = reader.readLine();
Cat catMother = new Cat(motherName, grandFather);
String sonName = reader.readLine();
Cat catSon = new Cat(sonName, catMother, catFather);
String daughterName = reader.readLine();
Cat catDaughter = new Cat(daughterName, catMother, catFather);
System.out.println(grandFather);
System.out.println(grandMother);
System.out.println(catFather);
System.out.println(catMother);
System.out.println(catSon);
System.out.println(catDaughter);
}
public static class Cat {
private String name;
private Cat parent;
private Cat parent2;
Cat(String name) {
this.name = name;
}
Cat(String name, Cat parent) {
this.name = name;
this.parent = parent;
}
public Cat(String name, Cat parent, Cat parent2) {
this.name = name;
this.parent = parent;
this.parent2 = parent2;
}
@Override
public String toString() {
if (parent == null & parent2 == null)
return "Cat name is " + name + ", no mother, no father ";
else if (parent2 == null)
return "Cat name is " + name + ", mother is " + parent.name + " no father";
else if (parent == null )
return "Cat name is " + name + ", father is " + parent2.name + " no mother";
else
return "Cat name is " + name + ", mother is " + parent.name + ", father is " + parent2.name ;
}
}
}