Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230928
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
javab_20230928
Commits
6bfda870
Commit
6bfda870
authored
Oct 18, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Koszyk - wersja 1
parent
3bd089b6
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
220 additions
and
0 deletions
+220
-0
AddToBasket.java
PC25-SklepWeb/src/main/java/sklep/basket/AddToBasket.java
+43
-0
Basket.java
PC25-SklepWeb/src/main/java/sklep/basket/Basket.java
+47
-0
ProductInBasket.java
...-SklepWeb/src/main/java/sklep/basket/ProductInBasket.java
+75
-0
products8.jsp
PC25-SklepWeb/src/main/webapp/products8.jsp
+55
-0
No files found.
PC25-SklepWeb/src/main/java/sklep/basket/AddToBasket.java
0 → 100644
View file @
6bfda870
package
sklep
.
basket
;
import
java.io.IOException
;
import
jakarta.servlet.ServletException
;
import
jakarta.servlet.annotation.WebServlet
;
import
jakarta.servlet.http.HttpServlet
;
import
jakarta.servlet.http.HttpServletRequest
;
import
jakarta.servlet.http.HttpServletResponse
;
import
jakarta.servlet.http.HttpSession
;
import
sklep.db.DBConnection
;
import
sklep.db.ProductDAO
;
import
sklep.model.Product
;
@WebServlet
(
"/add_to_basket"
)
public
class
AddToBasket
extends
HttpServlet
{
private
static
final
long
serialVersionUID
=
1L
;
@Override
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
try
{
int
productId
=
Integer
.
parseInt
(
request
.
getParameter
(
"productId"
));
try
(
DBConnection
db
=
DBConnection
.
open
())
{
ProductDAO
productDAO
=
db
.
productDAO
();
Product
product
=
productDAO
.
findById
(
productId
);
HttpSession
sesja
=
request
.
getSession
();
Basket
basket
=
(
Basket
)
sesja
.
getAttribute
(
"basket"
);
// jeśli w sesji nie ma jeszcze obiektu o tej nazwie, to wynikiem jest null
if
(
basket
==
null
)
{
basket
=
new
Basket
();
sesja
.
setAttribute
(
"basket"
,
basket
);
}
basket
.
addProduct
(
product
);
}
}
catch
(
Exception
e
)
{
// ignorujemy błędy
}
// Przekierowanie - każemy przeglądarce wejść pod ten adres.
response
.
sendRedirect
(
"products8.jsp"
);
}
}
PC25-SklepWeb/src/main/java/sklep/basket/Basket.java
0 → 100644
View file @
6bfda870
package
sklep
.
basket
;
import
java.math.BigDecimal
;
import
java.util.Collection
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.Map
;
import
sklep.model.Product
;
public
class
Basket
{
private
final
Map
<
Integer
,
ProductInBasket
>
elementy
=
new
HashMap
<>();
public
synchronized
void
addProduct
(
Product
product
,
int
quantity
)
{
if
(
elementy
.
containsKey
(
product
.
getProductId
()))
{
// jeśli w słowniku jest już taki element, to tylko zwiększamy ilość
elementy
.
get
(
product
.
getProductId
()).
increaseQuantity
(
quantity
);
}
else
{
// jeśli jeszcze nie ma, to tworzymy
elementy
.
put
(
product
.
getProductId
(),
new
ProductInBasket
(
product
.
getProductId
(),
product
.
getProductName
(),
product
.
getPrice
(),
quantity
));
}
}
public
synchronized
void
addProduct
(
Product
product
)
{
// "domyślną ilością, o którą zwiększamy, jest 1"
addProduct
(
product
,
1
);
}
public
synchronized
Collection
<
ProductInBasket
>
getElements
()
{
return
Collections
.
unmodifiableCollection
(
elementy
.
values
());
}
public
synchronized
BigDecimal
getTotalValue
()
{
return
getElements
().
stream
()
.
map
(
ProductInBasket:
:
getValue
)
.
reduce
(
BigDecimal
.
ZERO
,
BigDecimal:
:
add
);
}
@Override
public
synchronized
String
toString
()
{
return
"Koszyk o rozmiarze "
+
getElements
().
size
()
+
" i wartości "
+
getTotalValue
();
}
}
PC25-SklepWeb/src/main/java/sklep/basket/ProductInBasket.java
0 → 100644
View file @
6bfda870
package
sklep
.
basket
;
import
java.math.BigDecimal
;
import
java.util.Objects
;
public
class
ProductInBasket
{
private
int
productId
;
private
String
productName
;
private
BigDecimal
price
;
private
int
quantity
;
public
ProductInBasket
(
int
productId
,
String
productName
,
BigDecimal
price
,
int
quantity
)
{
this
.
productId
=
productId
;
this
.
productName
=
productName
;
this
.
price
=
price
;
this
.
quantity
=
quantity
;
}
public
BigDecimal
getPrice
()
{
return
price
;
}
public
void
setPrice
(
BigDecimal
price
)
{
this
.
price
=
price
;
}
public
int
getQuantity
()
{
return
quantity
;
}
public
void
setQuantity
(
int
quantity
)
{
this
.
quantity
=
quantity
;
}
public
int
getProductId
()
{
return
productId
;
}
public
String
getProductName
()
{
return
productName
;
}
@Override
public
String
toString
()
{
return
"ElementKoszyka [productId="
+
productId
+
", productName="
+
productName
+
", price="
+
price
+
", quantity="
+
quantity
+
"]"
;
}
@Override
public
int
hashCode
()
{
return
Objects
.
hash
(
price
,
productId
,
productName
,
quantity
);
}
@Override
public
boolean
equals
(
Object
obj
)
{
if
(
this
==
obj
)
return
true
;
if
(
obj
==
null
)
return
false
;
if
(
getClass
()
!=
obj
.
getClass
())
return
false
;
ProductInBasket
other
=
(
ProductInBasket
)
obj
;
return
Objects
.
equals
(
price
,
other
.
price
)
&&
productId
==
other
.
productId
&&
Objects
.
equals
(
productName
,
other
.
productName
)
&&
quantity
==
other
.
quantity
;
}
public
BigDecimal
getValue
()
{
return
price
.
multiply
(
BigDecimal
.
valueOf
(
quantity
));
}
public
void
increaseQuantity
(
int
change
)
{
quantity
+=
change
;
}
}
PC25-SklepWeb/src/main/webapp/products8.jsp
0 → 100644
View file @
6bfda870
<
%@
page
language=
"java"
contentType=
"text/html; charset=UTF-8"
pageEncoding=
"UTF-8"
%
>
<
%@
taglib
prefix=
"c"
uri=
"jakarta.tags.core"
%
>
<!DOCTYPE html>
<html>
<head>
<meta
charset=
"UTF-8"
>
<title>
Lista produktów 8
</title>
<link
rel=
"stylesheet"
type=
"text/css"
href=
"styl.css"
>
</head>
<body>
<h1>
Lista produktów - wersja 8
</h1>
<div
class=
"koszyk"
>
<h4>
Koszyk
</h4>
<ul>
<
%
--
Zauwa
ż
my
,
ż
e
dla
obiektu
koszyk
nie
wykonujemy
ju
ż
useBean
.
Po
prostu
zak
ł
adamy
,
ż
e
jest
obecny
(
w
sesji
).
Gdyby
go
nie
by
ł
o
,
to
p
ę
tla
si
ę
nie
wykona
.
--
%
>
<c:forEach
var=
"elm"
items=
"${basket.elements}"
>
<li>
${elm.productName} (${elm.quantity}) za
<b>
${elm.value}
</b></li>
</c:forEach>
</ul>
<p
class=
"total"
>
Do zapłaty: ${basket.totalValue}
</p>
</div>
<form
id=
"wyszukiwarka"
method=
"get"
>
<h2>
Filtr cen
</h2>
<table
class=
"formularz"
>
<tr><td><label
for=
"min_price"
>
Cena minimalna:
</label></td>
<td><input
id=
"min_price"
name=
"min_price"
type=
"number"
value=
"${param.min_price}"
></td></tr>
<tr><td><label
for=
"max_price"
>
Cena maksymalna:
</label></td>
<td><input
id=
"max_price"
name=
"max_price"
type=
"number"
value=
"${param.max_price}"
></td></tr>
<tr><td><button>
Filtruj
</button></td></tr>
</table>
</form>
<jsp:useBean
id=
"productBean"
class=
"sklep.web.ProductBean"
/>
<jsp:setProperty
name=
"productBean"
property=
"minPrice"
param=
"min_price"
/>
<jsp:setProperty
name=
"productBean"
property=
"maxPrice"
param=
"max_price"
/>
<c:forEach
var=
"product"
items=
"${productBean.filteredProducts}"
>
<div
class=
"product"
>
<img
class=
"photo"
src=
"photo?productId=${product.productId}"
alt=
""
/>
<h3>
${product.productName}
</h3>
<div
class=
"price"
>
Cena: ${product.price}
</div>
<div
class=
"price"
>
VAT ${product.vat * 100}%
</div>
<c:if
test=
"${not empty(product.description)}"
>
<p
class=
"description"
>
${product.description}
</p>
</c:if>
<div
class=
"action"
><a
href=
"add_to_basket?productId=${product.productId}"
>
dodaj do koszyka
</a></div>
</div>
</c:forEach>
</body>
</html>
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