Compare commits

..

No commits in common. "operations" and "master" have entirely different histories.

9 changed files with 60 additions and 254 deletions

View File

@ -2,63 +2,40 @@
<html> <html>
<head> <head>
<title>Calculator</title> <title>Калькулятор</title>
<link rel="stylesheet" href="https://unpkg.com/chota@latest"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
</head> </head>
<body> <body>
<div class="container" style="max-width:600px"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
<div class="card row is-full-screen is-center"> <div class="m-auto dflex flex-row align-items-center">
<div class="col"> <div class="col-sm-3 my-1">
<div class="row"> <input type="number" class="form-control" id="num1">
<div class="col"> </div>
<input type="text" id="value1"> <div class="col-sm-3 my-1">
</div> <select class="form-control" id="operation">
<div class="col"> <option value="sum" selected>+</option>
<select id="operation"> <option value="sub">-</option>
<option value="sum" selected>+</option> <option value="mul">*</option>
<option value="sub">-</option> <option value="div">/</option>
<option value="mul">*</option> </select>
<option value="invert">~</option> </div>
</select> <div class="col-sm-3 my-1">
</div> <input type="number" class="form-control" id="num2">
<div class="col"> </div>
<input type="text" id="value2"> <div class="col-sm-3 my-1">
</div> <button type="button" class="form-control btn btn-light" id="get-result" onclick="calculate()">=</button>
</div> </div>
<div class="row"> <div class="col-sm-3 my-1">
<div class="col"> <p class="h5" id="result"></p>
<button type="button" id="get-int-result" onclick="calculate('intops')">Result for int</button> </div>
</div>
<div class="col is-right">
<button type="button" id="get-string-result" onclick="calculate('stringops')">Result for string</button>
</div>
</div>
<div class="card row is-center">
<h5 class="is-center" id="result"></h5>
</div>
</div>
</div>
</div> </div>
<script> <script>
async function calculate(ops) { async function calculate() {
const value1 = document.getElementById("value1").value const num1 = document.getElementById("num1").value
const value2 = document.getElementById("value2").value const num2 = document.getElementById("num2").value
const op = document.getElementById("operation").value document.getElementById("result").innerHTML = await (await fetch(`http://127.0.0.1:8080/${document.getElementById("operation").value}?num1=${num1}&num2=${num2}`)).text()
if (op == 'sum') {
document.getElementById("result").innerHTML = await (await fetch(`http://127.0.0.1:8080/sum?value1=${value1}&value2=${value2}&ops=${ops}`)).text()
} else if (op == 'sub') {
document.getElementById("result").innerHTML = await (await fetch(`http://127.0.0.1:8080/sub?value1=${value1}&value2=${value2}&ops=${ops}`)).text()
} else if (op == 'mul') {
document.getElementById("result").innerHTML = await (await fetch(`http://127.0.0.1:8080/mul?value=${value1}&count=${value2}&ops=${ops}`)).text()
} else {
document.getElementById("result").innerHTML = await (await fetch(`http://127.0.0.1:8080/invert?value=${value1}&ops=${ops}`)).text()
}
} }
</script> </script>
</body> </body>

View File

@ -3,7 +3,7 @@ const http = require('http')
const requestListener = async function (req, res) { const requestListener = async function (req, res) {
res.writeHead(200); res.writeHead(200);
fs.readFile('index.html', 'utf8', (err, data) => { fs.readFile('index.html', (err, data) => {
res.end(data); res.end(data);
}); });
}; };

View File

@ -1,50 +1,41 @@
package np.something.controllers; package np.something.controllers;
import np.something.services.OperationsService;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
public class MathController { public class MathController {
private final OperationsService operationsService;
public MathController(OperationsService operationsService) {
this.operationsService = operationsService;
}
@GetMapping("/sum") @GetMapping("/sum")
public Object sum( public int sum(
@RequestParam(value = "value1", defaultValue = "0") String value1, @RequestParam(value = "num1", defaultValue = "0") int num1,
@RequestParam(value = "value2", defaultValue = "0") String value2, @RequestParam(value = "num2", defaultValue = "0") int num2
@RequestParam(value = "ops", defaultValue = "intops") String ops
) { ) {
return operationsService.add(value1, value2, ops);
return num1 + num2;
} }
@GetMapping("/sub") @GetMapping("/sub")
public Object sub( public int sub(
@RequestParam(value = "value1", defaultValue = "0") String value1, @RequestParam(value = "num1", defaultValue = "0") int num1,
@RequestParam(value = "value2", defaultValue = "0") String value2, @RequestParam(value = "num2", defaultValue = "0") int num2
@RequestParam(value = "ops", defaultValue = "intops") String ops
) { ) {
return operationsService.sub(value1, value2, ops); return num1 - num2;
} }
@GetMapping("/mul") @GetMapping("/mul")
public Object mul( public int mul(
@RequestParam(value = "value", defaultValue = "0") String value, @RequestParam(value = "num1", defaultValue = "0") int num1,
@RequestParam(value = "count", defaultValue = "0") int count, @RequestParam(value = "num2", defaultValue = "0") int num2
@RequestParam(value = "ops", defaultValue = "intops") String ops
) { ) {
return operationsService.mul(value, count, ops); return num1 * num2;
} }
@GetMapping("/invert") @GetMapping("/div")
public Object div( public int div(
@RequestParam(value = "value", defaultValue = "0") String value, @RequestParam(value = "num1", defaultValue = "0") int num1,
@RequestParam(value = "ops", defaultValue = "intops") String ops @RequestParam(value = "num2", defaultValue = "0") int num2
) { ) {
return operationsService.invert(value, ops); return num1 / num2;
} }
} }

