LabWork02: Вроде работает
This commit is contained in:
parent
d84000b5b4
commit
eada9f3c88
@ -3,27 +3,61 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Calc</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>
|
||||
|
||||
<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>
|
||||
<input type="number" class="form-control" id="num1">
|
||||
<select class="form-control" id="operation">
|
||||
<option value="sum">+</option>
|
||||
<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="val1">
|
||||
</div>
|
||||
<div class="col">
|
||||
<select id="operation">
|
||||
<option value="sum" selected>+</option>
|
||||
<option value="sub">-</option>
|
||||
<option value="mul">*</option>
|
||||
<option value="div">/</option>
|
||||
</select>
|
||||
<input type="number" class="form-control" id="num2">
|
||||
<button type="button" class="form-control btn btn-light" id="get-result" onclick="calc()">=</button>
|
||||
<p class="h5" id="result"></p>
|
||||
</div>
|
||||
<div class="col">
|
||||
<input type="text" id="val2">
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col">
|
||||
<button type="button" id="get-int-result" onclick="calculate('inttype')">Result for int</button>
|
||||
</div>
|
||||
<div class="col is-right">
|
||||
<button type="button" id="get-string-result" onclick="calculate('stringtype')">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 calc() {
|
||||
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(resulttype) {
|
||||
const val1 = document.getElementById("val1").value
|
||||
const val2 = document.getElementById("val2").value
|
||||
const op = document.getElementById("operation").value
|
||||
if (op == 'sum') {
|
||||
document.getElementById("result").innerHTML = await (await fetch(`http://127.0.0.1:8080/sum?val1=${val1}&val2=${val2}&type=${resulttype}`)).text()
|
||||
} else if (op == 'sub') {
|
||||
document.getElementById("result").innerHTML = await (await fetch(`http://127.0.0.1:8080/sub?val1=${val1}&val2=${val2}&type=${resulttype}`)).text()
|
||||
} else if (op == 'mul') {
|
||||
document.getElementById("result").innerHTML = await (await fetch(`http://127.0.0.1:8080/mul?val1=${val1}&val2=${val2}&type=${resulttype}`)).text()
|
||||
} else {
|
||||
document.getElementById("result").innerHTML = await (await fetch(`http://127.0.0.1:8080/div?val1=${val1}&val2=${val2}&type=${resulttype}`)).text()
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
@ -3,21 +3,32 @@ package ru.ulstu.is.labwork;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@SpringBootApplication
|
||||
@RestController
|
||||
//@RequestMapping("/lab1")
|
||||
public class LabworkApplication {
|
||||
//инимум 4 операции разные интерфейс с дженерик использ сервис
|
||||
//2 3 реализации с разными данными
|
||||
//писать контроллеры и делать интерфейс
|
||||
//озвращ свой тип данных обджект
|
||||
// написать набор тестов для кажд метода 12 тестов
|
||||
|
||||
//создать интерфейс 4 метода реализ какие то операц над данными
|
||||
//написать 2-3 реализации
|
||||
//каждая соответственно делает то что мы хотим с данными (реализ строка число)
|
||||
//потом пишем сервис (можно из примера) из поиска берем нужную реализацию и делаем действия
|
||||
//пишем констролер чтоб извне...
|
||||
//пишем интерфес на жабе
|
||||
//и написать тесты для каждого метода чтобы все проверить
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(LabworkApplication.class, args);
|
||||
}
|
||||
|
||||
@GetMapping("/good_morning")
|
||||
public String good_morning(@RequestParam(value = "name", defaultValue = "Sir") String name) {
|
||||
return String.format("Good morning, %s!", name);
|
||||
}
|
||||
@GetMapping("/sum")
|
||||
/*@GetMapping("/sum")
|
||||
public int sum( @RequestParam(value = "num1", defaultValue = "0") int num1,
|
||||
@RequestParam(value = "num2", defaultValue = "0") int num2)
|
||||
{
|
||||
@ -40,5 +51,5 @@ public class LabworkApplication {
|
||||
@RequestParam(value = "num2", defaultValue = "0") int num2)
|
||||
{
|
||||
return num1 / num2;
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
@ -0,0 +1,20 @@
|
||||
package ru.ulstu.is.labwork.calculator.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import ru.ulstu.is.labwork.calculator.domain.Calculator;
|
||||
import ru.ulstu.is.labwork.calculator.domain.CalculatorInt;
|
||||
import ru.ulstu.is.labwork.calculator.domain.CalculatorString;
|
||||
|
||||
@Configuration
|
||||
public class CalculatorConfiguration {
|
||||
@Bean(value = "inttype")
|
||||
public CalculatorInt createIntCalculator(){
|
||||
return new CalculatorInt();
|
||||
}
|
||||
|
||||
@Bean(value = "stringtype")
|
||||
public CalculatorString createStringCalculator(){
|
||||
return new CalculatorString();
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package ru.ulstu.is.labwork.calculator.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.ulstu.is.labwork.calculator.service.CalculatorService;
|
||||
|
||||
@RestController
|
||||
//@RequestMapping("/lab2")
|
||||
public class CalculatorController {
|
||||
private final CalculatorService calculatorService;
|
||||
|
||||
public CalculatorController(CalculatorService calculatorService){
|
||||
this.calculatorService = calculatorService;
|
||||
}
|
||||
|
||||
@GetMapping("/sum")
|
||||
public String sum( @RequestParam(value = "val1", defaultValue = "0") Object val1,
|
||||
@RequestParam(value = "val2", defaultValue = "0") Object val2,
|
||||
@RequestParam(value = "type", defaultValue = "inttype") String type)
|
||||
{
|
||||
return calculatorService.sum(val1, val2, type).toString();
|
||||
}
|
||||
@GetMapping("/sub")
|
||||
public String sub( @RequestParam(value = "val1", defaultValue = "0") Object val1,
|
||||
@RequestParam(value = "val2", defaultValue = "0") Object val2,
|
||||
@RequestParam(value = "type", defaultValue = "inttype") String type)
|
||||
{
|
||||
return calculatorService.sub(val1, val2, type).toString();
|
||||
}
|
||||
@GetMapping("/mul")
|
||||
public String mul( @RequestParam(value = "val1", defaultValue = "0") Object val1,
|
||||
@RequestParam(value = "val2", defaultValue = "0") Object val2,
|
||||
@RequestParam(value = "type", defaultValue = "inttype") String type)
|
||||
{
|
||||
return calculatorService.mul(val1, val2, type).toString();
|
||||
}
|
||||
@GetMapping("/div")
|
||||
public String div( @RequestParam(value = "val1", defaultValue = "0") Object val1,
|
||||
@RequestParam(value = "val2", defaultValue = "0") Object val2,
|
||||
@RequestParam(value = "type", defaultValue = "inttype") String type)
|
||||
{
|
||||
return calculatorService.div(val1, val2, type).toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package ru.ulstu.is.labwork.calculator.domain;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
public interface Calculator {
|
||||
Object sum(Object val1, Object val2);
|
||||
Object sub(Object val1, Object val2);
|
||||
Object mul(Object val1, Object val2);
|
||||
Object div(Object val1, Object val2);
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package ru.ulstu.is.labwork.calculator.domain;
|
||||
|
||||
public class CalculatorInt implements Calculator{
|
||||
|
||||
@Override
|
||||
public Object sum(Object val1, Object val2) {
|
||||
if (val1 instanceof Integer && val2 instanceof Integer){
|
||||
return (Integer)val1 + (Integer)val2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object sub(Object val1, Object val2) {
|
||||
if (val1 instanceof Integer && val2 instanceof Integer){
|
||||
return (Integer)val1 - (Integer)val2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object mul(Object val1, Object val2) {
|
||||
if (val1 instanceof Integer && val2 instanceof Integer){
|
||||
return (Integer)val1 * (Integer)val2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object div(Object val1, Object val2) {
|
||||
if (val1 instanceof Integer && val2 instanceof Integer){
|
||||
return (Integer)val1 / (Integer)val2;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package ru.ulstu.is.labwork.calculator.domain;
|
||||
|
||||
public class CalculatorString implements Calculator{
|
||||
@Override
|
||||
public Object sum(Object val1, Object val2) {
|
||||
return (String)val1 + '+' + (String)val2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object sub(Object val1, Object val2) {
|
||||
return (String)val1 + '-' + (String)val2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object mul(Object val1, Object val2) {
|
||||
return (String)val1 + '*' + (String)val2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object div(Object val1, Object val2) {
|
||||
return (String)val1 + '/' + (String)val2;
|
||||
}
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package ru.ulstu.is.labwork.calculator.service;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Service;
|
||||
import ru.ulstu.is.labwork.calculator.domain.Calculator;
|
||||
|
||||
@Service
|
||||
public class CalculatorService {
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
public CalculatorService(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
public Object sum(Object val1, Object val2, String type){
|
||||
final Calculator calculator = (Calculator) applicationContext.getBean(type);
|
||||
if (type.compareTo("inttype") == 0){
|
||||
return calculator.sum(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString()));
|
||||
}
|
||||
else if (type.compareTo("stringtype") == 0) {
|
||||
return String.format("%s", calculator.sum(val1.toString(), val2.toString()));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object sub(Object val1, Object val2, String type){
|
||||
final Calculator calculator = (Calculator) applicationContext.getBean(type);
|
||||
if (type.compareTo("inttype") == 0){
|
||||
return calculator.sub(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString()));
|
||||
}
|
||||
else if (type.compareTo("stringtype") == 0) {
|
||||
return String.format("%s", calculator.sub(val1.toString(), val2.toString()));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public Object mul(Object val1, Object val2, String type){
|
||||
final Calculator calculator = (Calculator) applicationContext.getBean(type);
|
||||
if (type.compareTo("inttype") == 0){
|
||||
return calculator.mul(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString()));
|
||||
}
|
||||
else if (type.compareTo("stringtype") == 0) {
|
||||
return String.format("%s", calculator.mul(val1.toString(), val2.toString()));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public Object div(Object val1, Object val2, String type){
|
||||
final Calculator calculator = (Calculator) applicationContext.getBean(type);
|
||||
if (type.compareTo("inttype") == 0){
|
||||
return calculator.div(Integer.parseInt(val1.toString()), Integer.parseInt(val2.toString()));
|
||||
}
|
||||
else if (type.compareTo("stringtype") == 0) {
|
||||
return String.format("%s", calculator.div(val1.toString(), val2.toString()));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -1,13 +1,69 @@
|
||||
package ru.ulstu.is.labwork;
|
||||
|
||||
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;
|
||||
import ru.ulstu.is.labwork.calculator.service.CalculatorService;
|
||||
|
||||
@SpringBootTest
|
||||
class LabworkApplicationTests {
|
||||
|
||||
@Autowired
|
||||
CalculatorService calculatorService;
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
void stringSum() {
|
||||
Assertions.assertEquals("Hello+World!", calculatorService.sum("Hello", "World!", "stringtype"));
|
||||
Assertions.assertEquals("+Hello, World!", calculatorService.sum("", "Hello, World!", "stringtype"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void stringSub() {
|
||||
Assertions.assertEquals("Крокодил-Гена", calculatorService.sub("Крокодил", "Гена", "stringtype"));
|
||||
Assertions.assertEquals("Атомное-сердце", calculatorService.sub("Атомное", "сердце", "stringtype"));
|
||||
Assertions.assertEquals("Почему-Жак Фреско", calculatorService.sub("Почему", "Жак Фреско", "stringtype"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void stringMul() {
|
||||
Assertions.assertEquals("Это*Звездочка", calculatorService.mul("Это", "Звездочка", "stringtype"));
|
||||
Assertions.assertEquals("п*чему", calculatorService.mul("п", "чему", "stringtype"));
|
||||
Assertions.assertEquals("Звездочкой * обозначают умножение", calculatorService.mul("Звездочкой ", " обозначают умножение", "stringtype"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void stringDiv() {
|
||||
Assertions.assertEquals("б/п", calculatorService.div("б", "п", "stringtype"));
|
||||
Assertions.assertEquals("/1", calculatorService.div("", "1", "stringtype"));
|
||||
Assertions.assertEquals("-/-", calculatorService.div("-", "-", "stringtype"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void intSum() {
|
||||
Assertions.assertEquals(22, calculatorService.sum(11, 11, "inttype"));
|
||||
Assertions.assertEquals(-10, calculatorService.sum(15, -25, "inttype"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void intSub() {
|
||||
Assertions.assertEquals(0, calculatorService.sub(10, 10, "inttype"));
|
||||
Assertions.assertEquals(100, calculatorService.sub(100, 0, "inttype"));
|
||||
Assertions.assertEquals(3, calculatorService.sub(-3, -6, "inttype"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void intMul() {
|
||||
Assertions.assertEquals(0, calculatorService.mul(0, 10, "inttype"));
|
||||
Assertions.assertEquals(18, calculatorService.mul(6, 3, "inttype"));
|
||||
Assertions.assertEquals(-4, calculatorService.mul(-1, 4, "inttype"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void intDiv() {
|
||||
Assertions.assertEquals(-1, calculatorService.div(-1, 1, "inttype"));
|
||||
Assertions.assertEquals(2, calculatorService.div(100, 50, "inttype"));
|
||||
Assertions.assertEquals(8, calculatorService.div(64, 8, "inttype"));
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user