LabWork02

This commit is contained in:
Overlordddd 2023-02-27 17:58:52 +04:00
parent 34939105ba
commit b2ef92b6ba
11 changed files with 267 additions and 355 deletions

View File

@ -21,18 +21,22 @@
<input type="number" class="form-control" value="0" id="first" />
<label for="second" class="form-label">num2</label>
<input type="number" class="form-control" value="0" id="second" />
<select class="form-select" id="type">
<option value="int">Integer</option>
<option value="string">String</option>
</select>
<label for="operation" class="form-label">Operation</label>
<select class="form-select" id="operation">
<option value="sum">+</option>
<option value="difference">-</option>
<option value="multiplication">*</option>
<option value="division">/</option>
<option value="cont">/</option>
</select>
<button type="button" class="btn btn-primary" id="calculate-button">Calculate</button>
<button type="button" class="btn btn-primary" id="binary-button">Binary</button>
<div>
<label for="result" class="form-label">Result</label>
<input type="number" readonly class="form-control" id="result" />
<input type="text" readonly class="form-control" id="result" />
</div>
</form>
</div>

View File

@ -1,7 +1,7 @@
{
"name": "int-prog",
"version": "1.0.0",
"lockfileVersion": 2,
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
@ -24,16 +24,6 @@
"node": ">=6"
}
},
"node_modules/@popperjs/core": {
"version": "2.11.6",
"resolved": "https://registry.npmjs.org/@popperjs/core/-/core-2.11.6.tgz",
"integrity": "sha512-50/17A98tWUfQ176raKiOGXuYpLyyVMkxxG6oylzL3BPOlA6ADGdK7EYunSa4I064xerltq9TGXs8HmOk5E+vw==",
"peer": true,
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/popperjs"
}
},
"node_modules/ansi-styles": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
@ -478,340 +468,5 @@
"node": ">=12"
}
}
},
"dependencies": {
"@fortawesome/fontawesome-free": {
"version": "6.2.0",
"resolved": "https://registry.npmjs.org/@fortawesome/fontawesome-free/-/fontawesome-free-6.2.0.tgz",
"integrity": "sha512-CNR7qRIfCwWHNN7FnKUniva94edPdyQzil/zCwk3v6k4R6rR2Fr8i4s3PM7n/lyfPA6Zfko9z5WDzFxG9SW1uQ=="
},
"@popperjs/core": {
"version": "2.11.6",
"resolved": "https://registry.npmjs.org/@popperjs/core/-/core-2.11.6.tgz",
"integrity": "sha512-50/17A98tWUfQ176raKiOGXuYpLyyVMkxxG6oylzL3BPOlA6ADGdK7EYunSa4I064xerltq9TGXs8HmOk5E+vw==",
"peer": true
},
"ansi-styles": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
"integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
"dev": true,
"requires": {
"color-convert": "^2.0.1"
}
},
"async": {
"version": "2.6.4",
"resolved": "https://registry.npmjs.org/async/-/async-2.6.4.tgz",
"integrity": "sha512-mzo5dfJYwAn29PeiJ0zvwTo04zj8HDJj0Mn8TD7sno7q12prdbnasKJHhkm2c1LgrhlJ0teaea8860oxi51mGA==",
"dev": true,
"requires": {
"lodash": "^4.17.14"
}
},
"basic-auth": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/basic-auth/-/basic-auth-2.0.1.tgz",
"integrity": "sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==",
"dev": true,
"requires": {
"safe-buffer": "5.1.2"
}
},
"bootstrap": {
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/bootstrap/-/bootstrap-5.2.1.tgz",
"integrity": "sha512-UQi3v2NpVPEi1n35dmRRzBJFlgvWHYwyem6yHhuT6afYF+sziEt46McRbT//kVXZ7b1YUYEVGdXEH74Nx3xzGA==",
"requires": {}
},
"call-bind": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.2.tgz",
"integrity": "sha512-7O+FbCihrB5WGbFYesctwmTKae6rOiIzmz1icreWJ+0aA7LJfuqhEso2T9ncpcFtzMQtzXf2QGGueWJGTYsqrA==",
"dev": true,
"requires": {
"function-bind": "^1.1.1",
"get-intrinsic": "^1.0.2"
}
},
"chalk": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
"integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
"dev": true,
"requires": {
"ansi-styles": "^4.1.0",
"supports-color": "^7.1.0"
}
},
"color-convert": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
"dev": true,
"requires": {
"color-name": "~1.1.4"
}
},
"color-name": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
"dev": true
},
"corser": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/corser/-/corser-2.0.1.tgz",
"integrity": "sha512-utCYNzRSQIZNPIcGZdQc92UVJYAhtGAteCFg0yRaFm8f0P+CPtyGyHXJcGXnffjCybUCEx3FQ2G7U3/o9eIkVQ==",
"dev": true
},
"debug": {
"version": "3.2.7",
"resolved": "https://registry.npmjs.org/debug/-/debug-3.2.7.tgz",
"integrity": "sha512-CFjzYYAi4ThfiQvizrFQevTTXHtnCqWfe7x1AhgEscTz6ZbLbfoLRLPugTQyBth6f8ZERVUSyWHFD/7Wu4t1XQ==",
"dev": true,
"requires": {
"ms": "^2.1.1"
}
},
"eventemitter3": {
"version": "4.0.7",
"resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-4.0.7.tgz",
"integrity": "sha512-8guHBZCwKnFhYdHr2ysuRWErTwhoN2X8XELRlrRwpmfeY2jjuUN4taQMsULKUVo1K4DvZl+0pgfyoysHxvmvEw==",
"dev": true
},
"follow-redirects": {
"version": "1.15.2",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz",
"integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==",
"dev": true
},
"function-bind": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz",
"integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==",
"dev": true
},
"get-intrinsic": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.0.tgz",
"integrity": "sha512-L049y6nFOuom5wGyRc3/gdTLO94dySVKRACj1RmJZBQXlbTMhtNIgkWkUHq+jYmZvKf14EW1EoJnnjbmoHij0Q==",
"dev": true,
"requires": {
"function-bind": "^1.1.1",
"has": "^1.0.3",
"has-symbols": "^1.0.3"
}
},
"has": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz",
"integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==",
"dev": true,
"requires": {
"function-bind": "^1.1.1"
}
},
"has-flag": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
"integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
"dev": true
},
"has-symbols": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.3.tgz",
"integrity": "sha512-l3LCuF6MgDNwTDKkdYGEihYjt5pRPbEg46rtlmnSPlUbgmB8LOIrKJbYYFBSbnPaJexMKtiPO8hmeRjRz2Td+A==",
"dev": true
},
"he": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/he/-/he-1.2.0.tgz",
"integrity": "sha512-F/1DnUGPopORZi0ni+CvrCgHQ5FyEAHRLSApuYWMmrbSwoN2Mn/7k+Gl38gJnR7yyDZk6WLXwiGod1JOWNDKGw==",
"dev": true
},
"html-encoding-sniffer": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/html-encoding-sniffer/-/html-encoding-sniffer-3.0.0.tgz",
"integrity": "sha512-oWv4T4yJ52iKrufjnyZPkrN0CH3QnrUqdB6In1g5Fe1mia8GmF36gnfNySxoZtxD5+NmYw1EElVXiBk93UeskA==",
"dev": true,
"requires": {
"whatwg-encoding": "^2.0.0"
}
},
"http-proxy": {
"version": "1.18.1",
"resolved": "https://registry.npmjs.org/http-proxy/-/http-proxy-1.18.1.tgz",
"integrity": "sha512-7mz/721AbnJwIVbnaSv1Cz3Am0ZLT/UBwkC92VlxhXv/k/BBQfM2fXElQNC27BVGr0uwUpplYPQM9LnaBMR5NQ==",
"dev": true,
"requires": {
"eventemitter3": "^4.0.0",
"follow-redirects": "^1.0.0",
"requires-port": "^1.0.0"
}
},
"http-server": {
"version": "14.1.1",
"resolved": "https://registry.npmjs.org/http-server/-/http-server-14.1.1.tgz",
"integrity": "sha512-+cbxadF40UXd9T01zUHgA+rlo2Bg1Srer4+B4NwIHdaGxAGGv59nYRnGGDJ9LBk7alpS0US+J+bLLdQOOkJq4A==",
"dev": true,
"requires": {
"basic-auth": "^2.0.1",
"chalk": "^4.1.2",
"corser": "^2.0.1",
"he": "^1.2.0",
"html-encoding-sniffer": "^3.0.0",
"http-proxy": "^1.18.1",
"mime": "^1.6.0",
"minimist": "^1.2.6",
"opener": "^1.5.1",
"portfinder": "^1.0.28",
"secure-compare": "3.0.1",
"union": "~0.5.0",
"url-join": "^4.0.1"
}
},
"iconv-lite": {
"version": "0.6.3",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz",
"integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==",
"dev": true,
"requires": {
"safer-buffer": ">= 2.1.2 < 3.0.0"
}
},
"lodash": {
"version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==",
"dev": true
},
"mime": {
"version": "1.6.0",
"resolved": "https://registry.npmjs.org/mime/-/mime-1.6.0.tgz",
"integrity": "sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==",
"dev": true
},
"minimist": {
"version": "1.2.8",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz",
"integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==",
"dev": true
},
"mkdirp": {
"version": "0.5.6",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.6.tgz",
"integrity": "sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==",
"dev": true,
"requires": {
"minimist": "^1.2.6"
}
},
"ms": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz",
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==",
"dev": true
},
"object-inspect": {
"version": "1.12.3",
"resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.12.3.tgz",
"integrity": "sha512-geUvdk7c+eizMNUDkRpW1wJwgfOiOeHbxBR/hLXK1aT6zmVSO0jsQcs7fj6MGw89jC/cjGfLcNOrtMYtGqm81g==",
"dev": true
},
"opener": {
"version": "1.5.2",
"resolved": "https://registry.npmjs.org/opener/-/opener-1.5.2.tgz",
"integrity": "sha512-ur5UIdyw5Y7yEj9wLzhqXiy6GZ3Mwx0yGI+5sMn2r0N0v3cKJvUmFH5yPP+WXh9e0xfyzyJX95D8l088DNFj7A==",
"dev": true
},
"portfinder": {
"version": "1.0.32",
"resolved": "https://registry.npmjs.org/portfinder/-/portfinder-1.0.32.tgz",
"integrity": "sha512-on2ZJVVDXRADWE6jnQaX0ioEylzgBpQk8r55NE4wjXW1ZxO+BgDlY6DXwj20i0V8eB4SenDQ00WEaxfiIQPcxg==",
"dev": true,
"requires": {
"async": "^2.6.4",
"debug": "^3.2.7",
"mkdirp": "^0.5.6"
}
},
"qs": {
"version": "6.11.0",
"resolved": "https://registry.npmjs.org/qs/-/qs-6.11.0.tgz",
"integrity": "sha512-MvjoMCJwEarSbUYk5O+nmoSzSutSsTwF85zcHPQ9OrlFoZOYIjaqBAJIqIXjptyD5vThxGq52Xu/MaJzRkIk4Q==",
"dev": true,
"requires": {
"side-channel": "^1.0.4"
}
},
"requires-port": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/requires-port/-/requires-port-1.0.0.tgz",
"integrity": "sha512-KigOCHcocU3XODJxsu8i/j8T9tzT4adHiecwORRQ0ZZFcp7ahwXuRU1m+yuO90C5ZUyGeGfocHDI14M3L3yDAQ==",
"dev": true
},
"safe-buffer": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==",
"dev": true
},
"safer-buffer": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==",
"dev": true
},
"secure-compare": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/secure-compare/-/secure-compare-3.0.1.tgz",
"integrity": "sha512-AckIIV90rPDcBcglUwXPF3kg0P0qmPsPXAj6BBEENQE1p5yA1xfmDJzfi1Tappj37Pv2mVbKpL3Z1T+Nn7k1Qw==",
"dev": true
},
"side-channel": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.0.4.tgz",
"integrity": "sha512-q5XPytqFEIKHkGdiMIrY10mvLRvnQh42/+GoBlFW3b2LXLE2xxJpZFdm94we0BaoV3RwJyGqg5wS7epxTv0Zvw==",
"dev": true,
"requires": {
"call-bind": "^1.0.0",
"get-intrinsic": "^1.0.2",
"object-inspect": "^1.9.0"
}
},
"supports-color": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
"integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
"dev": true,
"requires": {
"has-flag": "^4.0.0"
}
},
"union": {
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/union/-/union-0.5.0.tgz",
"integrity": "sha512-N6uOhuW6zO95P3Mel2I2zMsbsanvvtgn6jVqJv4vbVcz/JN0OkL9suomjQGmWtxJQXOCqUJvquc1sMeNz/IwlA==",
"dev": true,
"requires": {
"qs": "^6.4.0"
}
},
"url-join": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/url-join/-/url-join-4.0.1.tgz",
"integrity": "sha512-jk1+QP6ZJqyOiuEI9AEWQfju/nB2Pw466kbA0LEZljHwKeMgd9WrAEgEGxjPDD2+TNbbb37rTyhEfrCXfuKXnA==",
"dev": true
},
"whatwg-encoding": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/whatwg-encoding/-/whatwg-encoding-2.0.0.tgz",
"integrity": "sha512-p41ogyeMUrw3jWclHWTQg1k05DSVXPLcVxRTYsXUk+ZooOCZLcoYgPZ/HL/D/N+uQPOtcp1me1WhBEaX02mhWg==",
"dev": true,
"requires": {
"iconv-lite": "0.6.3"
}
}
}
}

