This commit is contained in:
AnnZhimol 2023-03-04 12:57:40 +04:00
parent 6dfb0655bb
commit eab61da355
14 changed files with 940 additions and 1124 deletions

View File

@ -1,60 +1,54 @@
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Лабораторная 1</title>
</head>
<body>
<div class="container-sm p-3">
<form id="calc">
<h2 class="text-center mb-3">Произведение</h2>
<div class="mb-3">
<div class="input-group mb-1">
<input type="number" class="form-control" placeholder="V1" aria-label="First Number" name="v1" autocomplete="off">
<span class="input-group-text">*</span>
<input type="number" class="form-control" placeholder="V2" aria-label="Second Number" name="v2" autocomplete="off">
<span class="input-group-text" id="calc_result">0</span>
<button type="submit" class="btn btn-primary">Подтвердить</button>
</div>
</div>
</form>
<form id="toUpperCase">
<h2 class="text-center mb-3">Поднять регистр</h2>
<div class="mb-3">
<div class="input-group mb-1">
<input type="text" class="form-control" placeholder="String" aria-label="Your String" name="value" autocomplete="off">
<span class="input-group-text" id="tuc_result"></span>
<button type="submit" class="btn btn-primary">Подтвердить</button>
</div>
</div>
</form>
<head>
<title>Лабораторная №2</title>
<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="node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<link href="node_modules/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet" />
<script src="main.js"></script>
</head>
<form id="split">
<h2 class="text-center mb-3">Разделить строку</h2>
<body>
<article class="d-flex">
<div class="d-flex flex-column align-items-start">
<div class="m-3">
<label class="form-label">Операция</label>
<select class="form-select form-select-lg" id="operation" onchange="changeVariableInputs()">
<option disabled>Выберите операцию</option>
<option value="sum">Сложение</option>
<option value="sut">Вычитание</option>
<option value="len">Подсчёт длины</option>
<option value="inv">Инверсия (Отрицание)</option>
</select>
<div class="mb-3">
<div class="input-group mb-1">
<input type="text" class="form-control" placeholder="String" aria-label="Your String" name="value" autocomplete="off">
<input type="text" class="form-control" placeholder="Separator" aria-label="Your String" name="sep" autocomplete="off">
<button type="submit" class="btn btn-primary">Подтвердить</button>
</div>
<span class="input-group-text" id="ss_result"></span>
<label class="form-label">Введите переменнную А</label>
<input type="number" class="form-control" id="var_a">
</div>
</form>
<form id="toHex">
<h2 class="text-center mb-3">16-ая система</h2>
<div class="mb-3">
<div class="input-group mb-1">
<input type="number" class="form-control" placeholder="Number" aria-label="Your Number" name="value" autocomplete="off">
<span class="input-group-text" id="nth_result"></span>
<button type="submit" class="btn btn-primary">Подтвердить</button>
</div>
</div>
</form>
<button type="button" class="btn btn-primary mx-3" onclick="calculate()">Подтвердить</button>
</div>
</div>
<script type="module" src="main.js"></script>
</body>
</html>
<div class="d-flex flex-column align-items-start m-3">
<div class="m-3 mt-0">
<label class="form-label">Тип переменной</label>
<select class="form-select form-select-lg" id="valueType" onchange="changeInputBoxType()">
<option disabled>Выберите тип</option>
<option value="number">Число</option>
<option value="string">Строка</option>
</select>
<div class="mb-3" id="InputFieldB">
<label class="form-label">Введите переменнную В</label>
<input type="number" class="form-control" id="var_b">
</div>
<div class="d-flex align-items-center mx-3">
<H1 id="ansver">Результат:</h1>
</div>
</div>
</div>
</article>
</body>
</html>

View File

