Commit 2e735883 by Patryk Czarnik

Kalkulator - wersja podstawowa, ale jest :)

parent 009f6ebd
...@@ -8,11 +8,21 @@ import org.springframework.web.bind.annotation.GetMapping; ...@@ -8,11 +8,21 @@ import org.springframework.web.bind.annotation.GetMapping;
public class KalkulatorController { public class KalkulatorController {
@GetMapping("/kalkulator") @GetMapping("/kalkulator")
public String dzialaj(Model model) { public String dzialaj(Long liczba1, Long liczba2, String operacja, Model model) {
// if(imie != null && !imie.isEmpty()) { if(liczba1 != null && liczba2 != null && operacja != null) {
// String powitanie = "Witaj " + imie; long wynik = oblicz(liczba1, liczba2, operacja);
// model.addAttribute("powitanie", powitanie); model.addAttribute("wynik", wynik);
// } }
return "kalkulator.html"; return "kalkulator.html";
} }
private long oblicz(long liczba1, long liczba2, String operacja) {
return switch (operacja) {
case "+" -> liczba1 + liczba2;
case "-" -> liczba1 - liczba2;
case "*" -> liczba1 * liczba2;
case "/" -> liczba1 / liczba2;
default -> 0;
};
}
} }
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
<button>Oblicz</button> <button>Oblicz</button>
</form> </form>
<!-- miejsce na wynik --> <div class="wynik" th:if="${wynik != null}" th:text="${wynik}">13</div>
<div><a th:href="@{/}">wróć do strony głównej</a></div> <div><a th:href="@{/}">wróć do strony głównej</a></div>
......
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