View File

@ -2,6 +2,7 @@ let calculateButton = document.getElementById("calculate-button");
let binaryButton = document.getElementById("binary-button");
let numberOneInput = document.getElementById("first");
let numberTwoInput = document.getElementById("second");
let operationType = document.getElementById("type");
let select = document.getElementById("operation");
let resultInput = document.getElementById("result");
@ -17,9 +18,10 @@ binaryButton.onclick = function() {
function executeRequest(address) {
let first = numberOneInput.value;
let second = numberTwoInput.value;
fetch(`http://localhost:8080/${address}?first=${first}&second=${second}`)
let type = operationType.value;
fetch(`http://localhost:8080/${address}?first=${first}&second=${second}&type=${type}`)
.then(response => response.text())
.then(res => resultInput.value = res);
.then(res => resultInput.value = String(res) );
}
function executeBinaryRequest(result) {

View File

@ -2,8 +2,6 @@ package com.LabWork.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@ -15,7 +13,7 @@ public class AppApplication {
SpringApplication.run(AppApplication.class, args);
}
@GetMapping("/hello")
/* @GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}
@ -51,6 +49,6 @@ public class AppApplication {
@GetMapping("/binary")
public String Binary(@RequestParam int result) {
return Integer.toBinaryString(result);
}
}*/
}

View File

@ -0,0 +1,19 @@
package com.LabWork.app.calc.configuration;
import com.LabWork.app.calc.domain.MethodInteger;
import com.LabWork.app.calc.domain.MethodString;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MethodConfiguration {
public MethodInteger createIntegerMethods() {
return new MethodInteger();
}
public MethodString createStringMethods() {
return new MethodString();
}
}

View File

@ -0,0 +1,51 @@
package com.LabWork.app.calc.controller;
import com.LabWork.app.calc.service.MethodService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@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("/multiplication")
@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.Multiplication(first, second, type);
}
@GetMapping("/difference")
@ResponseBody
public String difference(@RequestParam(value = "first", defaultValue = "0") Object first,
@RequestParam(value = "second", defaultValue = "0") Object second,
@RequestParam(value = "type", defaultValue = "int") String type) {
return methodService.Difference(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);
}
}

