Advertisement
hisyam99

JAVA

May 24th, 2023
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 KB | Source Code | 0 0
  1. interface Destroyable {
  2.     void destroyed();
  3. }
  4.  
  5. class Zombie {
  6.     protected int health;
  7.  
  8.     public Zombie(int health) {
  9.         this.health = health;
  10.     }
  11.  
  12.     public void destroyed() {
  13.         System.out.println("====== Zombie Mati ======");
  14.     }
  15.  
  16.     public String getZombieInfo() {
  17.         return "Zombie: " + getClass().getSimpleName() + "\nHealth: " + health;
  18.     }
  19. }
  20.  
  21. class WalkingZombie extends Zombie implements Destroyable {
  22.     public WalkingZombie(int health) {
  23.         super(health);
  24.     }
  25.  
  26.     @Override
  27.     public String getZombieInfo() {
  28.         return super.getZombieInfo();
  29.     }
  30. }
  31.  
  32. class Armor implements Destroyable {
  33.     private int strength;
  34.  
  35.     public Armor(int strength) {
  36.         this.strength = strength;
  37.     }
  38.  
  39.     public void destroyed() {
  40.         System.out.println("Armor hancur!");
  41.     }
  42.  
  43.     public String getArmorInfo() {
  44.         return "Armor Strength: " + strength;
  45.     }
  46.  
  47.     public void reduceStrength(double damage) {
  48.         strength -= damage;
  49.         if (strength <= 0) {
  50.             destroyed();
  51.         }
  52.     }
  53. }
  54.  
  55. class ArmoredZombie extends Zombie implements Destroyable {
  56.     private Armor armor;
  57.  
  58.     public ArmoredZombie(int health, Armor armor) {
  59.         super(health);
  60.         this.armor = armor;
  61.     }
  62.  
  63.     @Override
  64.     public String getZombieInfo() {
  65.         return super.getZombieInfo() + "\n" + armor.getArmorInfo();
  66.     }
  67.  
  68.     public void reduceArmorStrength(double damage) {
  69.         armor.reduceStrength(damage);
  70.     }
  71. }
  72.  
  73. class Plant {
  74.     public void attack(Destroyable destroyable) {
  75.         if (destroyable instanceof WalkingZombie) {
  76.             WalkingZombie walkingZombie = (WalkingZombie) destroyable;
  77.             walkingZombie.health -= 0.4 * walkingZombie.health;
  78.         } else if (destroyable instanceof ArmoredZombie) {
  79.             ArmoredZombie armoredZombie = (ArmoredZombie) destroyable;
  80.             while (armoredZombie.armor.strength > 0) {
  81.                 armoredZombie.reduceArmorStrength(0.2 * armoredZombie.armor.strength);
  82.             }
  83.             armoredZombie.health -= 0.2 * armoredZombie.health;
  84.         }
  85.     }
  86. }
  87.  
  88. public class Main {
  89.     public static void main(String[] args) {
  90.         Plant plant = new Plant();
  91.         ArmoredZombie armoredZombie = new ArmoredZombie(100, new Armor(100));
  92.  
  93.         System.out.println("====== Zombie sebelum diserang ======");
  94.         System.out.println(armoredZombie.getZombieInfo());
  95.  
  96.         System.out.println("====== Plant menyerang ArmoredZombie ======");
  97.         plant.attack(armoredZombie);
  98.  
  99.         System.out.println("====== Zombie Info ======");
  100.         System.out.println(armoredZombie.getZombieInfo());
  101.  
  102.         System.out.println("====== Plant menyerang ArmoredZombie ======");
  103.         plant.attack(armoredZombie);
  104.  
  105.         System.out.println("====== Zombie Info ======");
  106.         System.out.println(armoredZombie.getZombieInfo());
  107.  
  108.         System.out.println("Skip");
  109.         System.out.println("Skip");
  110.  
  111.         System.out.println("====== Plant menyerang ArmoredZombie ======");
  112.         plant.attack(armoredZombie);
  113.  
  114.         System.out.println("====== Zombie Info ======");
  115.         System.out.println(armoredZombie.getZombieInfo());
  116.  
  117.         System.out.println("====== Plant menyerang ArmoredZombie ======");
  118.         plant.attack(armoredZombie);
  119.  
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement