Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
java_dzienna_15_09
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_dzienna_15_09
Commits
4506f643
Commit
4506f643
authored
Sep 28, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refaktoryzacja kalkulatora
parent
0e9e6483
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
97 additions
and
1 deletions
+97
-1
Kalkulator3.java
PC25-Serwlety/src/main/java/kalkulator/Kalkulator3.java
+1
-1
Kalkulator4.java
PC25-Serwlety/src/main/java/kalkulator/Kalkulator4.java
+96
-0
No files found.
PC25-Serwlety/src/main/java/kalkulator/Kalkulator3.java
View file @
4506f643
...
...
@@ -31,8 +31,8 @@ public class Kalkulator3 extends HttpServlet {
out
.
println
(
"</head>"
);
out
.
println
(
"<body>"
);
out
.
println
(
"<h1>Kalkulator 3</h1>"
);
out
.
println
(
"<p>Wpisz liczby i wybierz działanie:</p>"
);
out
.
println
(
"<form method='post'>"
);
out
.
println
(
"<p>Wpisz liczby i wybierz działanie:</p>"
);
out
.
println
(
"<input name='liczba1' type='number'>"
);
out
.
println
(
"<select name='operacja'>"
);
out
.
println
(
"<option value='+'>+</option>"
);
...
...
PC25-Serwlety/src/main/java/kalkulator/Kalkulator4.java
0 → 100644
View file @
4506f643
package
kalkulator
;
import
java.io.IOException
;
import
java.io.PrintWriter
;
import
javax.servlet.ServletException
;
import
javax.servlet.annotation.WebServlet
;
import
javax.servlet.http.HttpServlet
;
import
javax.servlet.http.HttpServletRequest
;
import
javax.servlet.http.HttpServletResponse
;
@WebServlet
(
"/Kalkulator4"
)
public
class
Kalkulator4
extends
HttpServlet
{
private
static
final
long
serialVersionUID
=
1L
;
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
PrintWriter
out
=
naglowkiHTTP
(
response
);
poczatekHTML
(
out
);
wypiszFormularz
(
out
);
koniecHTML
(
out
);
}
protected
void
doPost
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
PrintWriter
out
=
naglowkiHTTP
(
response
);
poczatekHTML
(
out
);
wypiszFormularz
(
out
);
pobierzParametryIOblicz
(
request
,
out
);
koniecHTML
(
out
);
}
private
PrintWriter
naglowkiHTTP
(
HttpServletResponse
response
)
throws
IOException
{
response
.
setContentType
(
"text/html"
);
response
.
setCharacterEncoding
(
"utf-8"
);
PrintWriter
out
=
response
.
getWriter
();
return
out
;
}
private
void
poczatekHTML
(
PrintWriter
out
)
{
out
.
println
(
"<!DOCTYPE html>"
);
out
.
println
(
"<html>"
);
out
.
println
(
"<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 4</h1>"
);
}
private
void
koniecHTML
(
PrintWriter
out
)
{
out
.
println
(
"</body>"
);
out
.
println
(
"</html>"
);
}
private
void
wypiszFormularz
(
PrintWriter
out
)
{
out
.
println
(
"<form method='post'>"
);
out
.
println
(
"<p>Wpisz liczby i wybierz działanie:</p>"
);
out
.
println
(
"<input name='liczba1' type='number'>"
);
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 name='liczba2' type='number'>"
);
out
.
println
(
"<button>=</button>"
);
out
.
println
(
"</form>"
);
}
private
void
pobierzParametryIOblicz
(
HttpServletRequest
request
,
PrintWriter
out
)
{
String
param1
=
request
.
getParameter
(
"liczba1"
);
String
param2
=
request
.
getParameter
(
"liczba2"
);
String
operacja
=
request
.
getParameter
(
"operacja"
);
try
{
long
liczba1
=
Long
.
parseLong
(
param1
);
long
liczba2
=
Long
.
parseLong
(
param2
);
long
wynik
=
oblicz
(
liczba1
,
liczba2
,
operacja
);
out
.
printf
(
"<div class='wynik'>%d %s %d = <strong>%d</strong></div>\n"
,
liczba1
,
operacja
,
liczba2
,
wynik
);
}
catch
(
NumberFormatException
e
)
{
out
.
printf
(
"<div class='error'>Niepoprawny format liczby</div>\n"
);
}
catch
(
Exception
e
)
{
out
.
printf
(
"<div class='error'>Błąd: %s</div>\n"
,
e
.
getMessage
());
}
}
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
->
throw
new
IllegalArgumentException
(
"Nieznane działanie: "
+
operacja
);
};
}
}
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