Я посмотрел в других ветках, люди решили её намного проще, используя меньше конструкторов. Так почему мы здесь можем использовать меньше конструкторов?
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();
String grandMotherName = reader.readLine();
String fatherName = reader.readLine();
String motherName = reader.readLine();
String sonName = reader.readLine();
String daughterName = reader.readLine();
Cat catGrandFather = new Cat(grandFatherName);
Cat catGrandMother = new Cat(grandMotherName, catGrandFather);
Cat catFather = new Cat(fatherName, catGrandFather, catGrandMother);
Cat catMother = new Cat(motherName,catGrandFather, catGrandMother, catFather);
Cat catSon = new Cat(sonName,catGrandFather, catGrandMother, catFather, catMother);
Cat catDaughter = new Cat(daughterName,catGrandFather, catGrandMother, catFather, catMother, catSon);
System.out.println(catGrandFather);
System.out.println(catGrandMother);
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 grandMother;
private Cat grandFather;
private Cat father;
private Cat mother;
private Cat son;
private Cat daughter;
Cat(String name) {
this.name = name;
}
Cat(String name, Cat grandFather) {
this.name = name;
this.grandFather = grandFather;
}
Cat(String name, Cat grandFather, Cat grandMother) {
this.name = name;
this.grandFather = grandFather;
this.grandMother = grandMother;
}
Cat(String name, Cat grandFather, Cat grandMother, Cat father) {
this.name = name;
this.grandFather = grandFather;
this.grandMother = grandMother;
this.father = father;
}
Cat(String name, Cat grandFather, Cat grandMother, Cat father, Cat mother) {
this.name = name;
this.grandFather = grandFather;
this.grandMother = grandMother;
this.father = father;
this.mother = mother;
}
Cat(String name,Cat grandFather, Cat grandMother, Cat father, Cat mother, Cat son) {
this.name = name;
this.grandFather = grandFather;
this.grandMother = grandMother;
this.father = father;
this.mother = mother;
this.son = son;
}
Cat(String name,Cat grandFather, Cat grandMother, Cat father, Cat mother, Cat son, Cat daughter) {
this.name = name;
this.grandFather = grandFather;
this.grandMother = grandMother;
this.father = father;
this.mother = mother;
this.son = son;
this.daughter = daughter;
}
@Override
public String toString() {
if (mother!=null)
return "The cat's name is " + name + ", mother is " +mother.name+ " father is "+father.name;
else if (grandMother!=null&&father!=null)
return "The cat's name is " + name + ", mother is " +grandMother.name+ ", no father";
else if (grandMother!=null)
return "The cat's name is " + name + ", no mother, father is " +grandFather.name;
else
return "The cat's name is " + name + ", no mother, no father ";
/*
else if (grandMother != null&&grandFather != null&& son==null && daughter==null)
return "The cat's name is " + name + ", mother is " +grandMother.name+ " father is "+grandFather.name;
else if (mother != null&&father != null)
return "The cat's name is " + name + ", mother is " +mother.name+ " father is "+father.name;
*/
/*
if (grandMother == null)
return "The cat's name is " + name + ", no mother ";
else
return "The cat's name is " + name + ", mother is " + grandMother.name;
*/
}
}
}