Commit b0360931 by Patryk Czarnik

Skopiowanie prostej wersji kalkulatora

parent 0bbf0f9c
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ProstyKalkulator {
@RequestMapping("/moj_kalkulator")
public String dzialaj(Long liczba1, Long liczba2, String operacja, Model model) {
if(liczba1 != null && liczba2 != null && operacja != null) {
Long wynik = switch(operacja) {
case "+" -> liczba1 + liczba2;
case "-" -> liczba1 - liczba2;
case "*" -> liczba1 * liczba2;
case "/" -> liczba1 / liczba2;
default -> 0L;
};
model.addAttribute("wynik", wynik);
}
return "moj_kalkulator.html";
}
}
......@@ -36,6 +36,7 @@
<h2>Przykłady formularzy</h2>
<ul>
<li><a th:href="@{/moj_kalkulator}">Prosty Kalkulator</a> - wersja z zajęć</li>
<li><a th:href="@{/kalkulator}">Kalkulator</a> (formlularz)</li>
<li><a th:href="@{/kalkulator/historia}">Historia Kalkulatora HTML</a></li>
<li><a th:href="@{/historia.json}">Historia Kalkulatora JSON</a></li>
......
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Kalkulator</title>
<link rel="stylesheet" type="text/css" th:href="@{/styl.css}">
</head>
<body>
<h1>Kalkulator</h1>
<form class="kalkulator">
<input type="number" name="liczba1" th:value="${param.liczba1}">
<select name="operacja">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="number" name="liczba2" th:value="${param.liczba2}">
<br>
<button>Oblicz</button>
</form>
<div class="wynik">
[[${wynik}]]
</div>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment