The two conditions aren't satisfied.
1. The line about the father (third line) must match the conditions.
2. The line about the mother (fourth line) must match the conditions.
Output:
The cat's name is Grandfather Tiger, no mother, no father
The cat's name is Grandmother Puss, no mother, no father
The cat's name is Father Oscar, no mother, Grandfather Tiger is the father
The cat's name is Mother Missy, Grandmother Puss is the mother, no father
The cat's name is Son Simba, Mother Missy is the mother, Father Oscar is the father
The cat's name is Daughter Coco, Mother Missy is the mother, Father Oscar is the father
package com.codegym.task.task06.task0621;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Cat relations
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String grandfather = reader.readLine();
String grandmother = reader.readLine();
String father = reader.readLine();
String mother = reader.readLine();
String son = reader.readLine();
String daughter = reader.readLine();
Cat gf = new Cat(grandfather);
Cat gm = new Cat(grandmother);
Cat f = new Cat(father, gf);
Cat m = new Cat(mother, gm);
Cat s = new Cat(son, m, f);
Cat d = new Cat(daughter, m, f);
System.out.println(gf);
System.out.println(gm);
System.out.println(f);
System.out.println(m);
System.out.println(s);
System.out.println(d);
}
public static class Cat {
private String name;
private Cat parent;
private Cat mother;
private Cat father;
Cat(String name) {
this.name = name;
}
Cat(String name, Cat parent) {
this.name = name;
this.parent = parent;
}
Cat(String name, Cat mother, Cat father) {
this.name = name;
this.mother = mother;
this.father = father;
}
@Override
public String toString() {
if (parent != null) {
String who = parent.name.split(" ")[0].toLowerCase();
if (who.equals("mother") || who.equals("grandmother"))
return "The cat's name is " + name + ", " + parent.name + " is the mother, no father";
else if (who.equals("father") || who.equals("grandfather"))
return "The cat's name is " + name + ", no mother, " + parent.name + " is the father";
} else if (mother != null && father != null)
return "The cat's name is " + name + ", "+mother.name + " is the mother, " + father.name + " is the father";
return "The cat's name is " + name+", no mother, no father";
}
}
}