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
9f1048a1
Commit
9f1048a1
authored
Oct 09, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Kalkulator v1 i 2
parent
ce2eecf4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
111 additions
and
0 deletions
+111
-0
Kalkulator1.java
PC23-Serwlety/src/main/java/kalkulator/Kalkulator1.java
+45
-0
Kalkulator2.java
PC23-Serwlety/src/main/java/kalkulator/Kalkulator2.java
+66
-0
No files found.
PC23-Serwlety/src/main/java/kalkulator/Kalkulator1.java
0 → 100644
View file @
9f1048a1
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
(
"/Kalkulator1"
)
public
class
Kalkulator1
extends
HttpServlet
{
private
static
final
long
serialVersionUID
=
1L
;
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
(
"</head>"
);
out
.
println
(
"<body>"
);
out
.
println
(
"<h1>Kalkulator</h1>"
);
out
.
println
(
"<form>"
);
out
.
println
(
"<label for='liczba1'>Liczba 1:</label>"
);
out
.
println
(
"<input type='number' name='liczba1'>"
);
out
.
println
(
"<label for='liczba2'>Liczba 2:</label>"
);
out
.
println
(
"<input type='number' name='liczba2'>"
);
out
.
println
(
"<button>Dodaj</button>"
);
out
.
println
(
"</form>"
);
String
parametr1
=
request
.
getParameter
(
"liczba1"
);
String
parametr2
=
request
.
getParameter
(
"liczba2"
);
if
(
parametr1
!=
null
&&
parametr2
!=
null
)
{
long
liczba1
=
Long
.
parseLong
(
parametr1
);
long
liczba2
=
Long
.
parseLong
(
parametr2
);
long
wynik
=
liczba1
+
liczba2
;
out
.
println
(
"<div>"
+
liczba1
+
" + "
+
liczba2
+
" = "
+
wynik
+
"</p>"
);
}
}
}
PC23-Serwlety/src/main/java/kalkulator/Kalkulator2.java
0 → 100644
View file @
9f1048a1
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
;
import
org.apache.commons.lang3.StringUtils
;
@WebServlet
(
"/Kalkulator2"
)
public
class
Kalkulator2
extends
HttpServlet
{
private
static
final
long
serialVersionUID
=
1L
;
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>"
);
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>"
);
String
param1
=
request
.
getParameter
(
"liczba1"
);
String
param2
=
request
.
getParameter
(
"liczba2"
);
String
operacja
=
request
.
getParameter
(
"operacja"
);
if
(
StringUtils
.
isNoneBlank
(
param1
,
param2
,
operacja
))
{
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
);
};
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
());
}
}
}
}
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