Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20240528-BJava
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
20240528-BJava
Commits
c3aa8913
Commit
c3aa8913
authored
Jun 13, 2024
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Obsługa koszyka
parent
3170b44e
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
288 additions
and
0 deletions
+288
-0
AddToBasket.java
PC24-SklepWeb/src/main/java/sklep/basket/AddToBasket.java
+38
-0
Basket.java
PC24-SklepWeb/src/main/java/sklep/basket/Basket.java
+57
-0
BasketListener.java
PC24-SklepWeb/src/main/java/sklep/basket/BasketListener.java
+34
-0
ProductInBasket.java
...-SklepWeb/src/main/java/sklep/basket/ProductInBasket.java
+75
-0
RemoveFromBasket.java
...SklepWeb/src/main/java/sklep/basket/RemoveFromBasket.java
+30
-0
products8.jsp
PC24-SklepWeb/src/main/webapp/products8.jsp
+54
-0
No files found.
PC24-SklepWeb/src/main/java/sklep/basket/AddToBasket.java
0 → 100644
View file @
c3aa8913
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
{
@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"
);
// Zakładamy, że obiekt basket został dodany do sesji przez BasketListener
basket
.
addProduct
(
product
);
}
}
catch
(
Exception
e
)
{
// ignorujemy błędy
}
// Przekierowanie - każemy przeglądarce wejść pod ten adres.
response
.
sendRedirect
(
"products8.jsp"
);
}
}
PC24-SklepWeb/src/main/java/sklep/basket/Basket.java
0 → 100644
View file @
c3aa8913
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
void
removeProduct
(
int
productId
)
{
ProductInBasket
p
=
elementy
.
get
(
productId
);
if
(
p
!=
null
)
{
if
(
p
.
getQuantity
()
>
1
)
{
p
.
increaseQuantity
(-
1
);
}
else
{
elementy
.
remove
(
productId
);
}
}
}
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
();
}
}
PC24-SklepWeb/src/main/java/sklep/basket/BasketListener.java
0 → 100644
View file @
c3aa8913
package
sklep
.
basket
;
import
jakarta.servlet.ServletContextEvent
;
import
jakarta.servlet.ServletContextListener
;
import
jakarta.servlet.annotation.WebListener
;
import
jakarta.servlet.http.HttpSession
;
import
jakarta.servlet.http.HttpSessionEvent
;
import
jakarta.servlet.http.HttpSessionListener
;
@WebListener
public
class
BasketListener
implements
HttpSessionListener
,
ServletContextListener
{
public
void
sessionCreated
(
HttpSessionEvent
se
)
{
HttpSession
sesja
=
se
.
getSession
();
sesja
.
setMaxInactiveInterval
(
30
);
// pół minuty i sesja wygasa
System
.
out
.
println
(
"sessionCreated "
+
sesja
.
getId
());
Basket
basket
=
new
Basket
();
sesja
.
setAttribute
(
"basket"
,
basket
);
}
public
void
sessionDestroyed
(
HttpSessionEvent
se
)
{
HttpSession
sesja
=
se
.
getSession
();
System
.
out
.
println
(
"sessionDestroyed "
+
sesja
.
getId
());
}
public
void
contextInitialized
(
ServletContextEvent
sce
)
{
System
.
out
.
println
(
"contextInitialized"
);
}
public
void
contextDestroyed
(
ServletContextEvent
sce
)
{
System
.
out
.
println
(
"contextDestroyed"
);
}
}
PC24-SklepWeb/src/main/java/sklep/basket/ProductInBasket.java
0 → 100644
View file @
c3aa8913
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
;
}
}
PC24-SklepWeb/src/main/java/sklep/basket/RemoveFromBasket.java
0 → 100644
View file @
c3aa8913
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
;
@WebServlet
(
"/remove_from_basket"
)
public
class
RemoveFromBasket
extends
HttpServlet
{
@Override
protected
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
try
{
int
productId
=
Integer
.
parseInt
(
request
.
getParameter
(
"productId"
));
HttpSession
sesja
=
request
.
getSession
();
Basket
basket
=
(
Basket
)
sesja
.
getAttribute
(
"basket"
);
basket
.
removeProduct
(
productId
);
}
catch
(
Exception
e
)
{
// ignorujemy błędy
}
// Przekierowanie - każemy przeglądarce wejść pod ten adres.
response
.
sendRedirect
(
"products8.jsp"
);
}
}
PC24-SklepWeb/src/main/webapp/products8.jsp
0 → 100644
View file @
c3aa8913
<
%@
page
contentType=
"text/html;charset=UTF-8"
language=
"java"
%
>
<
%@
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>
<a
href=
"remove_from_basket?productId=${elm.productId}"
>
(–)
</a></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=
"bean"
class=
"sklep.web.ProductBean"
/>
<jsp:setProperty
name=
"bean"
property=
"minPrice"
param=
"min_price"
/>
<jsp:setProperty
name=
"bean"
property=
"maxPrice"
param=
"max_price"
/>
<c:forEach
var=
"product"
items=
"${bean.filteredProducts}"
>
<div
class=
"product"
>
<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
class=
"action"
><a
href=
"edit?productId=${product.productId}"
>
edytuj
</a></div>
</div>
</c:forEach>
<div
class=
"action"
><a
href=
"edit"
>
Dodaj nowy produkt
</a></div>
</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