diff --git a/front/index.html b/front/index.html index cdc1497..782e923 100644 --- a/front/index.html +++ b/front/index.html @@ -8,15 +8,48 @@ -

- - +

LabWork01

- - + + - - +
+ + + + +
+ + + + +
+ +

LabWork02

+ + + + + + + + = + Тут будет результат + +
+ + + + +
+ + + + +
+ + + + \ No newline at end of file diff --git a/front/main.js b/front/main.js index 323c81c..e8f07dd 100644 --- a/front/main.js +++ b/front/main.js @@ -1,9 +1,43 @@ -fetch("http://127.0.0.1:8080/date") -.then(response => response.text()) - .then((response) => { - console.log(response) - document.getElementById("date").textContent = response - }) +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) => { + console.log(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() { var text = document.getElementById("ilovetextfield").value @@ -33,4 +67,5 @@ function upperclick() { console.log(response) document.getElementById("uppertextfield").value = response }) -} \ No newline at end of file +} + diff --git a/src/main/java/com/webproglabs/lab1/Lab1Application.java b/src/main/java/com/webproglabs/lab1/Lab1Application.java index 0495784..6994b30 100644 --- a/src/main/java/com/webproglabs/lab1/Lab1Application.java +++ b/src/main/java/com/webproglabs/lab1/Lab1Application.java @@ -6,9 +6,7 @@ import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class Lab1Application { - public static void main(String[] args) { SpringApplication.run(Lab1Application.class, args); } - } diff --git a/src/main/java/com/webproglabs/lab1/config/Lab2Config.java b/src/main/java/com/webproglabs/lab1/config/Lab2Config.java new file mode 100644 index 0000000..70de7a8 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/config/Lab2Config.java @@ -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(); + } +} diff --git a/src/main/java/com/webproglabs/lab1/controllers/Lab2Controller.java b/src/main/java/com/webproglabs/lab1/controllers/Lab2Controller.java new file mode 100644 index 0000000..e7a65a1 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/controllers/Lab2Controller.java @@ -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); + } +} diff --git a/src/main/java/com/webproglabs/lab1/domain/Lab2Int.java b/src/main/java/com/webproglabs/lab1/domain/Lab2Int.java new file mode 100644 index 0000000..5c23936 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/domain/Lab2Int.java @@ -0,0 +1,58 @@ +package com.webproglabs.lab1.domain; + +public class Lab2Int implements Lab2Interface{ +// @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; + } +} diff --git a/src/main/java/com/webproglabs/lab1/domain/Lab2Interface.java b/src/main/java/com/webproglabs/lab1/domain/Lab2Interface.java new file mode 100644 index 0000000..8208b62 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/domain/Lab2Interface.java @@ -0,0 +1,11 @@ +package com.webproglabs.lab1.domain; + +public interface Lab2Interface { + T sum(T first, T second); + + T makeItBigger (T small); + + T reverse (T value); + + T lenght (T value); +} diff --git a/src/main/java/com/webproglabs/lab1/domain/Lab2String.java b/src/main/java/com/webproglabs/lab1/domain/Lab2String.java new file mode 100644 index 0000000..f16bc36 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/domain/Lab2String.java @@ -0,0 +1,61 @@ +package com.webproglabs.lab1.domain; + +public class Lab2String implements Lab2Interface{ +// @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); + } +} diff --git a/src/main/java/com/webproglabs/lab1/services/Lab2Service.java b/src/main/java/com/webproglabs/lab1/services/Lab2Service.java new file mode 100644 index 0000000..8e4a286 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/services/Lab2Service.java @@ -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); + } +} diff --git a/src/test/java/com/webproglabs/lab1/Lab1ApplicationTests.java b/src/test/java/com/webproglabs/lab1/Lab1ApplicationTests.java index e0e031b..6319173 100644 --- a/src/test/java/com/webproglabs/lab1/Lab1ApplicationTests.java +++ b/src/test/java/com/webproglabs/lab1/Lab1ApplicationTests.java @@ -1,13 +1,63 @@ package com.webproglabs.lab1; +import com.webproglabs.lab1.services.Lab2Service; +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 Lab1ApplicationTests { + @Autowired + Lab2Service lab2Service; + @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); } }