diff --git a/.gitignore b/.gitignore index cb0e6a9..fac7295 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ bin/* +src/*.class *.class \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index 18d341e..b0b8556 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -6,6 +6,29 @@ "configurations": [ { "type": "java", + "name": "Launch UrashimaTarou", + "request": "launch", + "mainClass": "UrashimaTarou", + "projectName": "JavaDesignPattern_3cd6a2ec" + }, + { + "type": "java", + "name": "Launch MyApp", + "request": "launch", + "mainClass": "MyApp", + "projectName": "JavaDesignPattern_3cd6a2ec", + "noDebug": false, + "__progressId": "d116ee82-b920-44ae-930c-76cce5bca10f" + }, + { + "type": "java", + "name": "Launch Urashima", + "request": "launch", + "mainClass": "Urashima", + "projectName": "JavaDesignPattern_3cd6a2ec" + }, + { + "type": "java", "name": "Launch Current File", "request": "launch", "mainClass": "${file}" @@ -18,4 +41,4 @@ "projectName": "JavaDesignPattern_7f164bb0" } ] -} \ No newline at end of file +} diff --git a/bin/Urashima.class b/bin/Urashima.class index 6a5a076..1776cab 100644 --- a/bin/Urashima.class +++ b/bin/Urashima.class Binary files differ diff --git a/src/Actor.java b/src/Actor.java index bfc5ee1..ad5b1ad 100644 --- a/src/Actor.java +++ b/src/Actor.java @@ -1,25 +1,79 @@ public class Actor { String name = ""; - void showName() { + public void showName() { System.out.println(name); } void getItem(Item item) { - System.out.println(name + " は " + item.to_string() + " を手に入れた。"); + System.out.println(name + " は " + item.toString() + " を受け取った。"); } - void giveItem(Item item, Actor name) { - System.out.println(this.name + " は " + item.to_string() + " を " + name.to_string() + " に渡した。"); - } - - String to_string(){ + @Override + public String toString() { return name; } + + boolean isOld = false; // 年をとるとtrueに設定 + + void checkOld() { + if (this.isOld) { + System.out.println(name + " は老人になってしまった。"); + } + } } class Urashima extends Actor { - Urashima(){ + Tamatebako tamatebako; + + Urashima() { name = "浦島太郎"; } + + void rescueSomeone(Actor someone) { + System.out.println(this.name + " は " + someone.toString() + " を助けた。"); + + } + + void getItem(Item item) { + super.getItem(item); + } + + void openBox(Item item) { + System.out.println(this.name + " は " + item.toString() + " を開けた。"); + this.isOld = true; + } +} + +class Kame extends Actor { + Kame() { + name = "亀"; + } + + void gotoRyuuguujou(Actor someone) { + System.out.println(this.name + " は "+ someone.toString() +" を竜宮城に連れて行った。"); + } +} + +class Childlen extends Actor { + Childlen() { + name = "子供たち"; + } + + void hit(Actor someone) { + System.out.println(this.name + " は " + someone.toString() + " をいじめている。"); + } +} + +class Otohime extends Actor { + Tamatebako tamatebako; + + Otohime() { + name = "乙姫"; + } + + void omotenashi(Actor someone) { + System.out.println(this.name + " は " + someone.toString() + " をもてなした。"); + + } } \ No newline at end of file diff --git a/src/App.class b/src/App.class deleted file mode 100644 index f2000fe..0000000 --- a/src/App.class +++ /dev/null Binary files differ diff --git a/src/Item.java b/src/Item.java index 0720cf1..d761337 100644 --- a/src/Item.java +++ b/src/Item.java @@ -2,13 +2,11 @@ String name = ""; public Item(String string) { + this.name = string; } - void setMono(String name) { - this.name = name; - } - - public String to_string() { + @Override + public String toString() { return name; } diff --git a/src/Urashima.java b/src/Urashima.java deleted file mode 100644 index a6d338d..0000000 --- a/src/Urashima.java +++ /dev/null @@ -1,10 +0,0 @@ -public class Urashima { - public static void main(String[] args) throws Exception { - System.out.println("Hello, World!"); - - // - Urashima urashima = new Urashima(); - Tamatebako tamatebako = new Tamatebako(); - urashima.getItem(tamatebako); - } -} diff --git a/src/Urashima.pu b/src/Urashima.pu new file mode 100644 index 0000000..1fe3f64 --- /dev/null +++ b/src/Urashima.pu @@ -0,0 +1,59 @@ +@startuml name 浦島太郎 + +class UrashimaTarou [[java:UrashimaTarou]] { + +{static} main(args: String[]): void +} + +class Item [[java:Item]] { + ~name: String + +Item(string: String) + +toString(): String +} +class Tamatebako [[java:Tamatebako]] { + ~Tamatebako() +} +class Item [[java:Item]] { +} +Item <|-- Tamatebako + +class Actor [[java:Actor]] { + ~name: String + ~isOld: boolean + +showName(): void + ~getItem(item: Item): void + +toString(): String + ~checkOld(): void +} +class Urashima [[java:Urashima]] { + ~tamatebako: Tamatebako + ~Urashima() + ~rescueSomeone(someone: Actor): void + ~getItem(item: Item): void + ~openBox(item: Item): void +} +class Actor [[java:Actor]] { +} +Actor <|-- Urashima +class Kame [[java:Kame]] { + ~Kame() + ~gotoRyuuguujou(someone: Actor): void +} +class Actor [[java:Actor]] { +} +Actor <|-- Kame +class Childlen [[java:Childlen]] { + ~Childlen() + ~hit(someone: Actor): void +} +class Actor [[java:Actor]] { +} +Actor <|-- Childlen +class Otohime [[java:Otohime]] { + ~tamatebako: Tamatebako + ~Otohime() + ~omotenashi(someone: Actor): void +} +class Actor [[java:Actor]] { +} +Actor <|-- Otohime +@enduml \ No newline at end of file diff --git a/src/UrashimaTarou.java b/src/UrashimaTarou.java new file mode 100644 index 0000000..880fc8d --- /dev/null +++ b/src/UrashimaTarou.java @@ -0,0 +1,31 @@ +public class UrashimaTarou { + public static void main(String[] args){ + System.out.println("浦島太郎\n"); + + // むかしむかし、あるところに浦島太郎がいました。 + Urashima urashima = new Urashima(); + + // 浦島太郎が浜に行くと、亀が子供たちにいじめられていました。 + Childlen children = new Childlen(); + Kame kame = new Kame(); + children.hit(kame); + + // 浦島太郎がカメを助けると、お礼に竜宮城に連れて行ってもらいました。 + urashima.rescueSomeone(kame); + kame.gotoRyuuguujou(urashima); + + // 乙姫はカメを助けた浦島太郎を歓迎した。 + Otohime otohime = new Otohime(); + otohime.omotenashi(urashima); + + // 浦島太郎は帰り際に乙姫から玉手箱を受け取る。 + Tamatebako tamatebako = new Tamatebako(); + urashima.getItem(tamatebako); + + // 地上に戻った浦島太郎は玉手箱を開けてしまい、老人になってしまう。 + urashima.openBox(tamatebako); + + urashima.checkOld(); + return; + } +}