Compare commits

..

2 Commits

Author SHA1 Message Date
a6edbfeaf1 Добавлен отчёт №2 2023-06-18 13:11:53 +04:00
abazov73
4a7874a0a7 Вторая лабораторная работа. 2023-03-14 09:47:00 +04:00
12 changed files with 305 additions and 13 deletions

View File

@ -0,0 +1,23 @@
package com.example.ipLab.TypesCalc.Configuration;
import com.example.ipLab.TypesCalc.domen.TypeCalcInteger;
import com.example.ipLab.TypesCalc.domen.TypeCalcString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CalcConfiguration {
private final Logger log = LoggerFactory.getLogger(CalcConfiguration.class);
@Bean(value="int")
public TypeCalcInteger createTypeCalcInteger(){
return new TypeCalcInteger();
}
@Bean(value="string")
public TypeCalcString createTypeCalcString(){
return new TypeCalcString();
}
}

View File

@ -0,0 +1,33 @@
package com.example.ipLab.TypesCalc.Service;
import com.example.ipLab.TypesCalc.domen.TypeCalc;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
@Service
public class CalcService {
private final ApplicationContext applicationContext;
public CalcService(ApplicationContext applicationContext){this.applicationContext = applicationContext;}
public Object Sum(Object obj1, Object obj2, String type){
final TypeCalc typeCalculator = (TypeCalc) applicationContext.getBean(type);
if (type.startsWith("int")) return typeCalculator.Sum(Integer.parseInt(obj1.toString()), Integer.parseInt(obj2.toString()));
return typeCalculator.Sum(obj1, obj2);
}
public Object Dif(Object obj1, Object obj2, String type){
final TypeCalc typeCalculator = (TypeCalc) applicationContext.getBean(type);
if (type.startsWith("int")) return typeCalculator.Dif(Integer.parseInt(obj1.toString()), Integer.parseInt(obj2.toString()));
return typeCalculator.Dif(obj1, obj2);
}
public Object Multiply(Object obj1, Object obj2, String type){
final TypeCalc typeCalculator = (TypeCalc) applicationContext.getBean(type);
if (type.startsWith("int")) return typeCalculator.Multiply(Integer.parseInt(obj1.toString()), Integer.parseInt(obj2.toString()));
return typeCalculator.Multiply(obj1, obj2);
}
public Object Div(Object obj1, Object obj2, String type){
final TypeCalc typeCalculator = (TypeCalc) applicationContext.getBean(type);
if (type.startsWith("int")) return typeCalculator.Div(Integer.parseInt(obj1.toString()), Integer.parseInt(obj2.toString()));
return typeCalculator.Div(obj1, obj2);
}
}

View File

@ -0,0 +1,41 @@
package com.example.ipLab.TypesCalc.controller;
import com.example.ipLab.TypesCalc.Service.CalcService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TypeCalcController {
private final CalcService calcService;
public TypeCalcController(CalcService calcService) {this.calcService = calcService;}
@GetMapping("/CalcSum")
public Object calcSum(@RequestParam(value = "obj1", defaultValue = "null objects") Object obj1,
@RequestParam(value = "obj2", defaultValue = "provided") Object obj2,
@RequestParam(value = "type", defaultValue = "string") String type){
return calcService.Sum(obj1, obj2, type);
}
@GetMapping("/CalcDif")
public Object calcDif(@RequestParam(value = "obj1", defaultValue = "null objects") Object obj1,
@RequestParam(value = "obj2", defaultValue = "provided") Object obj2,
@RequestParam(value = "type", defaultValue = "string") String type){
return calcService.Dif(obj1, obj2, type);
}
@GetMapping("/CalcMultiply")
public Object calcMultiply(@RequestParam(value = "obj1", defaultValue = "null objects") Object obj1,
@RequestParam(value = "obj2", defaultValue = "provided") Object obj2,
@RequestParam(value = "type", defaultValue = "string") String type){
return calcService.Multiply(obj1, obj2, type);
}
@GetMapping("/CalcDiv")
public Object calcDiv(@RequestParam(value = "obj1", defaultValue = "null objects") Object obj1,
@RequestParam(value = "obj2", defaultValue = "provided") Object obj2,
@RequestParam(value = "type", defaultValue = "string") String type){
return calcService.Div(obj1, obj2, type);
}
}

