Advertisement
jaVer404

level13.lesson11.bonus03

Jun 1st, 2015
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.44 KB | None | 0 0
  1. package com.javarush.test.level13.lesson11.bonus03;
  2.  
  3. public interface Attackable
  4. {
  5.     BodyPart attack();
  6. }
  7. /************************************************/
  8. package com.javarush.test.level13.lesson11.bonus03;
  9.  
  10. public abstract class AbstractRobot
  11. {
  12.     private static int hitCount;
  13.     private String name;
  14.     public BodyPart attack()
  15.     {
  16.         BodyPart attackedBodyPart = null;
  17.         hitCount = hitCount + 1;
  18.  
  19.         if (hitCount == 1)
  20.         {
  21.             attackedBodyPart =  BodyPart.ARM;
  22.         } else if (hitCount == 2)
  23.         {
  24.             attackedBodyPart =  BodyPart.HEAD;
  25.         } else if (hitCount == 3)
  26.         {
  27.             attackedBodyPart =  BodyPart.LEG;
  28.         }else if (hitCount == 4) {
  29.             hitCount = 0;
  30.             attackedBodyPart = BodyPart.CHEST;
  31.         }
  32.         return attackedBodyPart;
  33.     }
  34.     public BodyPart defense()
  35.     {
  36.         BodyPart defencedBodyPart = null;
  37.         hitCount = hitCount + 1;
  38.  
  39.         if (hitCount == 1)
  40.         {
  41.             defencedBodyPart =  BodyPart.HEAD;
  42.         } else if (hitCount == 2)
  43.         {
  44.             defencedBodyPart =  BodyPart.LEG;
  45.         } else if (hitCount == 3)
  46.         {
  47.             hitCount = 0;
  48.             defencedBodyPart =  BodyPart.ARM;
  49.         }
  50.         return defencedBodyPart;
  51.     }
  52.     public String getName()
  53.     {
  54.         return name;
  55.     }
  56. }
  57. /************************************************/
  58. package com.javarush.test.level13.lesson11.bonus03;
  59.  
  60. /* Битва роботов
  61. 1 Разобраться в том, что тут написано.
  62. 2 http://info.javarush.ru/uploads/images/00/00/07/2014/08/12/50f3e37f94.png
  63. 3 Смириться со своей участью и продолжить разбираться в коде.
  64. 4 ...
  65. 5 Порадоваться, что мы все поняли.
  66.  
  67. 6 Изменить код согласно новой архитектуре и добавить новую логику:
  68. 6.1 Сделать класс AbstractRobot абстрактным, вынести логику атаки и защиты из Robot в AbstractRobot.
  69. 6.2 Отредактировать класс Robot учитывая AbstractRobot.
  70. 6.3 Расширить класс BodyPart новой частью тела "грудь".
  71. 6.4 Добавить новую часть тела в реализацию интерфейсов Attackable и Defensable (в классе AbstractRobot).
  72.  
  73. 7 http://info.javarush.ru/uploads/images/00/00/07/2014/08/12/3b9c65580b.png
  74. */
  75.  
  76. public class Solution
  77. {
  78.     public static void main(String[] args)
  79.     {
  80.         Robot amigo = new Robot("Амиго");
  81.         Robot enemy = new Robot("Сгибальщик-02");
  82.  
  83.         doMove(amigo, enemy);
  84.         doMove(amigo, enemy);
  85.         doMove(enemy, amigo);
  86.         doMove(amigo, enemy);
  87.         doMove(enemy, amigo);
  88.         doMove(amigo, enemy);
  89.         doMove(enemy, amigo);
  90.         doMove(amigo, enemy);
  91.     }
  92.  
  93.     public static void doMove(AbstractRobot robotFirst, AbstractRobot robotSecond) {
  94.         BodyPart attacked = robotFirst.attack();
  95.         BodyPart defenced = robotFirst.defense();
  96.         System.out.println(String.format("%s атаковал робота %s, атакована %s, защищена %s",
  97.                 robotFirst.getName(), robotSecond.getName(), attacked, defenced));
  98.     }
  99. }
  100. /************************************************/
  101. package com.javarush.test.level13.lesson11.bonus03;
  102.  
  103. public class Robot extends AbstractRobot implements Attackable, Defensable
  104. {
  105.  
  106.     private String name;
  107.  
  108.     public Robot(String name)
  109.     {
  110.         this.name = name;
  111.     }
  112.  
  113.     public String getName()
  114.     {
  115.         return name;
  116.     }
  117. }
  118. /************************************************/
  119. package com.javarush.test.level13.lesson11.bonus03;
  120.  
  121. public final class BodyPart
  122. {
  123.     final static BodyPart LEG = new BodyPart("нога");
  124.     final static BodyPart HEAD = new BodyPart("голова");
  125.     final static BodyPart ARM = new BodyPart("рука");
  126.     final static BodyPart CHEST = new BodyPart("грудь");
  127.  
  128.     private String bodyPart;
  129.  
  130.     private BodyPart(String bodyPart)
  131.     {
  132.         this.bodyPart = bodyPart;
  133.     }
  134.  
  135.     @Override
  136.     public String toString()
  137.     {
  138.         return this.bodyPart;
  139.     }
  140. }
  141. /************************************************/
  142. package com.javarush.test.level13.lesson11.bonus03;
  143.  
  144. public interface Defensable
  145. {
  146.     BodyPart defense();
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement