This commit is contained in:
Katerina881 2023-02-21 11:20:02 +04:00
parent c3ce1336cb
commit 8315a23855
9 changed files with 255 additions and 61 deletions

View File

@ -2,40 +2,63 @@
<html> <html>
<head> <head>
<title>Калькулятор</title> <title>Calculator</title>
<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"> <link rel="stylesheet" href="https://unpkg.com/chota@latest">
</head> </head>
<body> <body>
<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="container" style="max-width:600px">
<div class="m-auto dflex flex-row align-items-center"> <div class="card row is-full-screen is-center">
<div class="col-sm-3 my-1"> <div class="col">
<input type="number" class="form-control" id="num1"> <div class="row">
<div class="col">
<input type="text" id="value1">
</div> </div>
<div class="col-sm-3 my-1"> <div class="col">
<select class="form-control" id="operation"> <select id="operation">
<option value="sum" selected>+</option> <option value="sum" selected>+</option>
<option value="sub">-</option> <option value="sub">-</option>
<option value="mul">*</option> <option value="mul">*</option>
<option value="div">/</option> <option value="invert">~</option>
</select> </select>
</div> </div>
<div class="col-sm-3 my-1"> <div class="col">
<input type="number" class="form-control" id="num2"> <input type="text" id="value2">
</div> </div>
<div class="col-sm-3 my-1">
<button type="button" class="form-control btn btn-light" id="get-result" onclick="calculate()">=</button>
</div> </div>
<div class="col-sm-3 my-1"> <div class="row">
<p class="h5" id="result"></p> <div class="col">
<button type="button" id="get-int-result" onclick="calculate('intops')">Result for int</button>
</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>
</div>
<script> <script>
async function calculate() { async function calculate(ops) {
const num1 = document.getElementById("num1").value const value1 = document.getElementById("value1").value
const num2 = document.getElementById("num2").value const value2 = document.getElementById("value2").value
document.getElementById("result").innerHTML = await (await fetch(`http://127.0.0.1:8080/${document.getElementById("operation").value}?num1=${num1}&num2=${num2}`)).text() const op = document.getElementById("operation").value
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', (err, data) => { fs.readFile('index.html', 'utf8', (err, data) => {
res.end(data); res.end(data);
}); });
}; };

View File

@ -1,41 +1,50 @@
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 {
@GetMapping("/sum") private final OperationsService operationsService;
public int sum(
@RequestParam(value = "num1", defaultValue = "0") int num1,
@RequestParam(value = "num2", defaultValue = "0") int num2
) {
return num1 + num2; public MathController(OperationsService operationsService) {
this.operationsService = operationsService;
}
@GetMapping("/sum")
public Object sum(
@RequestParam(value = "value1", defaultValue = "0") String value1,
@RequestParam(value = "value2", defaultValue = "0") String value2,
@RequestParam(value = "ops", defaultValue = "intops") String ops
) {
return operationsService.add(value1, value2, ops);
} }
@GetMapping("/sub") @GetMapping("/sub")
public int sub( public Object sub(
@RequestParam(value = "num1", defaultValue = "0") int num1, @RequestParam(value = "value1", defaultValue = "0") String value1,
@RequestParam(value = "num2", defaultValue = "0") int num2 @RequestParam(value = "value2", defaultValue = "0") String value2,
@RequestParam(value = "ops", defaultValue = "intops") String ops
) { ) {
return num1 - num2; return operationsService.sub(value1, value2, ops);
} }
@GetMapping("/mul") @GetMapping("/mul")
public int mul( public Object mul(
@RequestParam(value = "num1", defaultValue = "0") int num1, @RequestParam(value = "value", defaultValue = "0") String value,
@RequestParam(value = "num2", defaultValue = "0") int num2 @RequestParam(value = "count", defaultValue = "0") int count,
@RequestParam(value = "ops", defaultValue = "intops") String ops
) { ) {
return num1 * num2; return operationsService.mul(value, count, ops);
} }
@GetMapping("/div") @GetMapping("/invert")
public int div( public Object div(
@RequestParam(value = "num1", defaultValue = "0") int num1, @RequestParam(value = "value", defaultValue = "0") String value,
@RequestParam(value = "num2", defaultValue = "0") int num2 @RequestParam(value = "ops", defaultValue = "intops") String ops
) { ) {
return num1 / num2; return operationsService.invert(value, ops);
} }
} }

View File

@ -0,0 +1,25 @@
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

@ -0,0 +1,8 @@
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

@ -0,0 +1,30 @@
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

@ -0,0 +1,45 @@
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

@ -0,0 +1,67 @@
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

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