@ -1,71 +1,39 @@
import 'bootstrap/dist/css/bootstrap.css'
import serverRequest from "./serverRequsest.js";
async function calculate(){
const valA = document.getElementById("var_a").value
const valB = document.getElementById("var_b").value
const oper = document.getElementById("operation").value
const type = document.getElementById("valueType").value
const $forms = document.querySelectorAll("form");
var url = `http://localhost:8080/${oper}?type=${type}&a=${valA}&b=${valB}`
const onSubmit = (e) => {
e.preventDefault();
const form = e.target;
switch (form.id) {
case "calc" : {
calc(form)
break;
}
case "toUpperCase" : {
tuc(form)
break;
}
case "split" : {
split(form)
break;
}
case "toHex" : {
nth(form)
break;
}
}
console.log(url)
const responce = await fetch(url)
const json = await responce.json()
if(json.content.errorCode != null){
document.getElementById("ansver").innerHTML = "Ошибка: "+ json.content.errorMassage;
}
else{
document.getElementById("ansver").innerHTML = "Ответ: "+ json.content
}
}
function changeVariableInputs(){
const oper = document.getElementById("operation").value
const calc = (form) => {
const v1 = form.v1.value;
const v2 = form.v2.value;
serverRequest.calc(v1, v2).then(data => {
const $res = document.getElementById("calc_result");
$res.innerHTML = data;
})
if(oper === "len" || oper === "inv"){
document.getElementById("InputFieldB").classList.add("hide")
}
else{
document.getElementById("InputFieldB").classList.remove("hide")
}
}
const tuc = (form) => {
const value = form.value.value;
serverRequest.toUpperCase(value).then(data => {
const $res = document.getElementById("tuc_result");
$res.innerHTML = data;
})
}
const split = (form) => {
const value = form.value.value;
const sep = form.sep.value;
serverRequest.split(value, sep).then(data => {
const $res = document.getElementById("ss_result");
$res.innerHTML = data;
})
}
const nth = (form) => {
const value = form.value.value;
serverRequest.toHex(value).then(data => {
const $res = document.getElementById("nth_result");
$res.innerHTML = data;
})
}
$forms.forEach(item => {
item.onsubmit = onSubmit;
})
function changeInputBoxType(){
const type = document.getElementById("valueType").value
if(type === "number"){
document.getElementById("var_a").type = "number"
document.getElementById("var_b").type = "number"
}
else{
document.getElementById("var_a").type = "text"
document.getElementById("var_b").type = "text"
}
}

1558
Front/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,19 +1,18 @@
{
"name": "interface_project",
"private": true,
"version": "0.0.0",
"type": "module",
"name": "frontend",
"version": "1.0.0",
"description": "",
"main": "index.html",
"scripts": {
"dev": "./node_modules/.bin/vite",
"build": "./node_modules/.bin/vite build",
"preview": "./node_modules/.bin/vite preview"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "http-server -p 3000 ./"
},
"author": "",
"license": "ISC",
"dependencies": {
"bootstrap": "^5.2.3"
},
"devDependencies": {
"vite": "^4.1.0"
},
"dependencies": {
"ajax": "^0.0.4",
"axios": "^1.3.2",
"bootstrap": "^5.2.3"
"http-server": "14.1.1"
}
}
}

View File

@ -1,27 +0,0 @@
import axios from "axios";
export default class serverRequest {
static #url = "http://localhost:8080/";
static async calc(v1 = 0, v2 = 0) {
const { data } = await axios.get(this.#url + `calc?v1=${v1}&v2=${v2}`);
return data;
}
static async toUpperCase(str = "") {
const { data } = await axios.get(this.#url + `toUpperCase?value=${str}`);
return data;
}
static async split(str= "", sep = "") {
if (sep === " ") return;
const { data } = await axios.get(this.#url + `split?value=${str}&sep=${sep}`);
return data;
}
static async toHex(number = 0) {
const { data } = await axios.get(this.#url + `toHex?number=${number}`);
return data;
}
}

View File

@ -2,39 +2,10 @@ package ru.ulstu.is.cbapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
@SpringBootApplication
@RestController
@CrossOrigin
public class CbappApplication {
public static void main(String[] args) {
SpringApplication.run(CbappApplication.class, args);
}
// http://localhost:8080/calc?v1=15&v2=3
@GetMapping("/calc")
public Integer doProiz(@RequestParam(defaultValue = "10") int v1,
@RequestParam(defaultValue = "2") int v2) {
return v1*v2;
}
// http://localhost:8080/toUpperCase?value=internetprogramming
@GetMapping("/toUpperCase")
public @ResponseBody String toUpperCase(@RequestParam(defaultValue = "") String value) {
return value.toUpperCase();
}
// http://localhost:8080/split?value=dghghyyh&sep=g
@GetMapping("/split")
public @ResponseBody String[] split(@RequestParam(defaultValue = "") String value,
@RequestParam(defaultValue = " ") String sep) {
return value.split(sep);
}
// http://localhost:8080/toHex?number=765
@GetMapping("/toHex")
public @ResponseBody String toHex(@RequestParam(defaultValue = "0") int number) {
return Integer.toHexString(number);
}
}

View File

@ -0,0 +1,13 @@
package ru.ulstu.is.cbapp;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class ConfigWeb implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("*");
}
}

View File

@ -0,0 +1,42 @@
package ru.ulstu.is.cbapp.Lab2.controller;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.cbapp.Lab2.domain.ReturnObject;
import ru.ulstu.is.cbapp.Lab2.service.ServiceCalculate;
@RestController
@CrossOrigin
public class ControllerCalculate {
private final ServiceCalculate serviceCalculate;
public ControllerCalculate(ServiceCalculate serviceCalculate){
this.serviceCalculate=serviceCalculate;
}
@GetMapping("/sum")
public ReturnObject sum(@RequestParam(value = "type",defaultValue = "number") String type,
@RequestParam(value = "a", required = false) String a,
@RequestParam(value = "b", required = false) String b){
return new ReturnObject(serviceCalculate.add(a,b,type));
}
@GetMapping("/sut")
public ReturnObject sut(@RequestParam(value = "type",defaultValue = "number") String type,
@RequestParam(value = "a", required = false) String a,
@RequestParam(value = "b", required = false) String b){
return new ReturnObject(serviceCalculate.sut(a,b,type));
}
@GetMapping("/len")
public ReturnObject len(@RequestParam(value = "type",required = false) String type,
@RequestParam(value = "a", required = false) String a){
return new ReturnObject(serviceCalculate.len(a,type));
}
@GetMapping("/inv")
public ReturnObject inv(@RequestParam(value = "type",defaultValue = "number") String type,
@RequestParam(value = "a", defaultValue = "0") String a){
return new ReturnObject(serviceCalculate.inv(a,type));
}
}

View File

@ -0,0 +1,8 @@
package ru.ulstu.is.cbapp.Lab2.domain;
public interface Calculate<T> {
Object add(T a, T b);
Object sut(T a, T b);
Object len(T a);
Object inv(T a);
}

View File

@ -0,0 +1,11 @@
package ru.ulstu.is.cbapp.Lab2.domain;
public class Errors {
public Integer errorCode;
public String errorMassage;
public Errors(Integer code, String massage){
this.errorCode = code;
this.errorMassage = massage;
}
}

View File

@ -0,0 +1,29 @@
package ru.ulstu.is.cbapp.Lab2.domain;
import org.springframework.stereotype.Component;
@Component(value="number")
public class IntegerCalculate implements Calculate<Integer> {
@Override
public Integer add(Integer a, Integer b) {
return a + b;
}
@Override
public Integer sut(Integer a, Integer b) {
return a - b;
}
@Override
public Integer len(Integer a) {
Integer length = String.valueOf(a).length();
return length;
}
@Override
public Integer inv(Integer a) {
return -a;
}
}

View File

@ -0,0 +1,8 @@
package ru.ulstu.is.cbapp.Lab2.domain;
public class ReturnObject {
public Object content;
public ReturnObject(Object content){
this.content = content;
}
}

View File

@ -0,0 +1,27 @@
package ru.ulstu.is.cbapp.Lab2.domain;
import org.springframework.stereotype.Component;
@Component(value = "string")
public class StringCalculate implements Calculate<String> {
@Override
public String add(String a, String b) {
return a+b;
}
@Override
public String sut(String a, String b) {
return a.replace(b, "");
}
@Override
public String len(String a) {
return "Длина = "+a.length();
}
@Override
public String inv(String a) {
return new StringBuilder(a).reverse().toString();
}
}

View File

@ -0,0 +1,85 @@
package ru.ulstu.is.cbapp.Lab2.service;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import ru.ulstu.is.cbapp.Lab2.domain.Calculate;
import ru.ulstu.is.cbapp.Lab2.domain.Errors;
@Service
public class ServiceCalculate {
private final ApplicationContext applicationContext;
public ServiceCalculate(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public Object add(Object a, Object b, String type){
Calculate calculate = (Calculate) applicationContext.getBean(type);
if (!StringUtils.hasText(a.toString()) || !StringUtils.hasText(b.toString())){
return new Errors(400, "Необходимо заполенение обоих полей");
}
try {
if(type.equals("number")){
a = castToInt(a);
b = castToInt(b);
}
return calculate.add(a,b);
}
catch (Exception ex){
return new Errors(400, ex.getMessage());
}
}
public Object sut(Object a, Object b, String type){
Calculate calculate = (Calculate)applicationContext.getBean(type);
if (a==null || a.equals("") || b==null || b.equals("")){
return new Errors(400, "Необходимо заполенение обоих полей");
}
try {
if(type.equals("number")){
a = castToInt(a);
b = castToInt(b);
}
return calculate.sut(a,b);
}
catch (Exception ex){
return new Errors(400, ex.getMessage());
}
}
public Object len(Object a, String type){
Calculate calculate = (Calculate)applicationContext.getBean(type);
if (a==null || a.equals("")){
return new Errors(400, "Необходимо заполенение переменной");
}
try {
if(type.equals("number")){
a = castToInt(a);
}
return calculate.len(a);
}
catch (Exception ex){
return new Errors(400, ex.getMessage());
}
}
public Object inv(Object a, String type){
Calculate calculate = (Calculate)applicationContext.getBean(type);
if (a==null || a.equals("")){
return new Errors(400, "Необходимо заполенение переменной");
}
try {
if(type.equals("number")){
a = castToInt(a);
}
return calculate.inv(a);
}
catch (Exception ex){
return new Errors(400, ex.getMessage());
}
}
public Object castToInt(Object a){
return Integer.parseInt(a.toString());
}
}