Commit 982953d6 by Patryk Czarnik

Kalkulator - lepsza wersja

parent cbbebbea
...@@ -16,8 +16,16 @@ public class KalkulatorController { ...@@ -16,8 +16,16 @@ public class KalkulatorController {
} }
@PostMapping @PostMapping
public String kalkulatorPost(Long liczba1, Long liczba2, Model model) { public String kalkulatorPost(Long liczba1, Long liczba2, String operacja, Model model) {
model.addAttribute("wynik", liczba1+liczba2); long wynik = switch(operacja) {
case "+" -> liczba1 + liczba2;
case "-" -> liczba1 - liczba2;
case "*" -> liczba1 * liczba2;
case "/" -> liczba1 / liczba2;
default -> 0;
};
model.addAttribute("wynik", wynik);
return "kalkulator.html"; return "kalkulator.html";
} }
......
...@@ -2,23 +2,32 @@ ...@@ -2,23 +2,32 @@
<html lang="pl" xmlns:th="http://www.thymeleaf.org"> <html lang="pl" xmlns:th="http://www.thymeleaf.org">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<title>Rozmowa</title> <title>Kalkulator</title>
<link rel="stylesheet" type="text/css" th:href="@{/styl.css}" href="../static/styl.css"> <link rel="stylesheet" type="text/css" th:href="@{/styl.css}" href="../static/styl.css">
</head> </head>
<body> <body>
<h1>Rozmowa ze Springiem</h1> <h1>Kalkulator</h1>
<form method="post"> <form method="post">
<label for="liczba1">Podaj pierwszą liczbę</label><br> <p>Wprowadź liczby i wybierz działanie</p>
<input type="text" name="liczba1" id="liczba1"><br> <input type="number" name="liczba1" id="liczba1" th:value="${param.liczba1}">
<label for="liczba2">Podaj drugą liczbę</label><br> <select name="operacja">
<input type="text" name="liczba2" id="liczba2"><br> <option value="+">+</option>
<button>Dodaj</button> <option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type="number" name="liczba2" id="liczba2" th:value="${param.liczba2}">
<button>Oblicz</button>
</form> </form>
<div class="wynik" <div th:if="${wynik != null}" class="wynik">
th:if="${wynik != null}" <span th:text="${param.liczba1}">2</span>
th:text="${wynik}">1234</div> <span th:text="${param.operacja}">+</span>
<span th:text="${param.liczba2}">3</span>
=
<strong th:text="${wynik}">5</strong>
</div>
</body> </body>
</html> </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