diff --git "a/TIL/sample/2026-06-15-\354\272\241\354\212\220\355\231\224-\354\273\254\353\240\211\354\205\230.md" "b/TIL/sample/2026-06-15-\354\272\241\354\212\220\355\231\224-\354\273\254\353\240\211\354\205\230.md" index c668a35..3971f62 100644 --- "a/TIL/sample/2026-06-15-\354\272\241\354\212\220\355\231\224-\354\273\254\353\240\211\354\205\230.md" +++ "b/TIL/sample/2026-06-15-\354\272\241\354\212\220\355\231\224-\354\273\254\353\240\211\354\205\230.md" @@ -45,14 +45,15 @@ - Map: 키(key), 값(value)의 쌍으로 이루어진 요소를 담는 자료구조, 키의 중복은 허용되지 않는다. -- List의 for문은 일반 for 문과 다르다. - List personLists = new ArrayList<>(); - personLists.add(new PersonList("홍길동", 20)); - personLists.add(new PersonList("한석봉", 25)); +- for 문의 다른 형식 - for (PersonList name : personLists) { - System.out.println(name.getName() + "의 나이는 " + name.getAge() + "살"); - } + List personLists = new ArrayList<>(); + personLists.add(new PersonList("홍길동", 20)); + personLists.add(new PersonList("한석봉", 25)); + + for (PersonList name : personLists) { + System.out.println(name.getName() + "의 나이는 " + name.getAge() + "살"); + } ## 기억할 것 @@ -151,7 +152,7 @@ class ClericTest2 { ## 어려웠던 점 -- List에서 for문을 다르게 작성한다는 것이 헷갈림? +- for문의 같으면서 다른 형식의 헷갈림? - List외에도 Set, Map은 언제 어디서 써야 하나? ## 해결 방법 diff --git "a/TIL/sample/2026-06-16-\354\203\201\354\206\215.md" "b/TIL/sample/2026-06-16-\354\203\201\354\206\215.md" new file mode 100644 index 0000000..f7e6ab3 --- /dev/null +++ "b/TIL/sample/2026-06-16-\354\203\201\354\206\215.md" @@ -0,0 +1,92 @@ +# 2026-06-15 캡슐화-컬렉션 + +## 오늘 배운 내용 + +- 상속: 이전에 만든 클래스와 닮았지만, 일부 다른 클래스 를 만들 필요가 있을 경우가 늘어날 것이다. + - extends 로 상속을 쓴다. + - public class SuperHero extends Hero + - SuperHero가 자식 클래스 이름, Hero가 부모 클래스 + - boolean 같은 경우 private로 하고 getter 만들때 is를 붙이는게 관습이다. + - private boolean isFlying; + - public boolean isFlying(){return isFlying;} + - 다중상속은 Java에서는 금지 + + - final을 쓰면 상속을 막을 수가 있다. + - public final class String 하면 상속이 되지 않는다. + + - 상속을 받은 생성자는 부모 생성자부터 호출 후 자식 생성자를 호출한다. + - 부모 클래스에 생성자가 없다면 자식 클래스에서 에러가 뜬다. + - 상속을 받으면 반드시 부모(super) 클래스의 생성자를 반드시 호출해야 된다. + - 해결방법 + public SuperHero(String name, int hp){super(name,hp);} + - 단, 부모 클래스에 생성자가 있다면 에러가 나지는 않는다. + + - 부모 클래스(위로(super))로 갈수록 추상적인 것으로 일반화가 되고, 자식 클래스 일 수록 구체화 된다. + + +- 오버라이드(Override) + - 부모 클래스에 있는 메서드를 자식 클래스에서 재정의 하는 것 + - 상속 받은 메서드를 재정의 + + - 메서드도 부모 클래스에 final 이 있다면 상속을 막을 수 있다. + - public final void slip() + - public void attack() { super.attack(); << 상속 받은 것. + + +- 어노테이션 (주석) + - @Override 같은 것들 + +## 기억할 것 + +- 상속 받은 메서드도 재정의를 할 수 있다. +- 그 외 오늘 배운 내용을 메모 한 것을 기억해야 한다. + +========= + +## 실습 코드 + +```java +package com.survivalcoding; + +public class GreatWizard extends Wizard { + private int maxHp = 300; + + GreatWizard(String name, int hp, int mp) { + super(name, hp, mp); + setMp(150); + } + + @Override + void heal() { + if (getMp() < 5) { + System.out.println("마나가 부족합니다."); + } else { + setMp(getMp() - 5); + setHp(getHp() + 25); + System.out.println("힐을 시전했습니다. " + getName() + " HP : " + getHp()); + } + } + + void superHeal() { + if (getMp() < 50) { + System.out.println("마나가 부족합니다."); + } else { + setMp(getMp() - 50); + setHp(maxHp); + System.out.println("슈퍼 힐을 시전했습니다. " + getName() + " HP : " + getHp()); + } + } +} +``` + +## 어려웠던 점 + +- 어렵다기보다 헷갈린 점인데, 상속 받은 후에 super 클래스에서 private 면 get, set을 써야한다는 점 + +## 해결 방법 + +- 연습 말고는 없음 + +## 내일 더 공부할 것 + +- 부모,자식 클래스를 만들어서 여러 번 해보는 것 diff --git a/game/game.png b/game/game.png new file mode 100644 index 0000000..a58c009 Binary files /dev/null and b/game/game.png differ diff --git a/game/game.puml b/game/game.puml new file mode 100644 index 0000000..dc8c162 --- /dev/null +++ b/game/game.puml @@ -0,0 +1,49 @@ +@startuml +scale 1 + +class GreatWizard extends Wizard { + -int maxHp = 300 + +GreatWizard(String:name, int:hp, int:mp + <>, setMp(150)) + +<>void heal(Hero hero) + +void superHeal(Hero hero) +} + +class Wizard { + -String name + -int hp + -int mp = 100 + -Wand wand + + +Wizard(String:name, int:hp, int:mp, Wand:wand) + +Wizard(String:name, int:hp, int:mp) + +void heal(Hero hero) +} + + +class PoisonSlime extends Slime { + -int poisonCount = 5 + + +PoisonSlime(String:suffix<>) + +<>void attack(Hero hero) +} + +class Slime { + -final String suffix + -int hp = 100 + + +Slime(String:suffix) + +void attack(Hero hero) +} + +class Hero { + -String name; + -int hp; + -int maxHp = 300; + + +Hero(String name, int hp) + +void attack() +} + + +@enduml \ No newline at end of file diff --git a/game/src/main/java/com/survivalcoding/GreatWizard.java b/game/src/main/java/com/survivalcoding/GreatWizard.java new file mode 100644 index 0000000..eb286ad --- /dev/null +++ b/game/src/main/java/com/survivalcoding/GreatWizard.java @@ -0,0 +1,31 @@ +package com.survivalcoding; + +public class GreatWizard extends Wizard { + private int maxHp = 300; + + GreatWizard(String name, int hp, int mp) { + super(name, hp, mp); + setMp(150); + } + + @Override + void heal(Hero hero) { + if (getMp() < 5) { + System.out.println("마나가 부족합니다."); + } else { + setMp(getMp() - 5); + hero.setHp(hero.getHp() + 25); + System.out.println("힐을 시전했습니다. " + hero.getName() + " HP : " + hero.getHp()); + } + } + + void superHeal(Hero hero) { + if (getMp() < 50) { + System.out.println("마나가 부족합니다."); + } else { + setMp(getMp() - 50); + hero.setHp(hero.getMaxHp()); + System.out.println("슈퍼 힐을 시전했습니다. " + hero.getName() + " HP : " + hero.getHp()); + } + } +} diff --git a/game/src/main/java/com/survivalcoding/Hero.java b/game/src/main/java/com/survivalcoding/Hero.java index 539ecc6..ec223b2 100644 --- a/game/src/main/java/com/survivalcoding/Hero.java +++ b/game/src/main/java/com/survivalcoding/Hero.java @@ -1,10 +1,33 @@ package com.survivalcoding; public class Hero { - String name; + private String name; private int hp; + private int maxHp = 300; + + Hero(String name, int hp) { + setName(name); + setHp(hp); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getMaxHp() { + return maxHp; + } + + public void setMaxHp(int maxHp) { + this.maxHp = maxHp; + } public int getHp() { + return hp; } diff --git a/game/src/main/java/com/survivalcoding/PoisonSlime.java b/game/src/main/java/com/survivalcoding/PoisonSlime.java new file mode 100644 index 0000000..b190f04 --- /dev/null +++ b/game/src/main/java/com/survivalcoding/PoisonSlime.java @@ -0,0 +1,29 @@ +package com.survivalcoding; + +public class PoisonSlime extends Slime { + private int poisonCount = 5; + + public PoisonSlime(String suffix) { + super(suffix); + } + + public int getPoisonCount() { + return poisonCount; + } + + @Override + void attack(Hero hero) { + if (poisonCount > 0) { + super.attack(hero); + System.out.println("추가로, 독 포자를 살포했다!"); + int poisonDamage = hero.getHp() / 5; + hero.setHp(hero.getHp() - poisonDamage); + System.out.println(poisonDamage + "포인트 데미지"); + poisonCount--; + } + if (poisonCount <= 0) { + super.attack(hero); + } + } + +} diff --git a/game/src/main/java/com/survivalcoding/Slime.java b/game/src/main/java/com/survivalcoding/Slime.java new file mode 100644 index 0000000..5e7f594 --- /dev/null +++ b/game/src/main/java/com/survivalcoding/Slime.java @@ -0,0 +1,28 @@ +package com.survivalcoding; + +public class Slime { + private final String suffix; + private int hp = 100; + + public Slime(String suffix) { + this.suffix = suffix; + } + + public String getSuffix() { + return suffix; + } + + public int getHp() { + return hp; + } + + public void setHp(int hp) { + this.hp = hp; + } + + void attack(Hero hero) { + System.out.println("슬라임 " + suffix + "이/가 공격했다"); + System.out.println("10의 데미지"); + hero.setHp(hero.getHp() - 10); + } +} diff --git a/game/src/main/java/com/survivalcoding/Wizard.java b/game/src/main/java/com/survivalcoding/Wizard.java index bae80b0..1552629 100644 --- a/game/src/main/java/com/survivalcoding/Wizard.java +++ b/game/src/main/java/com/survivalcoding/Wizard.java @@ -2,7 +2,7 @@ public class Wizard { private int hp; - private int mp; + private int mp = 100; private String name; private Wand wand; @@ -21,7 +21,14 @@ public class Wizard { } } + Wizard(String name, int hp, int mp) { + setName(name); + setHp(hp); + setMp(mp); + } + public Wand getWand() { + return wand; } @@ -69,8 +76,19 @@ public void setName(String name) { } void heal(Hero hero) { - int basePoint = 10; - int recovPoint = (int) (basePoint * this.wand.getPower()); - hero.setHp(hero.getHp() + recovPoint); + if (this.mp < 10) { + System.out.println("마나가 부족합니다."); + } else { + this.mp -= 10; + hero.setHp(hero.getHp() + 20); + System.out.println("힐을 시전했습니다. " + hero.getName() + " HP : " + hero.getHp()); + } } } +// 이전 heal 메서드 +// void heal(Hero hero) { +// int basePoint = 10; +// int recovPoint = (int) (basePoint * this.wand.getPower()); +// hero.setHp(hero.getHp() + recovPoint); +// } + diff --git a/game/src/test/java/com/survivalcoding/GreatWizardTest.java b/game/src/test/java/com/survivalcoding/GreatWizardTest.java new file mode 100644 index 0000000..94d587f --- /dev/null +++ b/game/src/test/java/com/survivalcoding/GreatWizardTest.java @@ -0,0 +1,50 @@ +package com.survivalcoding; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class GreatWizardTest { + private Hero hero; + private Wizard wizard; + private GreatWizard greatWizard; + private int WHP = 100; + private int WMP = 100; + private int GHP = 200; + private int GMP = 150; + + @BeforeEach + void Every() { + hero = new Hero("유해진", WHP); + wizard = new Wizard("마법사", WHP, WMP); + greatWizard = new GreatWizard("위대한마법사", GHP, GMP); + } + + @Test + @DisplayName("Wizard heal에 따른 마나 감소와 Hero 체력 증가") + void wizradHeal() { + wizard.heal(hero); + assertEquals(90, wizard.getMp()); + assertEquals(120, hero.getHp()); + + for (int i = 0; i < 10; i++) { + wizard.heal(hero); + } + assertEquals(0, wizard.getMp()); + assertEquals(300, hero.getHp()); + } + + @Test + @DisplayName("GreatWizard Heal에 따른 마나 감소와 Hero 체력 증가") + void greatWizardHeal() { + greatWizard.heal(hero); + assertEquals(145, greatWizard.getMp()); + assertEquals(125, hero.getHp()); + + greatWizard.superHeal(hero); + assertEquals(95, greatWizard.getMp()); + assertEquals(300, hero.getHp()); + } +} \ No newline at end of file diff --git a/game/src/test/java/com/survivalcoding/PoisonSlimeTest.java b/game/src/test/java/com/survivalcoding/PoisonSlimeTest.java new file mode 100644 index 0000000..7ec0e0a --- /dev/null +++ b/game/src/test/java/com/survivalcoding/PoisonSlimeTest.java @@ -0,0 +1,37 @@ +package com.survivalcoding; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class PoisonSlimeTest { + private Hero hero; + private PoisonSlime poisonSlime; + private int HP = 200; + + @BeforeEach + void Every() { + hero = new Hero("유해진", HP); + poisonSlime = new PoisonSlime("A"); + } + + @Test + @DisplayName("기본 공격 + Poison 1번 공격") + void Attack() { + poisonSlime.attack(hero); + assertEquals(152, hero.getHp()); + } + + + @Test + @DisplayName("poisonCount가 0이 되어야 한다.") + void Count() { + for (int i = 0; i < 5; i++) { + poisonSlime.attack(hero); + } + + assertEquals(0, poisonSlime.getPoisonCount()); + } +} \ No newline at end of file diff --git a/game/src/test/java/com/survivalcoding/WizardTest.java b/game/src/test/java/com/survivalcoding/WizardTest.java index 72dd4af..cd64aaa 100644 --- a/game/src/test/java/com/survivalcoding/WizardTest.java +++ b/game/src/test/java/com/survivalcoding/WizardTest.java @@ -6,6 +6,11 @@ import static org.junit.jupiter.api.Assertions.assertThrows; class WizardTest { + private static final String NULL_NAME = null; + private static final String TWO_NAME = "AB"; + private static final int SETMP = -1; + private static final double SEP_MIN_POW = 0.4; + private static final double SEP_MAX_POW = 100.1; @Test @DisplayName("Wand, Wizard 클래스 테스트") @@ -13,30 +18,31 @@ void Wizard() { Wand wand = new Wand("지팡이", 50); Wizard wizard = new Wizard("아서스", 100, 50, wand); + // Wizard 이름 mp 테스트 assertThrows(IllegalArgumentException.class, () -> { - wizard.setName(null); + wizard.setName(NULL_NAME); }); assertThrows(IllegalArgumentException.class, () -> { - wizard.setName("ab"); + wizard.setName(TWO_NAME); }); assertThrows(IllegalArgumentException.class, () -> { - wizard.setMp(-1); + wizard.setMp(SETMP); }); // Wand 이름, 마력 테스트 assertThrows(IllegalArgumentException.class, () -> { - wand.setName(null); + wand.setName(NULL_NAME); }); assertThrows(IllegalArgumentException.class, () -> { - wand.setName("ab"); + wand.setName(TWO_NAME); }); assertThrows(IllegalArgumentException.class, () -> { - wand.setPower(0.4); + wand.setPower(SEP_MIN_POW); }); assertThrows(IllegalArgumentException.class, () -> { - wand.setPower(100.1); + wand.setPower(SEP_MAX_POW); }); } } \ No newline at end of file