I do not understand why I am failing this task.
I did read in the names of 6 cats in the specified order.
String catGrandFatherName = reader.readLine();
Cat catGrandfather = new Cat(catGrandFatherName);
String catGrandMotherName = reader.readLine();
Cat catGrandmother = new Cat(catGrandMotherName);
String catFatherName = reader.readLine();
Cat catFather = new Cat(catFatherName, catGrandFatherName);
String catMotherName = reader.readLine();
Cat catMother = new Cat(catMotherName, catGrandMotherName);
String CatSonName = reader.readLine();
Cat catSon = new Cat(CatSonName, catFatherName , catMotherName);
String CatDaughterName = reader.readLine();
Cat catDaughter = new Cat(CatDaughterName, catFatherName , catMotherName);
I did program should display 6 lines with information about the cats.
System.out.println(catGrandfather);
System.out.println(catGrandmother);
System.out.println(catFather);
System.out.println(catMother);
System.out.println(catSon);
System.out.println(catDaughter);
all the other condition should have been meet. Can someone give me some insight please?
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 catGrandFatherName = reader.readLine();
Cat catGrandfather = new Cat(catGrandFatherName);
String catGrandMotherName = reader.readLine();
Cat catGrandmother = new Cat(catGrandMotherName);
String catFatherName = reader.readLine();
Cat catFather = new Cat(catFatherName, catGrandFatherName);
String catMotherName = reader.readLine();
Cat catMother = new Cat(catMotherName, catGrandMotherName);
String CatSonName = reader.readLine();
Cat catSon = new Cat(CatSonName, catFatherName , catMotherName);
String CatDaughterName = reader.readLine();
Cat catDaughter = new Cat(CatDaughterName, catFatherName , catMotherName);
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 String father;
private String mother;
Cat(String name) {
this.name = name;
}
Cat(String name,String parent) {
this.name = name;
String parentName = parent;
if(parentName.substring(0,11).equals("Grandmother"))
{
this.mother = parent;
} else if(parentName.substring(0,11).equals("Grandfather"))
{
this.father = parent;
}
if(parentName.substring(0,7).equals("Mother"))
{
this.mother = parent;
} else if(parentName.substring(0,7).equals("Father"))
{
this.father = parent;
}
}
Cat(String name, String father, String mother){
this.name = name;
this.father = father;
this.mother = mother;
}
@Override
public String toString() {
if (father == null & mother == null) {
return "The cat's name is " + name + ", no mother, no father";
} else if(mother == null && father != null) {
return "The cat's name is " + name + ", no mother, " + father + " is the father";
} else if(mother != null && father == null) {
return "The cat's name is " + name + ", " + mother + " is mother," + " not is father";
}else {
return "The cat's name is " + name + ", " + mother + " is mother, " + father + " is father";
}
}
}
}