LabWork_02 done
This commit is contained in:
parent
035fadd9dc
commit
d80f64a86e
@ -14,31 +14,41 @@
|
||||
<div class="col-lg-6">
|
||||
<div class="card shadow-lg">
|
||||
<div class="card-header">
|
||||
<h3 class="text-center font-weight-light my-2">Calculator</h3>
|
||||
<h3 class="text-center font-weight-light my-2">LabWork2</h3>
|
||||
</div>
|
||||
<div class="card-body">
|
||||
<form>
|
||||
<div class="form-row">
|
||||
<div class="col">
|
||||
<div class="form-group">
|
||||
<label for="input1">Number 1</label>
|
||||
<input type="number" class="form-control" id="input1" placeholder="Enter Number 1">
|
||||
<label for="input1">Value 1</label>
|
||||
<input class="form-control" id="input1" placeholder="Enter Value 1">
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="form-group">
|
||||
<label for="input2">Number 2</label>
|
||||
<input type="number" class="form-control" id="input2" placeholder="Enter Number 2">
|
||||
<label for="input2">Value 2</label>
|
||||
<input class="form-control" id="input2" placeholder="Enter Value 2">
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="form-group">
|
||||
<label for="operator">Operator</label>
|
||||
<label for="operator">Method</label>
|
||||
<select class="form-control" id="operator">
|
||||
<option value="+">+</option>
|
||||
<option value="-">-</option>
|
||||
<option value="*">*</option>
|
||||
<option value="/">/</option>
|
||||
<option value="Method1">+</option>
|
||||
<option value="Method2">-, toUpper</option>
|
||||
<option value="Method3">*, toLower</option>
|
||||
<option value="Method4">/, first toLower, second toUpper</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col">
|
||||
<div class="form-group">
|
||||
<label for="operator">Type</label>
|
||||
<select class="form-control" id="Type">
|
||||
<option value="int">Integer</option>
|
||||
<option value="str">String</option>
|
||||
<option value="double">Double</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
@ -47,7 +57,7 @@
|
||||
<label for="result">Result</label>
|
||||
<input type="text" class="form-control result" id="result" readonly>
|
||||
</div>
|
||||
<button type="button" class="btn btn-primary btn-block" onclick="calculate()">Calculate</button>
|
||||
<button type="button" class="btn btn-primary btn-block" onclick="calculate()">Do</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -1,26 +1,16 @@
|
||||
'use strict'
|
||||
let num1 = document.getElementById("input1")
|
||||
let num2 = document.getElementById("input2")
|
||||
let operator = document.getElementById("operator")
|
||||
async function calculate(){
|
||||
let num1 = document.getElementById("input1").value
|
||||
let num2 = document.getElementById("input2").value
|
||||
let operator = document.getElementById("operator").value
|
||||
let result = document.getElementById("result")
|
||||
let type = document.getElementById("Type").value
|
||||
|
||||
|
||||
function FindOperator(operat){
|
||||
let OperatorVal = operat
|
||||
switch(OperatorVal){
|
||||
case "+": return "add"
|
||||
case "-": return "subtract"
|
||||
case "/": return "divide"
|
||||
case "*": return "multiply"
|
||||
}
|
||||
let response = await fetch(`http://localhost:8080/${operator}?Type=${type}&value1=${num1}&value2=${num2}`)
|
||||
let res = await response.text()
|
||||
result.value = res
|
||||
}
|
||||
|
||||
function calculate(){
|
||||
let number1 = num1.value
|
||||
let number2 = num2.value
|
||||
let oper = FindOperator(operator.value)
|
||||
|
||||
fetch(`http://localhost:8080/${oper}/${number1}/${number2}`)
|
||||
.then(response => response.text())
|
||||
.then(data => {result.value = data})
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,4 @@ public class IpApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(IpApplication.class, args);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,25 @@
|
||||
package ru.IP_LabWorks.IP.configuration;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import ru.IP_LabWorks.IP.domain.TypeDouble;
|
||||
import ru.IP_LabWorks.IP.domain.TypeInt;
|
||||
import ru.IP_LabWorks.IP.domain.TypeString;
|
||||
|
||||
@Configuration
|
||||
public class TypeConfiguration {
|
||||
@Bean(value = "int")
|
||||
public TypeInt createIntType(){
|
||||
return new TypeInt();
|
||||
}
|
||||
|
||||
@Bean(value = "str")
|
||||
public TypeString createStrType(){
|
||||
return new TypeString();
|
||||
}
|
||||
|
||||
@Bean(value = "double")
|
||||
public TypeDouble createDoubleType(){
|
||||
return new TypeDouble();
|
||||
}
|
||||
}
|
@ -1,29 +1,45 @@
|
||||
package ru.IP_LabWorks.IP.controllers;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.IP_LabWorks.IP.domain.ITypeInterface;
|
||||
import ru.IP_LabWorks.IP.service.TypeService;
|
||||
|
||||
@RestController
|
||||
public class MainController {
|
||||
@GetMapping("/add/{num1}/{num2}")
|
||||
public int add(@PathVariable int num1, @PathVariable int num2) {
|
||||
return num1 + num2;
|
||||
private final TypeService Service;
|
||||
|
||||
public MainController(TypeService service) {
|
||||
Service = service;
|
||||
}
|
||||
|
||||
@GetMapping("/subtract/{num1}/{num2}")
|
||||
public int subtract(@PathVariable int num1, @PathVariable int num2) {
|
||||
return num1 - num2;
|
||||
@GetMapping("/Method1")
|
||||
public Object Method1(@RequestParam(value = "Type") String Type,
|
||||
@RequestParam(value = "value1") Object value1,
|
||||
@RequestParam(value = "value2") Object value2){
|
||||
return Service.Method1(value1,value2,Type);
|
||||
}
|
||||
|
||||
@GetMapping("/multiply/{num1}/{num2}")
|
||||
public int multiply(@PathVariable int num1, @PathVariable int num2) {
|
||||
return num1 * num2;
|
||||
@GetMapping("/Method2")
|
||||
public Object Method2(@RequestParam(value = "Type") String Type,
|
||||
@RequestParam(value = "value1") Object value1,
|
||||
@RequestParam(value = "value2") Object value2){
|
||||
return Service.Method2(value1,value2,Type);
|
||||
}
|
||||
|
||||
@GetMapping("/divide/{num1}/{num2}")
|
||||
public int divide(@PathVariable int num1, @PathVariable int num2) {
|
||||
return num1 / num2;
|
||||
@GetMapping("/Method3")
|
||||
public Object Method3(@RequestParam(value = "Type") String Type,
|
||||
@RequestParam(value = "value1") Object value1,
|
||||
@RequestParam(value = "value2") Object value2){
|
||||
return Service.Method3(value1,value2,Type);
|
||||
}
|
||||
|
||||
@GetMapping("/Method4")
|
||||
public Object Method4(@RequestParam(value = "Type") String Type,
|
||||
@RequestParam(value = "value1") Object value1,
|
||||
@RequestParam(value = "value2") Object value2){
|
||||
return Service.Method4(value1,value2,Type);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,8 @@
|
||||
package ru.IP_LabWorks.IP.domain;
|
||||
|
||||
public interface ITypeInterface<T> {
|
||||
T Method1(T value1, T value2);
|
||||
T Method2(T value1, T value2);
|
||||
T Method3(T value1, T value2);
|
||||
T Method4(T value1, T value2);
|
||||
}
|
24
src/main/java/ru/IP_LabWorks/IP/domain/TypeDouble.java
Normal file
24
src/main/java/ru/IP_LabWorks/IP/domain/TypeDouble.java
Normal file
@ -0,0 +1,24 @@
|
||||
package ru.IP_LabWorks.IP.domain;
|
||||
|
||||
public class TypeDouble implements ITypeInterface<Double> {
|
||||
|
||||
@Override
|
||||
public Double Method1(Double value1, Double value2) {
|
||||
return value1 + value2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double Method2(Double value1, Double value2) {
|
||||
return value1 - value2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double Method3(Double value1, Double value2) {
|
||||
return value1 * value2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Double Method4(Double value1, Double value2) {
|
||||
return value1 / value2;
|
||||
}
|
||||
}
|
24
src/main/java/ru/IP_LabWorks/IP/domain/TypeInt.java
Normal file
24
src/main/java/ru/IP_LabWorks/IP/domain/TypeInt.java
Normal file
@ -0,0 +1,24 @@
|
||||
package ru.IP_LabWorks.IP.domain;
|
||||
|
||||
|
||||
public class TypeInt implements ITypeInterface<Integer> {
|
||||
@Override
|
||||
public Integer Method1(Integer value1, Integer value2) {
|
||||
return value1 + value2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer Method2(Integer value1, Integer value2) {
|
||||
return value1 - value2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer Method3(Integer value1, Integer value2) {
|
||||
return value1 * value2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer Method4(Integer value1, Integer value2) {
|
||||
return value1 / value2;
|
||||
}
|
||||
}
|
24
src/main/java/ru/IP_LabWorks/IP/domain/TypeString.java
Normal file
24
src/main/java/ru/IP_LabWorks/IP/domain/TypeString.java
Normal file
@ -0,0 +1,24 @@
|
||||
package ru.IP_LabWorks.IP.domain;
|
||||
|
||||
public class TypeString implements ITypeInterface<String> {
|
||||
|
||||
@Override
|
||||
public String Method1(String value1, String value2) {
|
||||
return value1 + value2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String Method2(String value1, String value2) {
|
||||
return value1.toUpperCase() + value2.toUpperCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String Method3(String value1, String value2) {
|
||||
return value1.toLowerCase() + value2.toLowerCase();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String Method4(String value1, String value2) {
|
||||
return value1.toLowerCase() + value2.toUpperCase();
|
||||
}
|
||||
}
|
68
src/main/java/ru/IP_LabWorks/IP/service/TypeService.java
Normal file
68
src/main/java/ru/IP_LabWorks/IP/service/TypeService.java
Normal file
@ -0,0 +1,68 @@
|
||||
package ru.IP_LabWorks.IP.service;
|
||||
|
||||
import ru.IP_LabWorks.IP.domain.ITypeInterface;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
@Service
|
||||
public class TypeService {
|
||||
|
||||
private final ApplicationContext applicationContext;
|
||||
private ITypeInterface _type;
|
||||
private Object _value1;
|
||||
private Object _value2;
|
||||
|
||||
public TypeService(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
private void ValidateParams(Object value1, Object value2, String type){
|
||||
_type = (ITypeInterface)applicationContext.getBean(type);
|
||||
|
||||
switch (type) {
|
||||
case "int" -> {
|
||||
try {
|
||||
_value1 = Integer.valueOf(value1.toString());
|
||||
_value2 = Integer.valueOf(value2.toString());
|
||||
}catch (Exception ex){
|
||||
_value1 = 0;
|
||||
_value2 = 0;
|
||||
}
|
||||
}
|
||||
case "double" -> {
|
||||
try {
|
||||
_value1 = Double.valueOf(value1.toString());
|
||||
_value2 = Double.valueOf(value2.toString());
|
||||
}catch (Exception ex){
|
||||
_value1 = 0.0;
|
||||
_value2 = 0.0;
|
||||
}
|
||||
}
|
||||
case "str" -> {
|
||||
_value1 = value1;
|
||||
_value2 = value2;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Object Method1(Object value1, Object value2, String type){
|
||||
ValidateParams(value1,value2,type);
|
||||
return String.format("%s", _type.Method1(_value1,_value2));
|
||||
}
|
||||
|
||||
public Object Method2(Object value1, Object value2, String type){
|
||||
ValidateParams(value1,value2,type);
|
||||
return String.format("%s", _type.Method2(_value1,_value2));
|
||||
}
|
||||
|
||||
public Object Method3(Object value1, Object value2, String type){
|
||||
ValidateParams(value1,value2,type);
|
||||
return String.format("%s", _type.Method3(_value1,_value2));
|
||||
}
|
||||
|
||||
public Object Method4(Object value1, Object value2, String type){
|
||||
ValidateParams(value1,value2,type);
|
||||
return String.format("%s", _type.Method4(_value1,_value2));
|
||||
}
|
||||
}
|
@ -1,13 +1,89 @@
|
||||
package ru.IP_LabWorks.IP;
|
||||
|
||||
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;
|
||||
import ru.IP_LabWorks.IP.service.TypeService;
|
||||
|
||||
@SpringBootTest
|
||||
class IpApplicationTests {
|
||||
@Autowired
|
||||
TypeService Service;
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIntPlus() {
|
||||
final String res = (String) Service.Method1( "100", "10", "int");
|
||||
Assertions.assertEquals("110", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIntMinus() {
|
||||
final String res = (String)Service.Method2( "100", "10", "int");
|
||||
Assertions.assertEquals("90", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIntMultiply() {
|
||||
final String res = (String)Service.Method3( "100", "10", "int");
|
||||
Assertions.assertEquals("1000", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testIntDivision() {
|
||||
final String res = (String)Service.Method4( "100", "10", "int");
|
||||
Assertions.assertEquals("10", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStringPlus() {
|
||||
final String res = (String)Service.Method1( "abc", "dfe", "str");
|
||||
Assertions.assertEquals("abcdfe", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStringtoUpper() {
|
||||
final String res = (String)Service.Method2( "abc", "dfe", "str");
|
||||
Assertions.assertEquals("ABCDFE", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStringtoLower() {
|
||||
final String res = (String)Service.Method3( "abc", "dfe", "str");
|
||||
Assertions.assertEquals("abcdfe", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testStringStrange() {
|
||||
final String res = (String)Service.Method4( "abc", "dfe", "str");
|
||||
Assertions.assertEquals("abcDFE", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDoublePlus() {
|
||||
final String res = (String)Service.Method1( "1.01", "2.76", "double");
|
||||
Assertions.assertEquals("3.7699999999999996", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDoubleMinus() {
|
||||
final String res = (String)Service.Method2( "1.01", "2.76", "double");
|
||||
Assertions.assertEquals("-1.7499999999999998", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDoubleMyltiply() {
|
||||
final String res = (String)Service.Method3( "1.01", "2.76", "double");
|
||||
Assertions.assertEquals("2.7876", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDoubleDivision() {
|
||||
final String res = (String)Service.Method4( "1.01", "2.76", "double");
|
||||
Assertions.assertEquals("0.36594202898550726", res);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user