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
b5a738e0
Commit
b5a738e0
authored
Sep 28, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
switch expression i ładniejsze komunikaty
parent
6023d5a7
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
17 deletions
+15
-17
Kalkulator2.java
PC25-Serwlety/src/main/java/kalkulator/Kalkulator2.java
+15
-17
No files found.
PC25-Serwlety/src/main/java/kalkulator/Kalkulator2.java
View file @
b5a738e0
...
@@ -13,6 +13,10 @@ import javax.servlet.http.HttpServletResponse;
...
@@ -13,6 +13,10 @@ import javax.servlet.http.HttpServletResponse;
public
class
Kalkulator2
extends
HttpServlet
{
public
class
Kalkulator2
extends
HttpServlet
{
private
static
final
long
serialVersionUID
=
1L
;
private
static
final
long
serialVersionUID
=
1L
;
// doGet wykona się po raz pierwszy, gdy wejdziemy pod adres Kalkulator2
// i wyświetli pusty formularz (wtedy parametry są nullami i nie wchodzimy do ifa)
// i wykona się po raz drugi po naciśnięciu guzika Oblicz i wysłaniu zapytania z parametrami
// - wtedy już wejdziemy do if-a i wyświetlimy wynik
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
response
.
setContentType
(
"text/html"
);
response
.
setContentType
(
"text/html"
);
response
.
setCharacterEncoding
(
"utf-8"
);
response
.
setCharacterEncoding
(
"utf-8"
);
...
@@ -47,25 +51,19 @@ public class Kalkulator2 extends HttpServlet {
...
@@ -47,25 +51,19 @@ public class Kalkulator2 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
;
long
wynik
=
switch
(
operacja
)
{
switch
(
operacja
)
{
case
"+"
->
liczba1
+
liczba2
;
case
"+"
:
wynik
=
liczba1
+
liczba2
;
case
"-"
->
liczba1
-
liczba2
;
break
;
case
"*"
->
liczba1
*
liczba2
;
case
"-"
:
wynik
=
liczba1
-
liczba2
;
case
"/"
->
liczba1
/
liczba2
;
break
;
default
->
throw
new
IllegalArgumentException
(
"Nieznane działanie: "
+
operacja
);
case
"*"
:
wynik
=
liczba1
*
liczba2
;
};
break
;
out
.
printf
(
"<div class='wynik'>%d %s %d = <strong>%d</strong></div>\n"
,
case
"/"
:
wynik
=
liczba1
/
liczba2
;
liczba1
,
operacja
,
liczba2
,
wynik
);
break
;
default
:
throw
new
IllegalArgumentException
(
"Nieznane działanie: "
+
operacja
);
}
out
.
println
(
"<div class='wynik'>Suma: "
+
wynik
+
"</div>"
);
}
catch
(
NumberFormatException
e
)
{
}
catch
(
NumberFormatException
e
)
{
out
.
print
ln
(
"<div class='error'>Niepoprawny format liczby</div>
"
);
out
.
print
f
(
"<div class='error'>Niepoprawny format liczby</div>\n
"
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
out
.
print
ln
(
"<div class='error'>Błąd: "
+
e
.
getMessage
()
+
"</div>"
);
out
.
print
f
(
"<div class='error'>Błąd: %s</div>\n"
,
e
.
getMessage
()
);
}
}
}
}
out
.
println
(
"</body>"
);
out
.
println
(
"</body>"
);
...
...
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