First Version

This commit is contained in:
shadowik 2023-02-21 16:06:34 +04:00
parent b4d43bf16a
commit 70d597ce77
9 changed files with 280 additions and 49 deletions

View File

@ -12,35 +12,35 @@ public class Demo1Application {
public static void main(String[] args) {
SpringApplication.run(Demo1Application.class, args);
}
@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}
@GetMapping("/div")
@ResponseBody
public double Division(@RequestParam(defaultValue = "1") double a, @RequestParam(defaultValue = "1") double b) {
if (b == 0) return 0;
return a / b;
}
@GetMapping("/mul")
@ResponseBody
public double Multiply(@RequestParam(defaultValue = "1") double a, @RequestParam(defaultValue = "1") double b) {
return a * b;
}
@GetMapping("/pow")
@ResponseBody
public double Pow(@RequestParam(defaultValue = "1") double a,
@RequestParam(defaultValue = "1") double b) {
return Math.pow(a, b);
}
@GetMapping("/plus")
@ResponseBody
public double Plus(@RequestParam(defaultValue = "1") double a,
@RequestParam(defaultValue = "1") double b) {
return a + b;
}
// @GetMapping("/hello")
// public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
// return String.format("Hello %s!", name);
// }
//
// @GetMapping("/div")
// @ResponseBody
// public double Division(@RequestParam(defaultValue = "1") double a, @RequestParam(defaultValue = "1") double b) {
// if (b == 0) return 0;
// return a / b;
// }
//
// @GetMapping("/mul")
// @ResponseBody
// public double Multiply(@RequestParam(defaultValue = "1") double a, @RequestParam(defaultValue = "1") double b) {
// return a * b;
// }
//
// @GetMapping("/pow")
// @ResponseBody
// public double Pow(@RequestParam(defaultValue = "1") double a,
// @RequestParam(defaultValue = "1") double b) {
// return Math.pow(a, b);
// }
//
// @GetMapping("/plus")
// @ResponseBody
// public double Plus(@RequestParam(defaultValue = "1") double a,
// @RequestParam(defaultValue = "1") double b) {
// return a + b;
// }
}

View File

@ -0,0 +1,20 @@
package com.example.demo.speaker.configuration;
import com.example.demo.speaker.domain.IMethods;
import com.example.demo.speaker.domain.MethodInteger;
import com.example.demo.speaker.domain.MethodString;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MethodConfiguration {
@Bean(value = "int")
public MethodInteger createIntegerMethods() {
return new MethodInteger();
}
@Bean(value = "string")
public MethodString createStringMethods() {
return new MethodString();
}
}

View File

@ -0,0 +1,54 @@
package com.example.demo.speaker.controller;
import com.example.demo.speaker.service.MethodService;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.GetMapping;
import java.io.Console;
@RestController
public class MethodController {
private final MethodService methodService;
public MethodController(MethodService methodService) {
this.methodService = methodService;
}
@GetMapping("/sum")
@ResponseBody
public String sum(@RequestParam(value = "first", defaultValue = "0") String first,
@RequestParam(value = "second", defaultValue = "0") String second,
@RequestParam(value = "type", defaultValue = "int") String type) {
Object a = first;
Object b = second;
System.out.println(a.getClass());
return methodService.Sum(a, b, type);
}
@GetMapping("/mul")
@ResponseBody
public String mul(@RequestParam(value = "first", defaultValue = "0") Object first,
@RequestParam(value = "second", defaultValue = "0") Object second,
@RequestParam(value = "type", defaultValue = "int") String type) {
return methodService.Multiply(first, second, type);
}
@GetMapping("/minus")
@ResponseBody
public String minus(@RequestParam(value = "first", defaultValue = "0") Object first,
@RequestParam(value = "second", defaultValue = "0") Object second,
@RequestParam(value = "type", defaultValue = "int") String type) {
return methodService.Minus(first, second, type);
}
@GetMapping("/cont")
@ResponseBody
public String cont(@RequestParam(value = "first", defaultValue = "0") Object first,
@RequestParam(value = "second", defaultValue = "0") Object second,
@RequestParam(value = "type", defaultValue = "int") String type) {
return methodService.Contains(first, second, type).toString();
}
}

View File

@ -0,0 +1,12 @@
package com.example.demo.speaker.domain;
public interface IMethods<T> {
T Sum(Object first, Object second);
T Multiply(Object first, Object second);
T Minus(Object first, Object second);
Boolean Contains(Object first, Object second);
}

View File

@ -0,0 +1,22 @@
package com.example.demo.speaker.domain;
public class MethodInteger implements IMethods<Integer> {
@Override
public Integer Sum(Object first, Object second) {
return Integer.parseInt((String) first) + Integer.parseInt((String) second);
}
@Override
public Integer Multiply(Object first, Object second) {
return Integer.parseInt((String) first) * Integer.parseInt((String) second);
}
@Override
public Integer Minus(Object first, Object second) {
return Integer.parseInt((String) first) - Integer.parseInt((String) second);
}
@Override
public Boolean Contains(Object first, Object second) {
return ((String)first).contains((String)second);
}
}

View File

@ -0,0 +1,30 @@
package com.example.demo.speaker.domain;
public class MethodString implements IMethods<String>{
@Override
public String Sum(Object first, Object second) {
return ((String)first).concat(((String)second));
}
@Override
public String Multiply(Object first, Object second) {
String res = ((String)first);
for (int i = 0; i < Integer.parseInt((String) second) - 1; i++) {
res = Sum(res, first);
}
return res;
}
@Override
public String Minus(Object first, Object second) {
if (Contains(first, second)) {
return ((String)first).replace((String)second, "");
}
return ((String)first);
}
@Override
public Boolean Contains(Object first, Object second) {
return ((String)first).contains((String)second);
}
}

