From a8a12bb3f88752f574ebbacf491f09da7397b579 Mon Sep 17 00:00:00 2001 From: Mark Struchkov Date: Sat, 12 Jan 2019 14:23:19 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B8=20?= =?UTF-8?q?=D0=B8=20=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Добавил класс, который вставляет слова в шаблонную строку Unit * Добавил тесты проверки этого класса * Добавил новый класс Person, который сохраняет текущий Unit --- .../sadtech/autoresponder/Autoresponder.java | 23 ++++++++-- .../sadtech/autoresponder/entity/Person.java | 11 +++++ .../repository/PersonRepository.java | 15 +++++++ .../repository/UnitRepository.java | 2 +- .../repository/impl/PersonRepositoryImpl.java | 31 +++++++++++++ .../repository/impl/UnitRepositoryImpl.java | 4 +- .../autoresponder/service/PersonService.java | 10 +++++ .../autoresponder/service/UnitService.java | 4 ++ .../service/impl/PersonServiceImpl.java | 21 +++++++++ .../service/impl/UnitServiceImpl.java | 7 +-- .../submodule/parser/InsertWords.java | 33 ++++++++++++++ .../submodule/parser/InsertWordsTest.java | 44 +++++++++++++++++++ 12 files changed, 196 insertions(+), 9 deletions(-) create mode 100644 src/main/java/org/sadtech/autoresponder/entity/Person.java create mode 100644 src/main/java/org/sadtech/autoresponder/repository/PersonRepository.java create mode 100644 src/main/java/org/sadtech/autoresponder/repository/impl/PersonRepositoryImpl.java create mode 100644 src/main/java/org/sadtech/autoresponder/service/PersonService.java create mode 100644 src/main/java/org/sadtech/autoresponder/service/impl/PersonServiceImpl.java create mode 100644 src/main/java/org/sadtech/autoresponder/submodule/parser/InsertWords.java create mode 100644 src/test/java/org/sadtech/autoresponder/submodule/parser/InsertWordsTest.java diff --git a/src/main/java/org/sadtech/autoresponder/Autoresponder.java b/src/main/java/org/sadtech/autoresponder/Autoresponder.java index 254c6e8..b4b7f4b 100644 --- a/src/main/java/org/sadtech/autoresponder/Autoresponder.java +++ b/src/main/java/org/sadtech/autoresponder/Autoresponder.java @@ -1,17 +1,34 @@ package org.sadtech.autoresponder; +import com.sun.istack.internal.NotNull; import lombok.AllArgsConstructor; +import org.sadtech.autoresponder.entity.Person; import org.sadtech.autoresponder.entity.Unit; +import org.sadtech.autoresponder.service.PersonService; import org.sadtech.autoresponder.service.UnitService; +import org.sadtech.autoresponder.submodule.parser.InsertWords; + +import java.util.List; @AllArgsConstructor public class Autoresponder { private UnitService unitService; + private PersonService personService; - public Unit nextUnit(Integer idUnit, String message) { - Unit unit = unitService.getUnitById(idUnit); - return unitService.nextUnit(unit, message); + public String answer(@NotNull Integer idPerson, @NotNull String message) { + Person person = personService.getPersonById(idPerson); + Unit unit = person.getUnit(); + return unitService.nextUnit(unit, message).getAnswer(); + } + + public String answer(@NotNull Integer idPerson, @NotNull String message, @NotNull List words) { + Person person = personService.getPersonById(idPerson); + Unit unit = unitService.nextUnit(person.getUnit(), message); + InsertWords insertWords = new InsertWords(); + insertWords.setInText(unit.getAnswer()); + insertWords.insert(words); + return insertWords.getOutText(); } } diff --git a/src/main/java/org/sadtech/autoresponder/entity/Person.java b/src/main/java/org/sadtech/autoresponder/entity/Person.java new file mode 100644 index 0000000..ab4a6b3 --- /dev/null +++ b/src/main/java/org/sadtech/autoresponder/entity/Person.java @@ -0,0 +1,11 @@ +package org.sadtech.autoresponder.entity; + +import lombok.Data; + +@Data +public class Person { + + private Integer id; + private Unit unit; + +} diff --git a/src/main/java/org/sadtech/autoresponder/repository/PersonRepository.java b/src/main/java/org/sadtech/autoresponder/repository/PersonRepository.java new file mode 100644 index 0000000..a9c668e --- /dev/null +++ b/src/main/java/org/sadtech/autoresponder/repository/PersonRepository.java @@ -0,0 +1,15 @@ +package org.sadtech.autoresponder.repository; + +import org.sadtech.autoresponder.entity.Person; + +import java.util.Collection; +import java.util.Map; + +public interface PersonRepository { + + void addPerson(Person person); + void removePerson(Person person); + void addPersonAll(Map personCollection); + Person getPersonById(Integer idPerson); + +} diff --git a/src/main/java/org/sadtech/autoresponder/repository/UnitRepository.java b/src/main/java/org/sadtech/autoresponder/repository/UnitRepository.java index 1d918ac..2b7817e 100644 --- a/src/main/java/org/sadtech/autoresponder/repository/UnitRepository.java +++ b/src/main/java/org/sadtech/autoresponder/repository/UnitRepository.java @@ -12,5 +12,5 @@ public interface UnitRepository { void addUnits(Collection units); - void removeUnit(Integer idUnit); + void removeUnit(Unit idUnit); } diff --git a/src/main/java/org/sadtech/autoresponder/repository/impl/PersonRepositoryImpl.java b/src/main/java/org/sadtech/autoresponder/repository/impl/PersonRepositoryImpl.java new file mode 100644 index 0000000..f6822ff --- /dev/null +++ b/src/main/java/org/sadtech/autoresponder/repository/impl/PersonRepositoryImpl.java @@ -0,0 +1,31 @@ +package org.sadtech.autoresponder.repository.impl; + +import org.sadtech.autoresponder.entity.Person; +import org.sadtech.autoresponder.repository.PersonRepository; + +import java.util.*; + +public class PersonRepositoryImpl implements PersonRepository { + + Map people = new HashMap<>(); + + @Override + public void addPerson(Person person) { + people.put(person.getId(), person); + } + + @Override + public void removePerson(Person person) { + people.remove(person); + } + + @Override + public void addPersonAll(Map personCollection) { + people.putAll(personCollection); + } + + @Override + public Person getPersonById(Integer idPerson) { + return people.get(idPerson); + } +} diff --git a/src/main/java/org/sadtech/autoresponder/repository/impl/UnitRepositoryImpl.java b/src/main/java/org/sadtech/autoresponder/repository/impl/UnitRepositoryImpl.java index f837ed4..17b73a2 100644 --- a/src/main/java/org/sadtech/autoresponder/repository/impl/UnitRepositoryImpl.java +++ b/src/main/java/org/sadtech/autoresponder/repository/impl/UnitRepositoryImpl.java @@ -27,7 +27,7 @@ public class UnitRepositoryImpl implements UnitRepository { } @Override - public void removeUnit(Integer idUnit) { - units.remove(idUnit); + public void removeUnit(Unit idUnit) { + units.remove(idUnit.getIdUnit()); } } diff --git a/src/main/java/org/sadtech/autoresponder/service/PersonService.java b/src/main/java/org/sadtech/autoresponder/service/PersonService.java new file mode 100644 index 0000000..66b993a --- /dev/null +++ b/src/main/java/org/sadtech/autoresponder/service/PersonService.java @@ -0,0 +1,10 @@ +package org.sadtech.autoresponder.service; + +import org.sadtech.autoresponder.entity.Person; + +public interface PersonService { + + Person getPersonById(Integer integer); + void addPerson(Person person); + +} diff --git a/src/main/java/org/sadtech/autoresponder/service/UnitService.java b/src/main/java/org/sadtech/autoresponder/service/UnitService.java index 3cf507c..326a1ad 100644 --- a/src/main/java/org/sadtech/autoresponder/service/UnitService.java +++ b/src/main/java/org/sadtech/autoresponder/service/UnitService.java @@ -1,10 +1,14 @@ package org.sadtech.autoresponder.service; import lombok.NonNull; +import org.sadtech.autoresponder.entity.Person; import org.sadtech.autoresponder.entity.Unit; +import java.util.List; + public interface UnitService { Unit nextUnit(Unit unit, String message); Unit getUnitById(Integer idUnit); + } diff --git a/src/main/java/org/sadtech/autoresponder/service/impl/PersonServiceImpl.java b/src/main/java/org/sadtech/autoresponder/service/impl/PersonServiceImpl.java new file mode 100644 index 0000000..32ed579 --- /dev/null +++ b/src/main/java/org/sadtech/autoresponder/service/impl/PersonServiceImpl.java @@ -0,0 +1,21 @@ +package org.sadtech.autoresponder.service.impl; + +import org.sadtech.autoresponder.entity.Person; +import org.sadtech.autoresponder.repository.PersonRepository; +import org.sadtech.autoresponder.service.PersonService; + +public class PersonServiceImpl implements PersonService { + + private PersonRepository personRepository; + + + @Override + public Person getPersonById(Integer integer) { + return personRepository.getPersonById(integer); + } + + @Override + public void addPerson(Person person) { + personRepository.addPerson(person); + } +} diff --git a/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java b/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java index 6563755..e4b58e9 100644 --- a/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java +++ b/src/main/java/org/sadtech/autoresponder/service/impl/UnitServiceImpl.java @@ -2,14 +2,13 @@ package org.sadtech.autoresponder.service.impl; import com.sun.istack.internal.NotNull; import lombok.AllArgsConstructor; -import lombok.NonNull; -import lombok.Setter; import org.sadtech.autoresponder.entity.Unit; import org.sadtech.autoresponder.repository.UnitRepository; -import org.sadtech.autoresponder.submodule.parser.Parser; import org.sadtech.autoresponder.service.UnitService; +import org.sadtech.autoresponder.submodule.parser.Parser; import java.util.ArrayList; +import java.util.List; @AllArgsConstructor public class UnitServiceImpl implements UnitService { @@ -38,5 +37,7 @@ public class UnitServiceImpl implements UnitService { public Unit getUnitById(@NotNull Integer idUnit) { return unitRepository.getUnitById(idUnit); } + + } diff --git a/src/main/java/org/sadtech/autoresponder/submodule/parser/InsertWords.java b/src/main/java/org/sadtech/autoresponder/submodule/parser/InsertWords.java new file mode 100644 index 0000000..452e31f --- /dev/null +++ b/src/main/java/org/sadtech/autoresponder/submodule/parser/InsertWords.java @@ -0,0 +1,33 @@ +package org.sadtech.autoresponder.submodule.parser; + +import lombok.Getter; +import lombok.Setter; + +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class InsertWords { + + @Setter + private String inText; + @Getter + private String outText; + + public void insert(List words) { + Pattern pattern = Pattern.compile("\\{(\\d+)}"); // Задаем шаблон + Matcher m = pattern.matcher(inText); // Инициализация Matcher + StringBuffer result = new StringBuffer(); // Буфер для конечного значения + while (m.find()) { // Проверка на совпадение + if (words.get(Integer.parseInt(m.group(1))) != null) { + m.appendReplacement(result, words.get(Integer.parseInt(m.group(1)))); // Подставляем значение из HashMap + } else { + m.appendReplacement(result, m.group(0)); + } + } + m.appendTail(result); // Добавить остаток строки + outText = result.toString(); + } + +} + diff --git a/src/test/java/org/sadtech/autoresponder/submodule/parser/InsertWordsTest.java b/src/test/java/org/sadtech/autoresponder/submodule/parser/InsertWordsTest.java new file mode 100644 index 0000000..f0d74b5 --- /dev/null +++ b/src/test/java/org/sadtech/autoresponder/submodule/parser/InsertWordsTest.java @@ -0,0 +1,44 @@ +package org.sadtech.autoresponder.submodule.parser; + +import org.junit.After; +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; + +public class InsertWordsTest { + + private ArrayList arrayList = new ArrayList<>(); + private InsertWords insertWords = new InsertWords(); + + @After + public void setUp() { + + } + + @Test + public void insert() { + insertWords.setInText("Проверка {0} теста"); + arrayList.add("первого"); + insertWords.insert(arrayList); + Assert.assertEquals(insertWords.getOutText(),"Проверка первого теста"); + } + + @Test + public void insert2() { + insertWords.setInText("Проверка {0} теста и {1} {теста}"); + arrayList.add("первого"); + arrayList.add("второго"); + insertWords.insert(arrayList); + Assert.assertEquals(insertWords.getOutText(),"Проверка первого теста и второго {теста}"); + } + + @Test + public void insert3() { + insertWords.setInText("Проверка {1} теста и {0} {теста}"); + arrayList.add("первого"); + arrayList.add("второго"); + insertWords.insert(arrayList); + Assert.assertEquals(insertWords.getOutText(),"Проверка второго теста и первого {теста}"); + } +} \ No newline at end of file