Добавление логики и тестов
* Добавил класс, который вставляет слова в шаблонную строку Unit * Добавил тесты проверки этого класса * Добавил новый класс Person, который сохраняет текущий Unit
This commit is contained in:
parent
1cf0b4dce6
commit
a8a12bb3f8
@ -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<String> 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
11
src/main/java/org/sadtech/autoresponder/entity/Person.java
Normal file
11
src/main/java/org/sadtech/autoresponder/entity/Person.java
Normal file
@ -0,0 +1,11 @@
|
||||
package org.sadtech.autoresponder.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Person {
|
||||
|
||||
private Integer id;
|
||||
private Unit unit;
|
||||
|
||||
}
|
@ -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<Integer, Person> personCollection);
|
||||
Person getPersonById(Integer idPerson);
|
||||
|
||||
}
|
@ -12,5 +12,5 @@ public interface UnitRepository {
|
||||
|
||||
void addUnits(Collection<Unit> units);
|
||||
|
||||
void removeUnit(Integer idUnit);
|
||||
void removeUnit(Unit idUnit);
|
||||
}
|
||||
|
@ -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<Integer, Person> 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<Integer, Person> personCollection) {
|
||||
people.putAll(personCollection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Person getPersonById(Integer idPerson) {
|
||||
return people.get(idPerson);
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
||||
}
|
@ -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);
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -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<String> 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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<String> 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(),"Проверка второго теста и первого {теста}");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user