View File

@ -0,0 +1,12 @@
package com.LabWork.app.calc.domain;
public interface IMethods<T> {
T Sum(T first, T second);
T Multiplication(T first, T second);
T Difference(T first, T second);
T Contains(T first, T second);
}

View File

@ -0,0 +1,30 @@
package com.LabWork.app.calc.domain;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component(value = "int")
public class MethodInteger implements IMethods<Integer> {
@Override
public Integer Sum(Integer first, Integer second) {
return first + second;
}
@Override
public Integer Multiplication(Integer first, Integer second) {
return first * second;
}
@Override
public Integer Difference(Integer first, Integer second) {
return first - second;
}
@Override
public Integer Contains(Integer first, Integer second) {
if (second==0){
return second;
}else{
return first/second;
}
}
}

View File

@ -0,0 +1,37 @@
package com.LabWork.app.calc.domain;
import org.springframework.stereotype.Component;
@Component(value = "string")
public class MethodString implements IMethods<String>{
@Override
public String Sum(String first, String second) {
return first.concat(second);
}
@Override
public String Multiplication(String first, String second) {
String res = first;
for (int i = 0; i < Integer.parseInt(second) - 1; i++) {
res = Sum(res, first);
}
return res;
}
@Override
public String Difference(String first, String second) {
if (first.contains(second)) {
return (first).replace(second, "");
}
return first;
}
@Override
public String Contains(String first, String second) {
if ((first).contains(second)) {
return "true";
} else{
return "false";
}
}
}

