Fix tests
This commit is contained in:
parent
28c8c4cb00
commit
3f7833f81a
@ -10,31 +10,43 @@
|
|||||||
<article class="container align-content-center">
|
<article class="container align-content-center">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-md-2 m-2 p-0">
|
<div class="col-md-2 m-2 p-0">
|
||||||
<select class="form-control" id="operation">
|
<select class="form-control" id="type">
|
||||||
<option value="doExp" selected>Возведение в квадрат</option>
|
<option value="int" selected>Числа</option>
|
||||||
<option value="doSin">Вычисление синуса</option>
|
<option value="str">Строки</option>
|
||||||
<option value="doAbs">Модуль числа</option>
|
|
||||||
<option value="doRand">Случайное число</option>
|
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-2 m-2 p-0">
|
<div class="col-md-2 m-2 p-0">
|
||||||
<input type="number" class="form-control" id="num">
|
<select class="form-control" id="calc">
|
||||||
|
<option value="sum" selected>Сумма</option>
|
||||||
|
<option value="sub">Вычитание</option>
|
||||||
|
<option value="mult">Умножение</option>
|
||||||
|
<option value="div">Деление</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<div class="col-md-2 m-2 p-0">
|
||||||
|
<input type="text" class="form-control" id="item1">
|
||||||
|
</div>
|
||||||
|
<div class="col-md-2 m-2 p-0">
|
||||||
|
<input type="text" class="form-control" id="item2">
|
||||||
</div>
|
</div>
|
||||||
<div class="col-md-3 m-2 p-0">
|
<div class="col-md-3 m-2 p-0">
|
||||||
<button type="button" class="form-control btn btn-info" onclick="manipulation()">Вывести результат</button>
|
<button type="button" class="form-control btn btn-info" onclick="calculator()">Вывести результат</button>
|
||||||
</div>
|
|
||||||
<div class="col-md-2 m-2 p-0 align-middle">
|
|
||||||
<p class="h4" id="result"></p>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="col-md-5 m-2 p-0 align-middle">
|
||||||
|
<p class="h4" id="result"></p>
|
||||||
|
</div>
|
||||||
</article>
|
</article>
|
||||||
|
|
||||||
<script>
|
|
||||||
async function manipulation() {
|
|
||||||
const num = document.getElementById("num").value
|
|
||||||
const oper = document.getElementById("operation").value
|
|
||||||
|
|
||||||
var url = `http://127.0.0.1:8080/${oper}?val=${num}`
|
<script>
|
||||||
|
async function calculator() {
|
||||||
|
const calc = document.getElementById("calc").value
|
||||||
|
const item1 = document.getElementById("item1").value
|
||||||
|
const item2 = document.getElementById("item2").value
|
||||||
|
const type = document.getElementById("type").value
|
||||||
|
|
||||||
|
var url = `http://127.0.0.1:8080/${calc}?item1=${item1}&item2=${item2}&type=${type}`
|
||||||
|
|
||||||
const responce = await fetch(url)
|
const responce = await fetch(url)
|
||||||
const resout = await responce.text()
|
const resout = await responce.text()
|
||||||
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.labs.LabWork.Calculator.Configuration;
|
||||||
|
|
||||||
|
import com.labs.LabWork.Calculator.Domain.IntCalculator;
|
||||||
|
import com.labs.LabWork.Calculator.Domain.StringCalculator;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
|
public class CalculatorConfiguration {
|
||||||
|
private final Logger log = LoggerFactory.getLogger(CalculatorConfiguration.class);
|
||||||
|
|
||||||
|
@Bean(value="int")
|
||||||
|
public IntCalculator createIntCalculator(){
|
||||||
|
return new IntCalculator();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(value="string")
|
||||||
|
public StringCalculator createStringCalculator(){
|
||||||
|
return new StringCalculator();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package com.labs.LabWork.Calculator.Controller;
|
||||||
|
import com.labs.LabWork.Calculator.Service.CalculatorService;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
public class CalculatorController {
|
||||||
|
private final CalculatorService calculatorService;
|
||||||
|
|
||||||
|
public CalculatorController(CalculatorService calculatorService) {
|
||||||
|
this.calculatorService = calculatorService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/sum")
|
||||||
|
public Object sum(@RequestParam(value = "item1", defaultValue = "null objects") Object item1,
|
||||||
|
@RequestParam(value = "item2", defaultValue = "provided") Object item2,
|
||||||
|
@RequestParam(value = "type", defaultValue = "string") String type){
|
||||||
|
return calculatorService.sum(item1, item2, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/sub")
|
||||||
|
public Object sub(@RequestParam(value = "item1", defaultValue = "null objects") Object item1,
|
||||||
|
@RequestParam(value = "item2", defaultValue = "provided") Object item2,
|
||||||
|
@RequestParam(value = "type", defaultValue = "string") String type){
|
||||||
|
return calculatorService.subtraction(item1, item2, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/mult")
|
||||||
|
public Object mult(@RequestParam(value = "item1", defaultValue = "null objects") Object item1,
|
||||||
|
@RequestParam(value = "item2", defaultValue = "provided") Object item2,
|
||||||
|
@RequestParam(value = "type", defaultValue = "string") String type){
|
||||||
|
return calculatorService.multiplication(item1, item2, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/div")
|
||||||
|
public Object div(@RequestParam(value = "item1", defaultValue = "null objects") Object item1,
|
||||||
|
@RequestParam(value = "item2", defaultValue = "provided") Object item2,
|
||||||
|
@RequestParam(value = "type", defaultValue = "string") String type){
|
||||||
|
return calculatorService.division(item1, item2, type);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
package com.labs.LabWork.Calculator.Domain;
|
||||||
|
|
||||||
|
public interface ICalculator<T> {
|
||||||
|
T sum(T item1, T item2);
|
||||||
|
T subtraction(T item1, T item2);
|
||||||
|
T multiplication(T item1, T item2);
|
||||||
|
T division(T item1, T item2);
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.labs.LabWork.Calculator.Domain;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component(value = "int")
|
||||||
|
public class IntCalculator implements ICalculator<Integer>{
|
||||||
|
@Override
|
||||||
|
public Integer sum(Integer num1, Integer num2) {
|
||||||
|
return num1 + num2;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer subtraction(Integer num1, Integer num2) {
|
||||||
|
return num1 - num2;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer multiplication(Integer num1, Integer num2) {
|
||||||
|
return num1*num2;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer division(Integer num1, Integer num2) {
|
||||||
|
try {
|
||||||
|
return num1 / num2;
|
||||||
|
}
|
||||||
|
catch (ArithmeticException ex){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
package com.labs.LabWork.Calculator.Domain;
|
||||||
|
|
||||||
|
public class StringCalculator implements ICalculator<String> {
|
||||||
|
@Override
|
||||||
|
public String sum(String s1, String s2) {
|
||||||
|
return s1 + s2;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String subtraction(String s1, String s2) {
|
||||||
|
String res = "";
|
||||||
|
for (char c : s1.toCharArray()){
|
||||||
|
boolean foundInOther = false;
|
||||||
|
for (int i = 0; i < s2.length(); i++){
|
||||||
|
if (c == s2.charAt(i)) {
|
||||||
|
foundInOther = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!foundInOther) res += c;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String multiplication(String s1, String s2) {
|
||||||
|
String res = "";
|
||||||
|
for (char c : s1.toCharArray()){
|
||||||
|
boolean foundInOther = false;
|
||||||
|
for (int i = 0; i < s2.length(); i++){
|
||||||
|
if (c == s2.charAt(i)) {
|
||||||
|
foundInOther = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (foundInOther) res += c;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String division(String s1, String s2) {
|
||||||
|
StringBuilder res = new StringBuilder();
|
||||||
|
int maxLength = Integer.max(s1.length(), s2.length());
|
||||||
|
for (int i = 0; i < maxLength; i++){
|
||||||
|
if (i < s1.length()){
|
||||||
|
res.append(s1.charAt(i));
|
||||||
|
}
|
||||||
|
if (i < s2.length()){
|
||||||
|
res.append(s2.charAt(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res.toString();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.labs.LabWork.Calculator.Service;
|
||||||
|
|
||||||
|
import com.labs.LabWork.Calculator.Domain.ICalculator;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class CalculatorService {
|
||||||
|
private final ApplicationContext applicationContext;
|
||||||
|
public CalculatorService(ApplicationContext applicationContext){
|
||||||
|
this.applicationContext = applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object sum(Object item1, Object item2, String type){
|
||||||
|
final ICalculator calculator = (ICalculator) applicationContext.getBean(type);
|
||||||
|
if (type.startsWith("int")) return calculator.sum(Integer.parseInt(item1.toString()), Integer.parseInt(item2.toString()));
|
||||||
|
return calculator.sum(item1, item2);
|
||||||
|
}
|
||||||
|
public Object subtraction(Object item1, Object item2, String type){
|
||||||
|
final ICalculator calculator = (ICalculator) applicationContext.getBean(type);
|
||||||
|
if (type.startsWith("int")) return calculator.subtraction(Integer.parseInt(item1.toString()), Integer.parseInt(item2.toString()));
|
||||||
|
return calculator.subtraction(item1, item2);
|
||||||
|
}
|
||||||
|
public Object multiplication(Object item1, Object item2, String type){
|
||||||
|
final ICalculator calculator = (ICalculator) applicationContext.getBean(type);
|
||||||
|
if (type.startsWith("int")) return calculator.multiplication(Integer.parseInt(item1.toString()), Integer.parseInt(item2.toString()));
|
||||||
|
return calculator.multiplication(item1, item2);
|
||||||
|
}
|
||||||
|
public Object division(Object item1, Object item2, String type){
|
||||||
|
final ICalculator calculator = (ICalculator) applicationContext.getBean(type);
|
||||||
|
if (type.startsWith("int")) return calculator.division(Integer.parseInt(item1.toString()), Integer.parseInt(item2.toString()));
|
||||||
|
return calculator.subtraction(item1, item2);
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +1,73 @@
|
|||||||
package com.labs.LabWork;
|
package com.labs.LabWork;
|
||||||
|
|
||||||
|
import com.labs.LabWork.Calculator.Service.CalculatorService;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class LabWorkApplicationTests {
|
class LabWorkApplicationTests {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
CalculatorService calculatorService;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void contextLoads() {
|
void testIntegerSum() {
|
||||||
|
final Object res = calculatorService.sum(5, 2, "int");
|
||||||
|
Assertions.assertEquals("7", res.toString());
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void testIntegerSub() {
|
||||||
|
final Object res = calculatorService.subtraction(4, 2, "int");
|
||||||
|
Assertions.assertEquals("2", res.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIntegerMult() {
|
||||||
|
final Object res = calculatorService.multiplication(10, 3, "int");
|
||||||
|
Assertions.assertEquals("30", res.toString());
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
void testIntegerDiv() {
|
||||||
|
final Object res = calculatorService.division(4, 2, "int");
|
||||||
|
Assertions.assertEquals("2", res.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIntegerDivBy0() {
|
||||||
|
final Object res = calculatorService.division(4, 0, "int");
|
||||||
|
Assertions.assertEquals("0", res.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testStringSum(){
|
||||||
|
final Object res = calculatorService.sum("2", "2", "string");
|
||||||
|
Assertions.assertEquals("22", res.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testStringSub(){
|
||||||
|
final Object res = calculatorService.subtraction("53", "3", "string");
|
||||||
|
Assertions.assertEquals("5", res.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testStringMult(){
|
||||||
|
final Object res = calculatorService.multiplication("523", "215", "string");
|
||||||
|
Assertions.assertEquals("52", res.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testStringDiv(){
|
||||||
|
final Object res = calculatorService.division("15", "24", "string");
|
||||||
|
Assertions.assertEquals("1245", res.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSpeakerErrorWiredInt() {
|
||||||
|
Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> calculatorService.sum("1", "1", "integer"));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user