View File

@ -0,0 +1,34 @@
package com.example.demo.speaker.service;
import com.example.demo.speaker.domain.IMethods;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
@Service
public class MethodService {
private final ApplicationContext applicationContext;
public MethodService(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public String Sum(Object first, Object second, String type) {
final IMethods method = (IMethods) applicationContext.getBean(type);
return String.format("%s", method.Sum(first, second));
}
public String Multiply(Object first, Object second, String type) {
final IMethods method = (IMethods) applicationContext.getBean(type);
return String.format("%s", method.Multiply(first, second));
}
public String Minus(Object first, Object second, String type) {
final IMethods method = (IMethods) applicationContext.getBean(type);
return String.format("%s", method.Minus(first, second));
}
public String Contains(Object first, Object second, String type) {
final IMethods method = (IMethods) applicationContext.getBean(type);
return String.format("%s", method.Contains(first, second));
}
}

View File

@ -1,13 +1,62 @@
package com.example.demo;
import com.example.demo.speaker.service.MethodService;
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 Demo1ApplicationTests {
@Autowired
MethodService methodService;
@Test
void contextLoads() {
void testMethodSumInt() {
final String res = methodService.Sum("1", "2", "int");
Assertions.assertEquals("3", res);
}
@Test
void testMethodSumString() {
final String res = methodService.Sum("1", "2", "string");
Assertions.assertEquals("12", res);
}
@Test
void testMethodMinusInt() {
final String res = methodService.Minus("1", "2", "int");
Assertions.assertEquals("-1", res);
}
@Test
void testMethodMinusString() {
final String res = methodService.Minus("214324", "4", "string");
Assertions.assertEquals("2132", res);
}
@Test
void testMethodMultInt() {
final String res = methodService.Multiply("1", "2", "int");
Assertions.assertEquals("2", res);
}
@Test
void testMethodMultString() {
final String res = methodService.Multiply("1", "2", "string");
Assertions.assertEquals("11", res);
}
@Test
void testMethodContainsInt() {
final String res = methodService.Contains("123", "2", "int");
Assertions.assertEquals("true", res);
}
@Test
void testMethodContainsString() {
final String res = methodService.Contains("1", "2", "string");
Assertions.assertEquals("false", res);
}
}

View File

@ -2,17 +2,23 @@
<form>
<div class="row data">
<div class="col">
<input v-model="val1" type="number" class="form-control" placeholder="First Value">
<input v-model="val1" type="text" class="form-control" placeholder="First Value">
</div>
<div class="col">
<input v-model="val2" type="number" class="form-control" placeholder="Second Value">
<input v-model="val2" type="text" class="form-control" placeholder="Second Value">
</div>
</div>
<div class="row data">
<select v-model="type" class="form-select" aria-label="Default select example">
<option value="int">Integer</option>
<option value="string">String</option>
</select>
</div>
<div class="d-flex justify-content-around">
<button v-on:click="div" class="btn btn-primary" type="button">Division</button>
<button v-on:click="pow" class="btn btn-primary" type="button">Power</button>
<button v-on:click="minus" class="btn btn-primary" type="button">Minus</button>
<button v-on:click="Contains" class="btn btn-primary" type="button">Contains</button>
<button v-on:click="mul" class="btn btn-primary" type="button">Multiply</button>
<button v-on:click="sum" class="btn btn-primary" type="button">Plus</button>
<button v-on:click="sum" class="btn btn-primary" type="button">Sum</button>
</div>
<div class="row data">
<div class="col">
@ -29,18 +35,20 @@ export default {
name: "Main",
data() {
return {
val1: 0,
val2: 0,
val1: "",
val2: "",
type: "int",
res: ""
}
},
methods: {
sum() {
let url = "http://localhost:8080/plus?a=" + this.val1.toString() + "&b=" + this.val2.toString();
let url = "http://localhost:8080/sum?first=" + this.val1 + "&second=" + this.val2 + "&type=" + this.type;
let vm = this;
console.log(url);
fetch(url)
.then(function (response) {
return response.json();
return response.text();
})
.then(function (data) {
console.info('Loaded');
@ -52,12 +60,12 @@ export default {
throw "Can't load items";
});
},
pow() {
let url = "http://localhost:8080/pow?a=" + this.val1.toString() + "&b=" + this.val2.toString();
Contains() {
let url = "http://localhost:8080/cont?first=" + this.val1 + "&second=" + this.val2 + "&type=" + this.type;
let vm = this;
fetch(url)
.then(function (response) {
return response.json();
return response.text();
})
.then(function (data) {
console.info('Loaded');
@ -70,11 +78,12 @@ export default {
});
},
mul() {
let url = "http://localhost:8080/mul?a=" + this.val1.toString() + "&b=" + this.val2.toString();
let url = "http://localhost:8080/mul?first=" + this.val1 + "&second=" + this.val2 + "&type=" + this.type;
let vm = this;
console.log(url);
fetch(url)
.then(function (response) {
return response.json();
return response.text();
})
.then(function (data) {
console.info('Loaded');
@ -86,12 +95,13 @@ export default {
throw "Can't load items";
});
},
div() {
let url = "http://localhost:8080/div?a=" + this.val1.toString() + "&b=" + this.val2.toString();
minus() {
let url = "http://localhost:8080/minus?first=" + this.val1 + "&second=" + this.val2 + "&type=" + this.type;
let vm = this;
console.log(url);
fetch(url)
.then(function (response) {
return response.json();
return response.text();
})
.then(function (data) {
console.info('Loaded');