Commit cd280f79 by Patryk Czarnik

Wydzielenie metody oblicz

parent 09bcd4a6
......@@ -19,18 +19,22 @@ public class Kalkulator {
}
@PostMapping
public String kalkulatorPost(Model model, Integer liczba1, Integer liczba2, String operacja) {
public String kalkulatorPost(Model model, Long liczba1, Long liczba2, String operacja) {
// Obsługa metody POST - zakładamy, że parametry zostały przekazane (a jeśli nie, to będzie wyjątek).
Integer wynik = switch(operacja) {
case "+" -> liczba1 + liczba2;
case "-" -> liczba1 - liczba2;
case "*" -> liczba1 * liczba2;
case "/" -> liczba1 / liczba2;
default -> 0;
};
long wynik = oblicz(liczba1, liczba2, operacja);
model.addAttribute("wynik", wynik);
return "kalkulator.html";
}
long oblicz(long liczba1, long liczba2, String operacja) {
switch(operacja) {
case "+" : return liczba1 + liczba2;
case "-" : return liczba1 - liczba2;
case "*" : return liczba1 * liczba2;
case "/" : return liczba1 / liczba2;
default : return 0;
}
}
}
......@@ -11,6 +11,9 @@
<form class="kalkulator" method="post">
<input type="number" name="liczba1" th:value="${param.liczba1}">
<!--/* Opis, jak zautomatyzować działanie select/option w Thymeleaf, ale jakoś nie umiem tu zastosować:
https://www.baeldung.com/thymeleaf-select-option
*/-->
<select name="operacja">
<option value="+">+</option>
<option value="-">-</option>
......
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