Initial commit

This commit is contained in:
Alexander Karpov 2023-11-27 16:01:05 +03:00
parent 8ec0ab6f0f
commit 48ad83ea5a
18 changed files with 316 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
.idea
# ---> Java # ---> Java
# Compiled class file # Compiled class file
*.class *.class

11
lab-3.iml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

21
src/StoryTeller.java Normal file
View File

@ -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);
}
}

View File

@ -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);
}

33
src/characters/Child.java Normal file
View File

@ -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) {
}
}

View File

@ -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
}
}

View File

@ -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 + " слушает.");
}
}

View File

@ -0,0 +1,5 @@
package enums;
public enum CharacterType {
TRAVELER, CHILD
}

View File

@ -0,0 +1,7 @@
package interfaces;
import model.StoryContext;
public interface Interactable {
void interact(StoryContext context);
}

View File

@ -0,0 +1,7 @@
package interfaces;
import model.StoryContext;
public interface Listenable {
void listen(StoryContext context);
}

View File

@ -0,0 +1,5 @@
package interfaces;
public interface Listener {
void listen();
}

8
src/model/City.java Normal file
View File

@ -0,0 +1,8 @@
package model;
public class City extends StoryElement {
public City(String description) {
super(description);
}
}

28
src/model/House.java Normal file
View File

@ -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<Character> 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);
}
}
}
}

View File

@ -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
}

View File

@ -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;
}
}

View File

@ -0,0 +1,25 @@
package story;
import interfaces.Listener;
import characters.Character;
import java.util.ArrayList;
class ListeningSession extends StoryEvent {
private ArrayList<Character> listeners;
public ListeningSession(String description, ArrayList<Character> listeners) {
super(description);
this.listeners = listeners;
}
@Override
public void occur() {
super.occur();
for (Character listener : listeners) {
if (listener instanceof Listener) {
((Listener) listener).listen();
}
}
}
}

21
src/story/Narrative.java Normal file
View File

@ -0,0 +1,21 @@
package story;
import java.util.ArrayList;
class Narrative {
private ArrayList<StoryEvent> events;
public Narrative() {
this.events = new ArrayList<>();
}
public void addEvent(StoryEvent event) {
events.add(event);
}
public void unfold() {
for (StoryEvent event : events) {
event.occur();
}
}
}

13
src/story/StoryEvent.java Normal file
View File

@ -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);
}
}