From 48ad83ea5acc907b092ac94d155e361332bf8db2 Mon Sep 17 00:00:00 2001 From: Alexander-D-Karpov Date: Mon, 27 Nov 2023 16:01:05 +0300 Subject: [PATCH] Initial commit --- .gitignore | 1 + lab-3.iml | 11 ++++++++++ src/StoryTeller.java | 21 +++++++++++++++++++ src/characters/Character.java | 28 +++++++++++++++++++++++++ src/characters/Child.java | 33 ++++++++++++++++++++++++++++++ src/characters/Traveler.java | 31 ++++++++++++++++++++++++++++ src/characters/Znayka.java | 24 ++++++++++++++++++++++ src/enums/CharacterType.java | 5 +++++ src/interfaces/Interactable.java | 7 +++++++ src/interfaces/Listenable.java | 7 +++++++ src/interfaces/Listener.java | 5 +++++ src/model/City.java | 8 ++++++++ src/model/House.java | 28 +++++++++++++++++++++++++ src/model/StoryContext.java | 13 ++++++++++++ src/model/StoryElement.java | 35 ++++++++++++++++++++++++++++++++ src/story/ListeningSession.java | 25 +++++++++++++++++++++++ src/story/Narrative.java | 21 +++++++++++++++++++ src/story/StoryEvent.java | 13 ++++++++++++ 18 files changed, 316 insertions(+) create mode 100644 lab-3.iml create mode 100644 src/StoryTeller.java create mode 100644 src/characters/Character.java create mode 100644 src/characters/Child.java create mode 100644 src/characters/Traveler.java create mode 100644 src/characters/Znayka.java create mode 100644 src/enums/CharacterType.java create mode 100644 src/interfaces/Interactable.java create mode 100644 src/interfaces/Listenable.java create mode 100644 src/interfaces/Listener.java create mode 100644 src/model/City.java create mode 100644 src/model/House.java create mode 100644 src/model/StoryContext.java create mode 100644 src/model/StoryElement.java create mode 100644 src/story/ListeningSession.java create mode 100644 src/story/Narrative.java create mode 100644 src/story/StoryEvent.java diff --git a/.gitignore b/.gitignore index 9154f4c..7941b1d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea # ---> Java # Compiled class file *.class diff --git a/lab-3.iml b/lab-3.iml new file mode 100644 index 0000000..c90834f --- /dev/null +++ b/lab-3.iml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/StoryTeller.java b/src/StoryTeller.java new file mode 100644 index 0000000..fed9bb5 --- /dev/null +++ b/src/StoryTeller.java @@ -0,0 +1,21 @@ +import characters.Character; +import model.*; +import characters.*; + +public class StoryTeller { + public static void main(String[] args) { + City flowerCity = new City("Цветочный город"); + House znaykasHouse = new House("Домик Знайки"); + StoryContext context = new StoryContext(flowerCity, znaykasHouse); + + Character znayka = new Znayka("описание"); + Character malish = new Child("Малыш", "Малыш"); + Character malishka = new Child("Малышка","Малышка"); + + znaykasHouse.addResident(znayka); + znaykasHouse.addResident(malish); + znaykasHouse.addResident(malishka); + + znaykasHouse.tellEveningStories(context); + } +} diff --git a/src/characters/Character.java b/src/characters/Character.java new file mode 100644 index 0000000..a3e66f1 --- /dev/null +++ b/src/characters/Character.java @@ -0,0 +1,28 @@ +package characters; + +import enums.CharacterType; +import interfaces.Interactable; +import interfaces.Listenable; +import model.StoryContext; +import model.StoryElement; + +public abstract class Character extends StoryElement implements Interactable, Listenable { + protected CharacterType type; + protected String name; + + public Character(String description, CharacterType type, String name) { + super(description); + this.type = type; + this.name = name; + } + + public abstract void listen(); + + public abstract void tell(); + + @Override + public abstract void interact(StoryContext context); + + @Override + public abstract void listen(StoryContext context); +} \ No newline at end of file diff --git a/src/characters/Child.java b/src/characters/Child.java new file mode 100644 index 0000000..51b48f9 --- /dev/null +++ b/src/characters/Child.java @@ -0,0 +1,33 @@ +package characters; + +import enums.CharacterType; +import model.StoryContext; + +public class Child extends Character { + + public Child(String description, String name) { + super(description, CharacterType.CHILD, name); + } + + @Override + public void listen() { + System.out.println(name + " listens to the story with wide-eyed wonder."); + } + + @Override + public void tell() { + + } + + // If Children have other specific interactions, you could override the interact method + @Override + public void interact(StoryContext context) { + // Child-specific interactions + System.out.println(name + " shares their impressions about the story."); + } + + @Override + public void listen(StoryContext context) { + + } +} \ No newline at end of file diff --git a/src/characters/Traveler.java b/src/characters/Traveler.java new file mode 100644 index 0000000..f97bbbf --- /dev/null +++ b/src/characters/Traveler.java @@ -0,0 +1,31 @@ +package characters; + +import enums.CharacterType; +import model.StoryContext; + +public class Traveler extends Character { + + public Traveler(String description, String name) { + super(description, CharacterType.TRAVELER, name); + } + + @Override + public void listen() { + + } + + @Override + public void tell() { + // Logic for telling a story + } + + @Override + public void interact(StoryContext context) { + System.out.println(name + " рассказывает историю о Зеленом городе."); + } + + @Override + public void listen(StoryContext context) { + // Listening logic for a traveler + } +} diff --git a/src/characters/Znayka.java b/src/characters/Znayka.java new file mode 100644 index 0000000..e4fe4d3 --- /dev/null +++ b/src/characters/Znayka.java @@ -0,0 +1,24 @@ +package characters; + +import enums.CharacterType; +import interfaces.Listener; +import model.StoryContext; + +public class Znayka extends Traveler implements Listener { + private String name; + + public Znayka(String description) { + super(description, "Знайка"); + this.name = name; + } + + @Override + public void tell() { + System.out.println(name + " рассказывает истории о жизни в Зеленом городе."); + } + + @Override + public void listen() { + System.out.println(name + " слушает."); + } +} diff --git a/src/enums/CharacterType.java b/src/enums/CharacterType.java new file mode 100644 index 0000000..86c2f55 --- /dev/null +++ b/src/enums/CharacterType.java @@ -0,0 +1,5 @@ +package enums; + +public enum CharacterType { + TRAVELER, CHILD +} diff --git a/src/interfaces/Interactable.java b/src/interfaces/Interactable.java new file mode 100644 index 0000000..caa9264 --- /dev/null +++ b/src/interfaces/Interactable.java @@ -0,0 +1,7 @@ +package interfaces; + +import model.StoryContext; + +public interface Interactable { + void interact(StoryContext context); +} \ No newline at end of file diff --git a/src/interfaces/Listenable.java b/src/interfaces/Listenable.java new file mode 100644 index 0000000..934021e --- /dev/null +++ b/src/interfaces/Listenable.java @@ -0,0 +1,7 @@ +package interfaces; + +import model.StoryContext; + +public interface Listenable { + void listen(StoryContext context); +} \ No newline at end of file diff --git a/src/interfaces/Listener.java b/src/interfaces/Listener.java new file mode 100644 index 0000000..b23b7a6 --- /dev/null +++ b/src/interfaces/Listener.java @@ -0,0 +1,5 @@ +package interfaces; + +public interface Listener { + void listen(); +} diff --git a/src/model/City.java b/src/model/City.java new file mode 100644 index 0000000..8f9c021 --- /dev/null +++ b/src/model/City.java @@ -0,0 +1,8 @@ +package model; + +public class City extends StoryElement { + + public City(String description) { + super(description); + } +} \ No newline at end of file diff --git a/src/model/House.java b/src/model/House.java new file mode 100644 index 0000000..5e2ab5b --- /dev/null +++ b/src/model/House.java @@ -0,0 +1,28 @@ +package model; + +import characters.Character; +import interfaces.Listenable; +import java.util.ArrayList; +import java.util.List; + +public class House extends StoryElement { + + private List residents; + + public House(String description) { + super(description); + this.residents = new ArrayList<>(); + } + + public void addResident(Character character) { + this.residents.add(character); + } + + public void tellEveningStories(StoryContext context) { + for (Character resident : residents) { + if (resident != null) { + ((Listenable) resident).listen(context); + } + } + } +} \ No newline at end of file diff --git a/src/model/StoryContext.java b/src/model/StoryContext.java new file mode 100644 index 0000000..42217e1 --- /dev/null +++ b/src/model/StoryContext.java @@ -0,0 +1,13 @@ +package model; + +public class StoryContext { + private City city; + private House house; + + public StoryContext(City city, House house) { + this.city = city; + this.house = house; + } + + // Getters and setters for city and house +} \ No newline at end of file diff --git a/src/model/StoryElement.java b/src/model/StoryElement.java new file mode 100644 index 0000000..46ab2da --- /dev/null +++ b/src/model/StoryElement.java @@ -0,0 +1,35 @@ +package model; + +import java.util.Objects; + +public abstract class StoryElement { + protected String description; + + public StoryElement(String description) { + this.description = description; + } + + public String getDescription() { + return description; + } + + @Override + public String toString() { + return this.getClass().getSimpleName() + "{" + + "description='" + description + '\'' + + '}'; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + StoryElement that = (StoryElement) obj; + return Objects.equals(description, that.description); + } + + @Override + public int hashCode() { + return description != null ? description.hashCode() : 0; + } +} \ No newline at end of file diff --git a/src/story/ListeningSession.java b/src/story/ListeningSession.java new file mode 100644 index 0000000..b57bf78 --- /dev/null +++ b/src/story/ListeningSession.java @@ -0,0 +1,25 @@ +package story; + +import interfaces.Listener; +import characters.Character; + +import java.util.ArrayList; + +class ListeningSession extends StoryEvent { + private ArrayList listeners; + + public ListeningSession(String description, ArrayList listeners) { + super(description); + this.listeners = listeners; + } + + @Override + public void occur() { + super.occur(); + for (Character listener : listeners) { + if (listener instanceof Listener) { + ((Listener) listener).listen(); + } + } + } +} \ No newline at end of file diff --git a/src/story/Narrative.java b/src/story/Narrative.java new file mode 100644 index 0000000..d31438e --- /dev/null +++ b/src/story/Narrative.java @@ -0,0 +1,21 @@ +package story; + +import java.util.ArrayList; + +class Narrative { + private ArrayList events; + + public Narrative() { + this.events = new ArrayList<>(); + } + + public void addEvent(StoryEvent event) { + events.add(event); + } + + public void unfold() { + for (StoryEvent event : events) { + event.occur(); + } + } +} \ No newline at end of file diff --git a/src/story/StoryEvent.java b/src/story/StoryEvent.java new file mode 100644 index 0000000..5face20 --- /dev/null +++ b/src/story/StoryEvent.java @@ -0,0 +1,13 @@ +package story; + +class StoryEvent { + private final String description; + + public StoryEvent(String description) { + this.description = description; + } + + public void occur() { + System.out.println(description); + } +} \ No newline at end of file