View File

@ -0,0 +1,8 @@
package com.example.ipLab.TypesCalc.domen;
public interface TypeCalc<T> {
T Sum(T obj1, T obj2);
T Dif(T obj1, T obj2);
T Multiply(T obj1, T obj2);
T Div(T obj1, T obj2);
}

View File

@ -0,0 +1,30 @@
package com.example.ipLab.TypesCalc.domen;
import org.springframework.stereotype.Component;
public class TypeCalcInteger implements TypeCalc<Integer>{
@Override
public Integer Sum(Integer num1, Integer num2) {
return num1 + num2;
}
@Override
public Integer Dif(Integer num1, Integer num2) {
return num1 - num2;
}
@Override
public Integer Multiply(Integer num1, Integer num2) {
return num1 * num2;
}
@Override
public Integer Div(Integer num1, Integer num2) {
try {
return num1 / num2;
}
catch (ArithmeticException ex){
return 0;
}
}
}

View File

@ -0,0 +1,57 @@
package com.example.ipLab.TypesCalc.domen;
import org.springframework.stereotype.Component;
public class TypeCalcString implements TypeCalc<String> {
@Override
public String Sum(String s1, String s2) {
return s1 + s2;
}
@Override
public String Dif(String s1, String s2) {
String res = "";
for (char c : s1.toCharArray()){
boolean foundInOther = false;
for (int i = 0; i < s2.length(); i++){
if (c == s2.charAt(i)) {
foundInOther = true;
break;
}
}
if (!foundInOther) res += c;
}
return res;
}
@Override
public String Multiply(String s1, String s2) {
String res = "";
for (char c : s1.toCharArray()){
boolean foundInOther = false;
for (int i = 0; i < s2.length(); i++){
if (c == s2.charAt(i)) {
foundInOther = true;
break;
}
}
if (foundInOther) res += c;
}
return res;
}
@Override
public String Div(String s1, String s2) {
StringBuilder res = new StringBuilder();
int maxLength = Integer.max(s1.length(), s2.length());
for (int i = 0; i < maxLength; i++){
if (i < s1.length()){
res.append(s1.charAt(i));
}
if (i < s2.length()){
res.append(s2.charAt(i));
}
}
return res.toString();
}
}

View File

