Commit 2088c158 by Patryk Czarnik

kalkulator4: extract method i styl "proceduralny"

parent 831573fc
......@@ -15,36 +15,28 @@ public class Kalkulator4 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html><head>");
out.println("<title>Kalkulator serwletowy</title>");
out.println("<link rel='stylesheet' type='text/css' href='styl.css'>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Kalkulator</h1>");
out.println("<form method='post'>");
out.println("<input type='number' name='liczba1'>");
out.println("<select name='operacja'>");
out.println("<option value='+'>+</option>");
out.println("<option value='-'>-</option>");
out.println("<option value='*'>*</option>");
out.println("<option value='/'>/</option>");
out.println("</select>");
out.println("<input type='number' name='liczba2'>");
out.println("<button>Oblicz</button>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
PrintWriter out = wyslijNaglowkiIPobierzWritera(response);
poczatekHtml(out);
formularz(out);
koniecHtml(out);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = wyslijNaglowkiIPobierzWritera(response);
poczatekHtml(out);
formularz(out);
pobierzParametryIWstawWynik(request, out);
koniecHtml(out);
}
private PrintWriter wyslijNaglowkiIPobierzWritera(HttpServletResponse response) throws IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
return response.getWriter();
}
private void poczatekHtml(PrintWriter out) {
out.println("<!DOCTYPE html>");
out.println("<html><head>");
out.println("<title>Kalkulator serwletowy</title>");
......@@ -52,6 +44,9 @@ public class Kalkulator4 extends HttpServlet {
out.println("</head>");
out.println("<body>");
out.println("<h1>Kalkulator</h1>");
}
private void formularz(PrintWriter out) {
out.println("<form method='post'>");
out.println("<input type='number' name='liczba1'>");
out.println("<select name='operacja'>");
......@@ -63,7 +58,14 @@ public class Kalkulator4 extends HttpServlet {
out.println("<input type='number' name='liczba2'>");
out.println("<button>Oblicz</button>");
out.println("</form>");
}
private void koniecHtml(PrintWriter out) {
out.println("</body>");
out.println("</html>");
}
private void pobierzParametryIWstawWynik(HttpServletRequest request, PrintWriter out) {
String param1 = request.getParameter("liczba1");
String param2 = request.getParameter("liczba2");
String operacja = request.getParameter("operacja");
......@@ -71,13 +73,7 @@ public class Kalkulator4 extends HttpServlet {
try {
long liczba1 = Long.parseLong(param1);
long liczba2 = Long.parseLong(param2);
long wynik = switch(operacja) {
case "+" -> liczba1 + liczba2;
case "-" -> liczba1 - liczba2;
case "*" -> liczba1 * liczba2;
case "/" -> liczba1 / liczba2;
default -> throw new IllegalArgumentException("Nieznane działanie: " + operacja);
};
long wynik = oblicz(operacja, liczba1, liczba2);
out.printf("<div class='wynik'>%d %s %d = <strong>%d</strong></div>\n",
liczba1, operacja, liczba2, wynik);
} catch (NumberFormatException e) {
......@@ -85,8 +81,16 @@ public class Kalkulator4 extends HttpServlet {
} catch(Exception e) {
out.printf("<div class='error'>Błąd: %s</div>\n", e.getMessage());
}
out.println("</body>");
out.println("</html>");
}
private long oblicz(String operacja, long liczba1, long liczba2) {
long wynik = switch(operacja) {
case "+" -> liczba1 + liczba2;
case "-" -> liczba1 - liczba2;
case "*" -> liczba1 * liczba2;
case "/" -> liczba1 / liczba2;
default -> throw new IllegalArgumentException("Nieznane działanie: " + operacja);
};
return wynik;
}
}
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