Example 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
My 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
Also bonus points for helping me understand why my while loop doesn't satisfy the first condition.
Thanks!package com.codegym.task.task06.task0621;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/*
Cat relations
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
ArrayList<Cat> list = new ArrayList<>();
/*while (true){
String name = reader.readLine();
if (name == null || name.equals("") || name.isEmpty()){
break;
}*/
for (int i = 0; i < 6; i++){
String name = reader.readLine();
if (Cat.catNum == 0){
Cat grandFather = new Cat(name);
list.add(grandFather);
}
else if (Cat.catNum == 1){
Cat grandMother = new Cat(name);
list.add(grandMother);
}
else if (Cat.catNum == 2){
Cat father = new Cat(name, null, list.get(0));
list.add(father);
}
else if (Cat.catNum == 3){
Cat mother = new Cat(name, list.get(1), null);
list.add(mother);
}
else if (Cat.catNum == 4){
Cat son = new Cat (name, list.get(3), list.get(2));
list.add(son);
}
else if (Cat.catNum == 5){
Cat daughter = new Cat(name, list.get(3), list.get(2));
list.add(daughter);
}
}
for (Cat str : list){
System.out.println(str);
}
}
public static class Cat {
private String name;
private Cat mother;
private Cat father;
public static int catNum;
private Cat parent;
Cat(String name) {
this.name = name;
catNum++;
}
Cat(String name, Cat mother, Cat father){
this.name = name;
this.mother = mother;
this.father = father;
catNum++;
}
@Override
public String toString() {
if (mother == null && father == 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 + ", " + mother.name + " is the mother, no father";
else if (father != null && mother == null)
return "The cat's name is " + name + ", no mother, " + father.name + " is the father";
else
return "The cat's name is " + name + ", " + mother.name + " is the mother, " + father.name + " is the father";
}
}
}