Lab2
This commit is contained in:
parent
c3ce1336cb
commit
8315a23855
@ -2,40 +2,63 @@
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title>Калькулятор</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">
|
||||
<title>Calculator</title>
|
||||
<link rel="stylesheet" href="https://unpkg.com/chota@latest">
|
||||
</head>
|
||||
|
||||
<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="m-auto dflex flex-row align-items-center">
|
||||
<div class="col-sm-3 my-1">
|
||||
<input type="number" class="form-control" id="num1">
|
||||
</div>
|
||||
<div class="col-sm-3 my-1">
|
||||
<select class="form-control" id="operation">
|
||||
<option value="sum" selected>+</option>
|
||||
<option value="sub">-</option>
|
||||
<option value="mul">*</option>
|
||||
<option value="div">/</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-sm-3 my-1">
|
||||
<input type="number" class="form-control" id="num2">
|
||||
</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 class="col-sm-3 my-1">
|
||||
<p class="h5" id="result"></p>
|
||||
</div>
|
||||
<div class="container" style="max-width:600px">
|
||||
<div class="card row is-full-screen is-center">
|
||||
<div class="col">
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<input type="text" id="value1">
|
||||
</div>
|
||||
<div class="col">
|
||||
<select id="operation">
|
||||
<option value="sum" selected>+</option>
|
||||
<option value="sub">-</option>
|
||||
<option value="mul">*</option>
|
||||
<option value="invert">~</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="col">
|
||||
<input type="text" id="value2">
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<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>
|
||||
|
||||
<script>
|
||||
async function calculate() {
|
||||
const num1 = document.getElementById("num1").value
|
||||
const num2 = document.getElementById("num2").value
|
||||
document.getElementById("result").innerHTML = await (await fetch(`http://127.0.0.1:8080/${document.getElementById("operation").value}?num1=${num1}&num2=${num2}`)).text()
|
||||
async function calculate(ops) {
|
||||
const value1 = document.getElementById("value1").value
|
||||
const value2 = document.getElementById("value2").value
|
||||
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>
|
||||
</body>
|
||||
|
@ -3,7 +3,7 @@ const http = require('http')
|
||||
|
||||
const requestListener = async function (req, res) {
|
||||
res.writeHead(200);
|
||||
fs.readFile('index.html', (err, data) => {
|
||||
fs.readFile('index.html', 'utf8', (err, data) => {
|
||||
res.end(data);
|
||||
});
|
||||
};
|
||||
|
@ -1,41 +1,50 @@
|
||||
package np.something.controllers;
|
||||
|
||||
import np.something.services.OperationsService;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class MathController {
|
||||
@GetMapping("/sum")
|
||||
public int sum(
|
||||
@RequestParam(value = "num1", defaultValue = "0") int num1,
|
||||
@RequestParam(value = "num2", defaultValue = "0") int num2
|
||||
) {
|
||||
private final OperationsService operationsService;
|
||||
|
||||
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")
|
||||
public int sub(
|
||||
@RequestParam(value = "num1", defaultValue = "0") int num1,
|
||||
@RequestParam(value = "num2", defaultValue = "0") int num2
|
||||
public Object sub(
|
||||
@RequestParam(value = "value1", defaultValue = "0") String value1,
|
||||
@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")
|
||||
public int mul(
|
||||
@RequestParam(value = "num1", defaultValue = "0") int num1,
|
||||
@RequestParam(value = "num2", defaultValue = "0") int num2
|
||||
public Object mul(
|
||||
@RequestParam(value = "value", defaultValue = "0") String value,
|
||||
@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")
|
||||
public int div(
|
||||
@RequestParam(value = "num1", defaultValue = "0") int num1,
|
||||
@RequestParam(value = "num2", defaultValue = "0") int num2
|
||||
@GetMapping("/invert")
|
||||
public Object div(
|
||||
@RequestParam(value = "value", defaultValue = "0") String value,
|
||||
@RequestParam(value = "ops", defaultValue = "intops") String ops
|
||||
) {
|
||||
return num1 / num2;
|
||||
return operationsService.invert(value, ops);
|
||||
}
|
||||
}
|
||||
|
25
src/main/java/np/something/model/IntegerOperations.java
Normal file
25
src/main/java/np/something/model/IntegerOperations.java
Normal 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;
|
||||
}
|
||||
}
|
8
src/main/java/np/something/model/Operations.java
Normal file
8
src/main/java/np/something/model/Operations.java
Normal 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);
|
||||
}
|
30
src/main/java/np/something/model/StringOperations.java
Normal file
30
src/main/java/np/something/model/StringOperations.java
Normal 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();
|
||||
}
|
||||
}
|
45
src/main/java/np/something/services/OperationsService.java
Normal file
45
src/main/java/np/something/services/OperationsService.java
Normal 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);
|
||||
}
|
||||
}
|
67
src/test/java/np/something/OperationsServiceTests.java
Normal file
67
src/test/java/np/something/OperationsServiceTests.java
Normal 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"));
|
||||
}
|
||||
}
|
@ -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() {
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user