@ -1,13 +1,72 @@
package com.example.ipLab;
import com.example.ipLab.TypesCalc.Service.CalcService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class IpLabApplicationTests {
@Autowired
CalcService calcService;
@Test
void contextLoads() {
void testIntegerCalcSum() {
final Object res = calcService.Sum(2, 2, "int");
Assertions.assertEquals("4", res.toString());
}
@Test
void testIntegerCalcDif() {
final Object res = calcService.Dif(2, 2, "int");
Assertions.assertEquals("0", res.toString());
}
@Test
void testIntegerCalcMultiply() {
final Object res = calcService.Multiply(2, 3, "int");
Assertions.assertEquals("6", res.toString());
}
@Test
void testIntegerCalcDiv() {
final Object res = calcService.Div(4, 2, "int");
Assertions.assertEquals("2", res.toString());
}
@Test
void testIntegerCalcDivBy0() {
final Object res = calcService.Div(4, 0, "int");
Assertions.assertEquals("0", res.toString());
}
@Test
void testStringCalcSum(){
final Object res = calcService.Sum("2", "2", "string");
Assertions.assertEquals("22", res.toString());
}
@Test
void testStringCalcDif(){
final Object res = calcService.Dif("524", "24", "string");
Assertions.assertEquals("5", res.toString());
}
@Test
void testStringCalcMultiply(){
final Object res = calcService.Multiply("523", "215", "string");
Assertions.assertEquals("52", res.toString());
}
@Test
void testStringCalcDiv(){
final Object res = calcService.Div("135", "24", "string");
Assertions.assertEquals("12345", res.toString());
}
@Test
void testSpeakerErrorWiredInt() {
Assertions.assertThrows(NoSuchBeanDefinitionException.class, () -> calcService.Sum("1", "1", "integer"));
}
}

View File

@ -4,7 +4,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src ="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<script src ="scripts/calc.js"></script>
<script src ="scripts/calcType.js"></script>
<link href="node_modules/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet" />
<link href="node_modules/@fortawesome/fontawesome-free/css/all.min.css" rel="stylesheet" />
<title>Calc</title>
@ -12,13 +12,18 @@
</head>
<body class="h-100">
<div class="d-flex flex-column align-content-center flex-wrap">
<div class="input-group p-3">
<input id="numberInput" type="number" class="form-control" placeholder="Введите число..." required>
<button class="btn btn-outline-secondary" onclick="calcSecond()" type="button">^2</button>
<button class="btn btn-outline-secondary" onclick="calcRoot()" type="button"></button>
<button class="btn btn-outline-secondary" onclick="calcFact()" type="button">!</button>
<button class="btn btn-outline-secondary" onclick="calcDigit()" type="button">Сумма цифр</button>
</div>
<div class="input-group p-3">
<input type="text" class="form-control" id="obj1Input" placeholder="Введите объект 1...">
<input type="text" class="form-control" id="obj2Input" placeholder="Введите объект 2...">
<select class="custom-select" id="typeSelect">
<option selected value="int">Числа</option>
<option value="string">Строки</option>
</select>
<button class="btn btn-outline-secondary" onclick="calcSum()" type="button">+</button>
<button class="btn btn-outline-secondary" onclick="calcDif()" type="button">-</button>
<button class="btn btn-outline-secondary" onclick="calcMultiply()" type="button">*</button>
<button class="btn btn-outline-secondary" onclick="calcDiv()" type="button">/</button>
</div>
<a id="responseField" class="m-3"></a>
</div>
</body>

View File

@ -1,4 +1,4 @@
function calcSecond(){
function calcSum(){
var num = document.getElementById("numberInput").value;
fetch("http://127.0.0.1:8080/second?num=" + num)
.then(function(response) {
@ -13,7 +13,7 @@ function calcSecond(){
.catch(err => {document.getElementById("responseField").innerHTML = "Ошибка: " + err;})
}
function calcRoot(){
function calcDif(){
var num = document.getElementById("numberInput").value;
if (num < 0) {
document.getElementById("responseField").innerHTML = "Результат: введите НЕОТРИЦАТЕЛЬНОЕ число";
@ -33,7 +33,7 @@ function calcRoot(){
.catch(err => {document.getElementById("responseField").innerHTML = "Ошибка: " + err;})
}
function calcFact(){
function calcMultiply(){
var num = document.getElementById("numberInput").value;
if (num < 0) {
document.getElementById("responseField").innerHTML = "Результат: введите НЕОТРИЦАТЕЛЬНОЕ число";
@ -53,7 +53,7 @@ function calcFact(){
.catch(err => {document.getElementById("responseField").innerHTML = "Ошибка: " + err;})
}
function calcDigit(){
function calcDiv(){
var num = document.getElementById("numberInput").value;
fetch("http://127.0.0.1:8080/digit?num=" + num)
.then((response) => {

View File

@ -0,0 +1,36 @@
function calcSum(){
fetchServer("CalcSum");
}
function calcDif(){
fetchServer("CalcDif");
}
function calcMultiply(){
fetchServer("CalcMultiply");
}
function calcDiv(){
fetchServer("CalcDiv");
}
function fetchServer(adress){
var obj1 = document.getElementById("obj1Input").value;
var obj2 = document.getElementById("obj2Input").value;
var type = document.getElementById("typeSelect").value;
if (type == "int" && (isNaN(obj1) || isNaN(obj2))){
document.getElementById("responseField").innerHTML = "Ошибка: введите число для операций с числами или измените тип данных!";
return;
}
fetch("http://127.0.0.1:8080/" + adress + "?obj1=" + obj1 + "&obj2=" + obj2 + "&type=" + type)
.then(function(response) {
if (!response.ok){
return response.text().then(text => {throw new Error(text)});
}
return response.text();
})
.then((response) => {
document.getElementById("responseField").innerHTML = "Результат: " + response;
})
.catch(err => {document.getElementById("responseField").innerHTML = "Ошибка: " + err;})
}