Lab2
This commit is contained in:
parent
8157875d1e
commit
2ba5860a70
@ -8,15 +8,48 @@
|
|||||||
<script src="main.js"></script>
|
<script src="main.js"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h3 id="date" class="text-center my-3 "></h3>
|
|
||||||
|
|
||||||
<input id="ilovetextfield">
|
<h3 class="my-3 ms-3"> LabWork01 </h3>
|
||||||
<button id = "ilovebutton" onclick="iloveclick()">Готово</button>
|
|
||||||
|
|
||||||
<input id="asktextfield">
|
<input class="my-3 ms-3" id="ilovetextfield">
|
||||||
<button id = "askbutton" onclick="askclick()">Спросить</button>
|
<button type="button"class="btn btn-primary my-3 ms-3" id = "ilovebutton" onclick="iloveclick()">Готово</button>
|
||||||
|
|
||||||
<input id="uppertextfield">
|
<br>
|
||||||
<button id = "upperbutton" onclick="upperclick()">Поднять</button>
|
|
||||||
|
<input class="my-3 ms-3" id="asktextfield">
|
||||||
|
<button type="button"class="btn btn-primary my-3 ms-3" id = "askbutton" onclick="askclick()">Спросить</button>
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
|
<input class="my-3 ms-3" id="uppertextfield">
|
||||||
|
<button type="button"class="btn btn-primary my-3 ms-3" id = "upperbutton" onclick="upperclick()">Поднять</button>
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
|
<h3 class="ms-3 my-3"> LabWork02</h3>
|
||||||
|
|
||||||
|
<input class="my-3 ms-3" id="sum_first_tf">
|
||||||
|
<span> + </span>
|
||||||
|
<input class="my-3" id="sum_second_tf">
|
||||||
|
<button type="button"class="btn btn-primary my-3 mx-3" id="sum_button" onclick="sumLab2('String')"> Сумма строк</button>
|
||||||
|
<button type="button"class="btn btn-primary my-3 mx-3" id="sum_button" onclick="sumLab2('int')"> Сумма чисел</button>
|
||||||
|
<span> = </span>
|
||||||
|
<span class="border border-primary my-3 ms-3 p-3" id="sum_result">Тут будет результат</span>
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
|
<input class="my-3 ms-3" id="makeitbigger_tf">
|
||||||
|
<button type="button"class="btn btn-primary my-3 ms-3" id="makeitbigger_button" onclick="makeItBiggerLab2('String')">Увеличить строку</button>
|
||||||
|
<button type="button"class="btn btn-primary my-3 ms-3" id="makeitbigger_button" onclick="makeItBiggerLab2('int')">Увеличить число</button>
|
||||||
|
<br>
|
||||||
|
|
||||||
|
<input class="my-3 ms-3" id="reverse_tf">
|
||||||
|
<button type="button"class="btn btn-primary my-3 ms-3" id="reverse_button" onclick="reverseLab2('String')">Наоборот строка</button>
|
||||||
|
<button type="button"class="btn btn-primary my-3 ms-3" id="reverse_button" onclick="reverseLab2('int')">Наоборот число</button>
|
||||||
|
<br>
|
||||||
|
|
||||||
|
<input class="my-3 ms-3" id="lenght_tf">
|
||||||
|
<button type="button"class="btn btn-primary my-3 ms-3" id="lenght_button" onclick="lenghtLab2('String')">Длина строки</button>
|
||||||
|
<button type="button"class="btn btn-primary my-3 ms-3" id="lenght_button" onclick="lenghtLab2('int')">Длина числа</button>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
@ -1,9 +1,43 @@
|
|||||||
fetch("http://127.0.0.1:8080/date")
|
function sumLab2(type) {
|
||||||
|
var first = document.getElementById("sum_first_tf").value
|
||||||
|
var second = document.getElementById("sum_second_tf").value
|
||||||
|
fetch("http://127.0.0.1:8080/lab2/sum?" + "first=" + first + "&second=" + second + "&type=" + type)
|
||||||
.then(response => response.text())
|
.then(response => response.text())
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
console.log(response)
|
console.log(response)
|
||||||
document.getElementById("date").textContent = response
|
document.getElementById("sum_result").textContent=response
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeItBiggerLab2(type) {
|
||||||
|
var value = document.getElementById("makeitbigger_tf").value
|
||||||
|
fetch("http://127.0.0.1:8080/lab2/makeitbigger?small="+value + "&type=" + type)
|
||||||
|
.then(response => response.text())
|
||||||
|
.then((response) => {
|
||||||
|
console.log(response)
|
||||||
|
document.getElementById("makeitbigger_tf").value = response
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function reverseLab2(type) {
|
||||||
|
var value = document.getElementById("reverse_tf").value
|
||||||
|
fetch("http://127.0.0.1:8080/lab2/reverse?value="+value + "&type=" + type)
|
||||||
|
.then(response => response.text())
|
||||||
|
.then((response) => {
|
||||||
|
console.log(response)
|
||||||
|
document.getElementById("reverse_tf").value = response
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function lenghtLab2(type) {
|
||||||
|
var value = document.getElementById("lenght_tf").value
|
||||||
|
fetch("http://127.0.0.1:8080/lab2/lenght?value="+value + "&type=" + type)
|
||||||
|
.then(response => response.text())
|
||||||
|
.then((response) => {
|
||||||
|
console.log(response)
|
||||||
|
document.getElementById("lenght_tf").value = response
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
function iloveclick() {
|
function iloveclick() {
|
||||||
var text = document.getElementById("ilovetextfield").value
|
var text = document.getElementById("ilovetextfield").value
|
||||||
@ -34,3 +68,4 @@ function upperclick() {
|
|||||||
document.getElementById("uppertextfield").value = response
|
document.getElementById("uppertextfield").value = response
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,9 +6,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class Lab1Application {
|
public class Lab1Application {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(Lab1Application.class, args);
|
SpringApplication.run(Lab1Application.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
20
src/main/java/com/webproglabs/lab1/config/Lab2Config.java
Normal file
20
src/main/java/com/webproglabs/lab1/config/Lab2Config.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package com.webproglabs.lab1.config;
|
||||||
|
|
||||||
|
import com.webproglabs.lab1.domain.Lab2Int;
|
||||||
|
import com.webproglabs.lab1.domain.Lab2String;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class Lab2Config {
|
||||||
|
|
||||||
|
@Bean(value="int")
|
||||||
|
public Lab2Int createLab2Int() {
|
||||||
|
return new Lab2Int();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(value="String")
|
||||||
|
public Lab2String createLab2String() {
|
||||||
|
return new Lab2String();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,35 @@
|
|||||||
|
package com.webproglabs.lab1.controllers;
|
||||||
|
|
||||||
|
import com.webproglabs.lab1.services.Lab2Service;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class Lab2Controller {
|
||||||
|
private final Lab2Service lab2Service;
|
||||||
|
|
||||||
|
public Lab2Controller(Lab2Service lab2Service) {this.lab2Service = lab2Service;}
|
||||||
|
|
||||||
|
@GetMapping("/lab2/sum")
|
||||||
|
public Object sum(@RequestParam (value = "first", defaultValue = "hello") String first,
|
||||||
|
@RequestParam (value = "second", defaultValue = "world") String second,
|
||||||
|
@RequestParam (value = "type") String type) {
|
||||||
|
return lab2Service.sum(first, second, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/lab2/makeitbigger")
|
||||||
|
public Object makeItBigger(@RequestParam (value = "small") String small, @RequestParam (value = "type") String type) {
|
||||||
|
return lab2Service.makeItBigger(small, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/lab2/reverse")
|
||||||
|
public Object reverse(@RequestParam(value="value")String value, @RequestParam (value = "type") String type) {
|
||||||
|
return lab2Service.reverse(value, type);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("lab2/lenght")
|
||||||
|
public Object lenght(@RequestParam(value = "value") String value, @RequestParam (value = "type") String type) {
|
||||||
|
return lab2Service.lenght(value, type);
|
||||||
|
}
|
||||||
|
}
|
58
src/main/java/com/webproglabs/lab1/domain/Lab2Int.java
Normal file
58
src/main/java/com/webproglabs/lab1/domain/Lab2Int.java
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package com.webproglabs.lab1.domain;
|
||||||
|
|
||||||
|
public class Lab2Int implements Lab2Interface<Integer>{
|
||||||
|
// @Override
|
||||||
|
// public Object sum(Object first, Object second) {
|
||||||
|
// if (!(first instanceof Integer && second instanceof Integer)) {
|
||||||
|
// throw new IllegalArgumentException();
|
||||||
|
// }
|
||||||
|
// return (Integer)first + (Integer)second;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public Object makeItBigger(Object small) {
|
||||||
|
// if (!(small instanceof Integer)) {
|
||||||
|
// throw new IllegalArgumentException();
|
||||||
|
// }
|
||||||
|
// return (Integer)small * (Integer)small;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public Object reverse(Object value) {
|
||||||
|
// if (!(value instanceof Integer)) {
|
||||||
|
// throw new IllegalArgumentException();
|
||||||
|
// }
|
||||||
|
// return (Integer)value * (-1);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public Object lenght(Object value) {
|
||||||
|
// if (!(value instanceof Integer)) {
|
||||||
|
// throw new IllegalArgumentException();
|
||||||
|
// }
|
||||||
|
// int newvalue = (Integer)value;
|
||||||
|
// if (newvalue < 0) newvalue = newvalue * (-1);
|
||||||
|
// return newvalue;
|
||||||
|
// }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer sum(Integer first, Integer second) {
|
||||||
|
return first + second;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer makeItBigger(Integer small) {
|
||||||
|
return small*small;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer reverse(Integer value) {
|
||||||
|
return value*(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer lenght(Integer value) {
|
||||||
|
if (value < 0) return value *(-1);
|
||||||
|
else return value;
|
||||||
|
}
|
||||||
|
}
|
11
src/main/java/com/webproglabs/lab1/domain/Lab2Interface.java
Normal file
11
src/main/java/com/webproglabs/lab1/domain/Lab2Interface.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package com.webproglabs.lab1.domain;
|
||||||
|
|
||||||
|
public interface Lab2Interface<T> {
|
||||||
|
T sum(T first, T second);
|
||||||
|
|
||||||
|
T makeItBigger (T small);
|
||||||
|
|
||||||
|
T reverse (T value);
|
||||||
|
|
||||||
|
T lenght (T value);
|
||||||
|
}
|
61
src/main/java/com/webproglabs/lab1/domain/Lab2String.java
Normal file
61
src/main/java/com/webproglabs/lab1/domain/Lab2String.java
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
package com.webproglabs.lab1.domain;
|
||||||
|
|
||||||
|
public class Lab2String implements Lab2Interface<String>{
|
||||||
|
// @Override
|
||||||
|
// public Object sum(Object first, Object second) {
|
||||||
|
// if (!(first instanceof String && second instanceof String)) {
|
||||||
|
// throw new IllegalArgumentException();
|
||||||
|
// }
|
||||||
|
// return ((String) first).concat((String)second);
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public Object makeItBigger(Object small) {
|
||||||
|
// if (!(small instanceof String)) {
|
||||||
|
// throw new IllegalArgumentException();
|
||||||
|
// }
|
||||||
|
// return ((String) small).toUpperCase();
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public Object reverse(Object value) {
|
||||||
|
// if (!(value instanceof String)) {
|
||||||
|
// throw new IllegalArgumentException();
|
||||||
|
// }
|
||||||
|
// StringBuilder sb = new StringBuilder((String)value);
|
||||||
|
// sb.reverse();
|
||||||
|
// return sb.toString();
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// @Override
|
||||||
|
// public Object lenght(Object value) {
|
||||||
|
// if (!(value instanceof String)) {
|
||||||
|
// throw new IllegalArgumentException();
|
||||||
|
// }
|
||||||
|
// return ((String) value).length();
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String sum(String first, String second) {
|
||||||
|
return first.concat(second);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String makeItBigger(String small) {
|
||||||
|
return small.toUpperCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String reverse(String value) {
|
||||||
|
StringBuilder sb = new StringBuilder(value);
|
||||||
|
sb.reverse();
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String lenght(String value) {
|
||||||
|
int len = value.length();
|
||||||
|
return Integer.toString(len);
|
||||||
|
}
|
||||||
|
}
|
47
src/main/java/com/webproglabs/lab1/services/Lab2Service.java
Normal file
47
src/main/java/com/webproglabs/lab1/services/Lab2Service.java
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package com.webproglabs.lab1.services;
|
||||||
|
|
||||||
|
import com.webproglabs.lab1.domain.Lab2Interface;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class Lab2Service {
|
||||||
|
private final ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
public Lab2Service(ApplicationContext applicationContext) {
|
||||||
|
this.applicationContext = applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object sum(Object first, Object second, String type) {
|
||||||
|
if (type.equals("int")) {
|
||||||
|
first = Integer.parseInt(first.toString());
|
||||||
|
second = Integer.parseInt(second.toString());
|
||||||
|
}
|
||||||
|
final Lab2Interface summator =(Lab2Interface)applicationContext.getBean(type);
|
||||||
|
return summator.sum(first, second);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object makeItBigger(Object small, String type) {
|
||||||
|
if (type.equals("int")) {
|
||||||
|
small = Integer.parseInt(small.toString());
|
||||||
|
}
|
||||||
|
final Lab2Interface biggernator = (Lab2Interface)applicationContext.getBean(type);
|
||||||
|
return biggernator.makeItBigger(small);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object reverse (Object value, String type) {
|
||||||
|
if (type.equals("int")) {
|
||||||
|
value = Integer.parseInt(value.toString());
|
||||||
|
}
|
||||||
|
final Lab2Interface reversenator = (Lab2Interface) applicationContext.getBean(type);
|
||||||
|
return reversenator.reverse(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object lenght (Object value, String type) {
|
||||||
|
if (type.equals("int")) {
|
||||||
|
value = Integer.parseInt(value.toString());
|
||||||
|
}
|
||||||
|
final Lab2Interface leghtgetter = (Lab2Interface) applicationContext.getBean(type);
|
||||||
|
return leghtgetter.lenght(value);
|
||||||
|
}
|
||||||
|
}
|
@ -1,13 +1,63 @@
|
|||||||
package com.webproglabs.lab1;
|
package com.webproglabs.lab1;
|
||||||
|
|
||||||
|
import com.webproglabs.lab1.services.Lab2Service;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
class Lab1ApplicationTests {
|
class Lab1ApplicationTests {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
Lab2Service lab2Service;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void contextLoads() {
|
void testSumInt() {
|
||||||
|
final int res = (Integer)lab2Service.sum(3, 5, "int");
|
||||||
|
Assertions.assertEquals(8, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSumString() {
|
||||||
|
final String res = (String)lab2Service.sum("Hello", "World", "String");
|
||||||
|
Assertions.assertEquals("HelloWorld", res);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBigInt() {
|
||||||
|
final int res = (Integer)lab2Service.makeItBigger(4, "int");
|
||||||
|
Assertions.assertEquals(16, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testBigString() {
|
||||||
|
final String res = (String)lab2Service.makeItBigger("hello", "String");
|
||||||
|
Assertions.assertEquals("HELLO", res);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testReverseInt() {
|
||||||
|
final int res = (Integer)lab2Service.reverse(17, "int");
|
||||||
|
Assertions.assertEquals(-17, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testReverseString() {
|
||||||
|
final String res = (String)lab2Service.reverse("hello", "String");
|
||||||
|
Assertions.assertEquals("olleh", res);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testLenghtInt() {
|
||||||
|
final int res = (Integer)lab2Service.lenght(17, "int");
|
||||||
|
Assertions.assertEquals(17, res);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testLenghtString() {
|
||||||
|
final String res = (String)lab2Service.lenght("hello", "String");
|
||||||
|
Assertions.assertEquals("5", res);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user