Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
a0de72dc75 |
@ -1,23 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
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);
|
||||
}
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
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);
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,57 +0,0 @@
|
||||
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();
|
||||
}
|
||||
}
|
@ -1,72 +1,13 @@
|
||||
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 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());
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
@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"));
|
||||
}
|
||||
}
|
||||
|
@ -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/calcType.js"></script>
|
||||
<script src ="scripts/calc.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>
|
||||
@ -13,16 +13,11 @@
|
||||
<body class="h-100">
|
||||
<div class="d-flex flex-column align-content-center flex-wrap">
|
||||
<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>
|
||||
<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>
|
||||
<a id="responseField" class="m-3"></a>
|
||||
</div>
|
||||
|
@ -1,4 +1,4 @@
|
||||
function calcSum(){
|
||||
function calcSecond(){
|
||||
var num = document.getElementById("numberInput").value;
|
||||
fetch("http://127.0.0.1:8080/second?num=" + num)
|
||||
.then(function(response) {
|
||||
@ -13,7 +13,7 @@ function calcSum(){
|
||||
.catch(err => {document.getElementById("responseField").innerHTML = "Ошибка: " + err;})
|
||||
}
|
||||
|
||||
function calcDif(){
|
||||
function calcRoot(){
|
||||
var num = document.getElementById("numberInput").value;
|
||||
if (num < 0) {
|
||||
document.getElementById("responseField").innerHTML = "Результат: введите НЕОТРИЦАТЕЛЬНОЕ число";
|
||||
@ -33,7 +33,7 @@ function calcDif(){
|
||||
.catch(err => {document.getElementById("responseField").innerHTML = "Ошибка: " + err;})
|
||||
}
|
||||
|
||||
function calcMultiply(){
|
||||
function calcFact(){
|
||||
var num = document.getElementById("numberInput").value;
|
||||
if (num < 0) {
|
||||
document.getElementById("responseField").innerHTML = "Результат: введите НЕОТРИЦАТЕЛЬНОЕ число";
|
||||
@ -53,7 +53,7 @@ function calcMultiply(){
|
||||
.catch(err => {document.getElementById("responseField").innerHTML = "Ошибка: " + err;})
|
||||
}
|
||||
|
||||
function calcDiv(){
|
||||
function calcDigit(){
|
||||
var num = document.getElementById("numberInput").value;
|
||||
fetch("http://127.0.0.1:8080/digit?num=" + num)
|
||||
.then((response) => {
|
||||
|
@ -1,36 +0,0 @@
|
||||
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;})
|
||||
}
|
BIN
ПИбд-23_Абазов_А.А_Лабораторная№1.docx
Normal file
BIN
ПИбд-23_Абазов_А.А_Лабораторная№1.docx
Normal file
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user