View File

@ -0,0 +1,55 @@
package com.LabWork.app.calc.service;
import com.LabWork.app.calc.domain.IMethods;
import com.LabWork.app.calc.domain.MethodInteger;
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);
if (method instanceof MethodInteger) {
return String.format("%s", method.Sum(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));
}
else {
return String.format("%s", method.Sum(first, second));
}
}
public String Multiplication(Object first, Object second, String type) {
final IMethods method = (IMethods) applicationContext.getBean(type);
if (method instanceof MethodInteger) {
return String.format("%s", method.Multiplication(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));
}
else {
return String.format("%s", method.Multiplication(first, second));
}
}
public String Difference(Object first, Object second, String type) {
final IMethods method = (IMethods) applicationContext.getBean(type);
if (method instanceof MethodInteger) {
return String.format("%s", method.Difference(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));
}
else {
return String.format("%s", method.Difference(first, second));
}
}
public String Contains(Object first, Object second, String type) {
final IMethods method = (IMethods) applicationContext.getBean(type);
if (method instanceof MethodInteger) {
return String.format("%s", method.Contains(Integer.parseInt(first.toString()), Integer.parseInt(second.toString())));
}
else {
return String.format("%s", method.Contains(first, second));
}
}
}

View File

@ -1,13 +1,62 @@
package com.LabWork.app;
import com.LabWork.app.calc.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 AppApplicationTests {
@Autowired
MethodService methodService;
@Test
void contextLoads() {
void testMethodSumInt() {
final String res = methodService.Sum(1, 2, "int");
Assertions.assertEquals(3, Integer.parseInt(res));
}
@Test
void testMethodSumString() {
final String res = methodService.Sum("1", "2", "string");
Assertions.assertEquals("12", res);
}
@Test
void testMethodMinusInt() {
final String res = methodService.Difference(1, 2, "int");
Assertions.assertEquals(-1, Integer.parseInt(res));
}
@Test
void testMethodMinusString() {
final String res = methodService.Difference("214324", "4", "string");
Assertions.assertEquals("2132", res);
}
@Test
void testMethodMultInt() {
final String res = methodService.Multiplication(1, 2, "int");
Assertions.assertEquals(2, Integer.parseInt(res));
}
@Test
void testMethodMultString() {
final String res = methodService.Multiplication("1", "2", "string");
Assertions.assertEquals("11", res);
}
@Test
void testMethodContainsInt() {
final String res = methodService.Contains(123, 2, "int");
Assertions.assertEquals(61, Integer.parseInt(res));
}
@Test
void testMethodContainsString() {
final String res = methodService.Contains("1", "2", "string");
Assertions.assertEquals("false", res);
}
}