View File

@ -1,25 +0,0 @@
package np.something.model;
import org.springframework.stereotype.Component;
@Component(value = "intops")
public class IntegerOperations implements Operations<Integer> {
@Override
public Integer add(Integer first, Integer second) {
return first + second;
}
@Override
public Integer sub(Integer first, Integer second) {
return first - second;
}
@Override
public Integer mul(Integer value, int count) {
return value * count;
}
@Override
public Integer invert(Integer value) {
return ~value;
}
}

View File

@ -1,8 +0,0 @@
package np.something.model;
public interface Operations<T> {
T add(T first, T second);
T sub(T first, T second);
T mul(T value, int count);
T invert(T value);
}

View File

@ -1,30 +0,0 @@
package np.something.model;
import org.springframework.stereotype.Component;
@Component(value = "stringops")
public class StringOperations implements Operations<String>{
@Override
public String add(String first, String second) {
return first + second;
}
@Override
public String sub(String first, String second) {
return first.replace(second, "");
}
@Override
public String mul(String value, int count) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < count; i++) {
result.append(value);
}
return result.toString();
}
@Override
public String invert(String value) {
return new StringBuilder(value).reverse().toString();
}
}

View File

@ -1,45 +0,0 @@
package np.something.services;
import np.something.model.Operations;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
@Service
public class OperationsService {
private final ApplicationContext applicationContext;
public OperationsService(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public Object add(Object first, Object second, String ops) {
Operations operations = (Operations) applicationContext.getBean(ops);
if (ops.startsWith("int")) {
return operations.add(Integer.parseInt(first.toString()), Integer.parseInt(second.toString()));
}
return operations.add(first, second);
}
public Object sub(Object first, Object second, String ops) {
Operations operations = (Operations) applicationContext.getBean(ops);
if (ops.startsWith("int")) {
return operations.sub(Integer.parseInt(first.toString()), Integer.parseInt(second.toString()));
}
return operations.sub(first, second);
}
public Object mul(Object value, int count, String ops) {
Operations operations = (Operations) applicationContext.getBean(ops);
if (ops.startsWith("int")) {
return operations.mul(Integer.parseInt(value.toString()), count);
}
return operations.mul(value, count);
}
public Object invert(Object value, String ops) {
Operations operations = (Operations) applicationContext.getBean(ops);
if (ops.startsWith("int")) {
return operations.invert(Integer.parseInt(value.toString()));
}
return operations.invert(value);
}
}

View File

@ -1,67 +0,0 @@
package np.something;
import np.something.services.OperationsService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class OperationsServiceTests {
@Autowired
OperationsService operationsService;
@Test
void stringSum() {
Assertions.assertEquals("Hello, World!", operationsService.add("Hello,", " World!", "stringops"));
Assertions.assertEquals("Hello, World!", operationsService.add("", "Hello, World!", "stringops"));
}
@Test
void stringSub() {
Assertions.assertEquals("Яблк", operationsService.sub("Яблоко", "о", "stringops"));
Assertions.assertEquals("Тлвизор", operationsService.sub("Телевизор", "е", "stringops"));
Assertions.assertEquals("Rewte", operationsService.sub("Rewrite", "ri", "stringops"));
}
@Test
void stringMul() {
Assertions.assertEquals("ДеревоДерево", operationsService.mul("Дерево", 2, "stringops"));
Assertions.assertEquals("ЛоЛоЛо", operationsService.mul("Ло", 3, "stringops"));
Assertions.assertEquals("", operationsService.mul("Слово", 0, "stringops"));
}
@Test
void stringInvert() {
Assertions.assertEquals("оволС", operationsService.invert("Слово", "stringops"));
Assertions.assertEquals("droW", operationsService.invert("Word", "stringops"));
Assertions.assertEquals("", operationsService.invert("", "stringops"));
}
@Test
void intSum() {
Assertions.assertEquals(22, operationsService.add(11, 11, "intops"));
Assertions.assertEquals(-10, operationsService.add(15, -25, "intops"));
}
@Test
void intSub() {
Assertions.assertEquals(0, operationsService.sub(10, 10, "intops"));
Assertions.assertEquals(100, operationsService.sub(100, 0, "intops"));
Assertions.assertEquals(3, operationsService.sub(-3, -6, "intops"));
}
@Test
void intMul() {
Assertions.assertEquals(0, operationsService.mul(0, 10, "intops"));
Assertions.assertEquals(18, operationsService.mul(6, 3, "intops"));
Assertions.assertEquals(-4, operationsService.mul(-1, 4, "intops"));
}
@Test
void intInvert() {
Assertions.assertEquals(0, operationsService.invert(-1, "intops"));
Assertions.assertEquals(-2, operationsService.invert(1, "intops"));
Assertions.assertEquals(-3, operationsService.invert(2, "intops"));
}
}

View File

@ -0,0 +1,13 @@
package np.something;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SomethingApplicationTests {
@Test
void contextLoads() {
}
}