Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface Destroyable {
- void destroyed();
- }
- class Zombie {
- protected int health;
- public Zombie(int health) {
- this.health = health;
- }
- public void destroyed() {
- System.out.println("====== Zombie Mati ======");
- }
- public String getZombieInfo() {
- return "Zombie: " + getClass().getSimpleName() + "\nHealth: " + health;
- }
- }
- class WalkingZombie extends Zombie implements Destroyable {
- public WalkingZombie(int health) {
- super(health);
- }
- @Override
- public String getZombieInfo() {
- return super.getZombieInfo();
- }
- }
- class Armor implements Destroyable {
- private int strength;
- public Armor(int strength) {
- this.strength = strength;
- }
- public void destroyed() {
- System.out.println("Armor hancur!");
- }
- public String getArmorInfo() {
- return "Armor Strength: " + strength;
- }
- public void reduceStrength(double damage) {
- strength -= damage;
- if (strength <= 0) {
- destroyed();
- }
- }
- }
- class ArmoredZombie extends Zombie implements Destroyable {
- private Armor armor;
- public ArmoredZombie(int health, Armor armor) {
- super(health);
- this.armor = armor;
- }
- @Override
- public String getZombieInfo() {
- return super.getZombieInfo() + "\n" + armor.getArmorInfo();
- }
- public void reduceArmorStrength(double damage) {
- armor.reduceStrength(damage);
- }
- }
- class Plant {
- public void attack(Destroyable destroyable) {
- if (destroyable instanceof WalkingZombie) {
- WalkingZombie walkingZombie = (WalkingZombie) destroyable;
- walkingZombie.health -= 0.4 * walkingZombie.health;
- } else if (destroyable instanceof ArmoredZombie) {
- ArmoredZombie armoredZombie = (ArmoredZombie) destroyable;
- while (armoredZombie.armor.strength > 0) {
- armoredZombie.reduceArmorStrength(0.2 * armoredZombie.armor.strength);
- }
- armoredZombie.health -= 0.2 * armoredZombie.health;
- }
- }
- }
- public class Main {
- public static void main(String[] args) {
- Plant plant = new Plant();
- ArmoredZombie armoredZombie = new ArmoredZombie(100, new Armor(100));
- System.out.println("====== Zombie sebelum diserang ======");
- System.out.println(armoredZombie.getZombieInfo());
- System.out.println("====== Plant menyerang ArmoredZombie ======");
- plant.attack(armoredZombie);
- System.out.println("====== Zombie Info ======");
- System.out.println(armoredZombie.getZombieInfo());
- System.out.println("====== Plant menyerang ArmoredZombie ======");
- plant.attack(armoredZombie);
- System.out.println("====== Zombie Info ======");
- System.out.println(armoredZombie.getZombieInfo());
- System.out.println("Skip");
- System.out.println("Skip");
- System.out.println("====== Plant menyerang ArmoredZombie ======");
- plant.attack(armoredZombie);
- System.out.println("====== Zombie Info ======");
- System.out.println(armoredZombie.getZombieInfo());
- System.out.println("====== Plant menyerang ArmoredZombie ======");
- plant.attack(armoredZombie);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement