Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_weekendowa_20221008
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
java_weekendowa_20221008
Commits
2088c158
Commit
2088c158
authored
Oct 22, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
kalkulator4: extract method i styl "proceduralny"
parent
831573fc
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
34 deletions
+38
-34
Kalkulator4.java
PC23-Serwlety/src/main/java/kalkulator/Kalkulator4.java
+38
-34
No files found.
PC23-Serwlety/src/main/java/kalkulator/Kalkulator4.java
View file @
2088c158
...
@@ -15,36 +15,28 @@ public class Kalkulator4 extends HttpServlet {
...
@@ -15,36 +15,28 @@ public class Kalkulator4 extends HttpServlet {
@Override
@Override
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
response
.
setContentType
(
"text/html"
);
PrintWriter
out
=
wyslijNaglowkiIPobierzWritera
(
response
);
response
.
setCharacterEncoding
(
"UTF-8"
);
poczatekHtml
(
out
);
PrintWriter
out
=
response
.
getWriter
();
formularz
(
out
);
out
.
println
(
"<!DOCTYPE html>"
);
koniecHtml
(
out
);
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>"
);
}
}
@Override
@Override
protected
void
doPost
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
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
.
setContentType
(
"text/html"
);
response
.
setCharacterEncoding
(
"UTF-8"
);
response
.
setCharacterEncoding
(
"UTF-8"
);
PrintWriter
out
=
response
.
getWriter
();
return
response
.
getWriter
();
}
private
void
poczatekHtml
(
PrintWriter
out
)
{
out
.
println
(
"<!DOCTYPE html>"
);
out
.
println
(
"<!DOCTYPE html>"
);
out
.
println
(
"<html><head>"
);
out
.
println
(
"<html><head>"
);
out
.
println
(
"<title>Kalkulator serwletowy</title>"
);
out
.
println
(
"<title>Kalkulator serwletowy</title>"
);
...
@@ -52,6 +44,9 @@ public class Kalkulator4 extends HttpServlet {
...
@@ -52,6 +44,9 @@ public class Kalkulator4 extends HttpServlet {
out
.
println
(
"</head>"
);
out
.
println
(
"</head>"
);
out
.
println
(
"<body>"
);
out
.
println
(
"<body>"
);
out
.
println
(
"<h1>Kalkulator</h1>"
);
out
.
println
(
"<h1>Kalkulator</h1>"
);
}
private
void
formularz
(
PrintWriter
out
)
{
out
.
println
(
"<form method='post'>"
);
out
.
println
(
"<form method='post'>"
);
out
.
println
(
"<input type='number' name='liczba1'>"
);
out
.
println
(
"<input type='number' name='liczba1'>"
);
out
.
println
(
"<select name='operacja'>"
);
out
.
println
(
"<select name='operacja'>"
);
...
@@ -63,7 +58,14 @@ public class Kalkulator4 extends HttpServlet {
...
@@ -63,7 +58,14 @@ public class Kalkulator4 extends HttpServlet {
out
.
println
(
"<input type='number' name='liczba2'>"
);
out
.
println
(
"<input type='number' name='liczba2'>"
);
out
.
println
(
"<button>Oblicz</button>"
);
out
.
println
(
"<button>Oblicz</button>"
);
out
.
println
(
"</form>"
);
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
param1
=
request
.
getParameter
(
"liczba1"
);
String
param2
=
request
.
getParameter
(
"liczba2"
);
String
param2
=
request
.
getParameter
(
"liczba2"
);
String
operacja
=
request
.
getParameter
(
"operacja"
);
String
operacja
=
request
.
getParameter
(
"operacja"
);
...
@@ -71,13 +73,7 @@ public class Kalkulator4 extends HttpServlet {
...
@@ -71,13 +73,7 @@ public class Kalkulator4 extends HttpServlet {
try
{
try
{
long
liczba1
=
Long
.
parseLong
(
param1
);
long
liczba1
=
Long
.
parseLong
(
param1
);
long
liczba2
=
Long
.
parseLong
(
param2
);
long
liczba2
=
Long
.
parseLong
(
param2
);
long
wynik
=
switch
(
operacja
)
{
long
wynik
=
oblicz
(
operacja
,
liczba1
,
liczba2
);
case
"+"
->
liczba1
+
liczba2
;
case
"-"
->
liczba1
-
liczba2
;
case
"*"
->
liczba1
*
liczba2
;
case
"/"
->
liczba1
/
liczba2
;
default
->
throw
new
IllegalArgumentException
(
"Nieznane działanie: "
+
operacja
);
};
out
.
printf
(
"<div class='wynik'>%d %s %d = <strong>%d</strong></div>\n"
,
out
.
printf
(
"<div class='wynik'>%d %s %d = <strong>%d</strong></div>\n"
,
liczba1
,
operacja
,
liczba2
,
wynik
);
liczba1
,
operacja
,
liczba2
,
wynik
);
}
catch
(
NumberFormatException
e
)
{
}
catch
(
NumberFormatException
e
)
{
...
@@ -85,8 +81,16 @@ public class Kalkulator4 extends HttpServlet {
...
@@ -85,8 +81,16 @@ public class Kalkulator4 extends HttpServlet {
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
out
.
printf
(
"<div class='error'>Błąd: %s</div>\n"
,
e
.
getMessage
());
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
;
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment