Advertisement
jaVer404

level06.lesson11.bonus02_NO

Apr 7th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.95 KB | None | 0 0
  1. package com.javarush.test.level06.lesson11.bonus02;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. /* Нужно добавить в программу новую функциональность
  8. Задача:
  9. У каждой кошки есть имя и кошка-мама. Создать класс, который бы описывал данную ситуацию.
  10. Создать два объекта: кошку-дочь и кошку-маму. Вывести их на экран.
  11.  
  12. Новая задача: У каждой кошки есть имя, кошка-папа и кошка-мама.
  13. Изменить класс Cat так, чтобы он мог описать данную ситуацию.
  14. Создать 6 объектов: маму, папу, сына, дочь, бабушку(мамина мама) и дедушку(папин папа).
  15. Вывести их всех на экран в порядке: дедушка, бабушка, папа, мама, сын, дочь.
  16.  
  17. Пример ввода:
  18. дедушка Вася
  19. бабушка Мурка
  20. папа Котофей
  21. мама Василиса
  22. сын Мурчик
  23. дочь Пушинка
  24.  
  25. Пример вывода:
  26. Cat name is дедушка Вася, no mother, no father
  27. Cat name is бабушка Мурка, no mother, no father
  28. Cat name is папа Котофей, no mother, father is дедушка Вася
  29. Cat name is мама Василиса, mother is бабушка Мурка, no father
  30. Cat name is сын Мурчик, mother is мама Василиса, father is папа Котофей
  31. Cat name is дочь Пушинка, mother is мама Василиса, father is папа Котофей
  32. */
  33.  
  34. public class Solution
  35. {
  36.     public static void main(String[] args) throws IOException
  37.     {
  38.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  39.  
  40.         String grandFatherName = reader.readLine();
  41.         Cat catgrandFather = new Cat(grandFatherName, null, null);
  42.  
  43.         String grandMotherName = reader.readLine();
  44.         Cat catgrandMother = new Cat(grandMotherName, null, null);
  45.  
  46.         String fatherName = reader.readLine();
  47.         Cat justFather = new Cat(fatherName, null, catgrandFather);
  48.  
  49.         String motherName = reader.readLine();
  50.         Cat justMother = new Cat(motherName, catgrandMother, null);
  51.  
  52.         String sonName = reader.readLine();
  53.         Cat son = new Cat(sonName, justMother, justFather);
  54.  
  55.         String doughterName = reader.readLine();
  56.         Cat doughter = new Cat(doughterName, justMother, justFather);
  57.  
  58.         System.out.println(catgrandFather);
  59.         System.out.println(catgrandMother);
  60.         System.out.println(justFather);
  61.         System.out.println(justMother);
  62.         System.out.println(son);
  63.         System.out.println(doughter);
  64.     }
  65.  
  66.     public static class Cat
  67.     {
  68.         private String name;
  69.         private Cat parentFather;
  70.         private Cat parentMother;
  71.  
  72.  
  73.         Cat(String name, Cat parentMother, Cat parentFather)
  74.         {
  75.             this.name = name;
  76.             this.parentMother = parentMother;
  77.             this.parentFather = parentFather;
  78.         }
  79.  
  80.         @Override
  81.         public String toString()
  82.         {
  83.             if (parentFather == null&&parentMother==null)
  84.                 return "Cat name is " + name + ", no mother, " + "no father";
  85.  
  86.             else if (parentMother==null&&parentFather==this.parentFather) {
  87.                 return "Cat name is " + name + ", no mother, " +"father is " + parentFather.name;
  88.             }
  89.  
  90.             else if (parentMother==this.parentMother&&parentFather==null) {
  91.                 return "Cat name is " + name + ", mother is " + parentMother.name + ", no father";
  92.             }
  93.  
  94.  
  95.             else
  96.                 return "Cat name is " + name + ", mother is " + parentMother.name +", father is " + parentFather.name;
  97.         }
  98.     }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement