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
e53ca620
Commit
e53ca620
authored
Oct 05, 2022
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Koszyk
parent
a0d112e1
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
184 additions
and
0 deletions
+184
-0
Basket.java
PC30-SklepSpring/src/main/java/sklep/basket/Basket.java
+47
-0
BasketConfiguration.java
...pring/src/main/java/sklep/basket/BasketConfiguration.java
+32
-0
ProductInBasket.java
...lepSpring/src/main/java/sklep/basket/ProductInBasket.java
+75
-0
ProductController.java
...ing/src/main/java/sklep/controller/ProductController.java
+15
-0
product.jsp
...SklepSpring/src/main/webapp/WEB-INF/templates/product.jsp
+1
-0
products.jsp
...klepSpring/src/main/webapp/WEB-INF/templates/products.jsp
+14
-0
No files found.
PC30-SklepSpring/src/main/java/sklep/basket/Basket.java
0 → 100644
View file @
e53ca620
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
();
}
}
PC30-SklepSpring/src/main/java/sklep/basket/BasketConfiguration.java
0 → 100644
View file @
e53ca620
package
sklep
.
basket
;
import
javax.servlet.http.HttpSession
;
import
javax.servlet.http.HttpSessionEvent
;
import
javax.servlet.http.HttpSessionListener
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
@Configuration
public
class
BasketConfiguration
{
// Spring utworzy obiekt klasy BasketConfiguration i wykona metodę listenerSesji.
// Uzyskany w ten sposób obiekt HttpSessionListener zostanie zarejestrowany jako listener sesji,
// a zatem metoda sessionCreated będzie wykonywana za każdym razem, gdy w aplikacji będzie powstawać nowa sesja
// (gdy nowy klient wejdzie na stronę).
@Bean
public
HttpSessionListener
listenerSesji
()
{
return
new
HttpSessionListener
()
{
@Override
public
void
sessionCreated
(
HttpSessionEvent
se
)
{
HttpSession
session
=
se
.
getSession
();
session
.
setAttribute
(
"basket"
,
new
Basket
());
}
@Override
public
void
sessionDestroyed
(
HttpSessionEvent
se
)
{
}
};
}
}
PC30-SklepSpring/src/main/java/sklep/basket/ProductInBasket.java
0 → 100644
View file @
e53ca620
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
;
}
}
PC30-SklepSpring/src/main/java/sklep/controller/ProductController.java
View file @
e53ca620
...
@@ -13,7 +13,10 @@ import org.springframework.validation.BindingResult;
...
@@ -13,7 +13,10 @@ import org.springframework.validation.BindingResult;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.PathVariable
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestHeader
;
import
org.springframework.web.bind.annotation.SessionAttribute
;
import
sklep.basket.Basket
;
import
sklep.model.Product
;
import
sklep.model.Product
;
import
sklep.repository.ProductRepository
;
import
sklep.repository.ProductRepository
;
import
sklep.utils.ExceptionUtils
;
import
sklep.utils.ExceptionUtils
;
...
@@ -95,5 +98,17 @@ public class ProductController {
...
@@ -95,5 +98,17 @@ public class ProductController {
return
"product_form"
;
return
"product_form"
;
}
}
}
}
@GetMapping
(
"/products/{id}/add-to-basket"
)
public
String
addToBasket
(
@SessionAttribute
Basket
basket
,
@PathVariable
(
"id"
)
int
productId
,
@RequestHeader
(
name
=
"Referer"
,
defaultValue
=
"/products"
)
String
adresPowrotu
)
{
Optional
<
Product
>
product
=
productRepository
.
findById
(
productId
);
if
(
product
.
isPresent
())
{
basket
.
addProduct
(
product
.
get
());
}
return
"redirect:"
+
adresPowrotu
;
}
}
}
PC30-SklepSpring/src/main/webapp/WEB-INF/templates/product.jsp
View file @
e53ca620
...
@@ -17,6 +17,7 @@
...
@@ -17,6 +17,7 @@
<p>
Stawka VAT:
<span
class=
"product-price"
>
${product.vat * 100}%
</span></p>
<p>
Stawka VAT:
<span
class=
"product-price"
>
${product.vat * 100}%
</span></p>
<p
class=
"product-description"
>
${product.description}
</p>
<p
class=
"product-description"
>
${product.description}
</p>
<div
class=
"action"
><a
href=
"/products/${product.productId}/edit"
>
Edytuj
</a></div>
<div
class=
"action"
><a
href=
"/products/${product.productId}/edit"
>
Edytuj
</a></div>
<div
class=
"action"
><a
href=
"/products/${product.productId}/add-to-basket"
>
Dodaj do koszyka
</a></div>
</div>
</div>
<p><a
href=
"/products"
>
Przejdź do listy produktów
</a></p>
<p><a
href=
"/products"
>
Przejdź do listy produktów
</a></p>
...
...
PC30-SklepSpring/src/main/webapp/WEB-INF/templates/products.jsp
View file @
e53ca620
...
@@ -8,6 +8,19 @@
...
@@ -8,6 +8,19 @@
<link
rel=
"stylesheet"
type=
"text/css"
href=
"/styl.css"
/>
<link
rel=
"stylesheet"
type=
"text/css"
href=
"/styl.css"
/>
</head>
</head>
<body>
<body>
<c:if
test=
"${not empty basket and not empty basket.elements}"
>
<div
class=
"basket"
>
<h4>
Koszyk
</h4>
<ul>
<c:forEach
var=
"p"
items=
"${basket.elements}"
>
<li>
${p.productName}: ${p.quantity} × ${p.price} =
<b>
${p.value}
</b></li>
</c:forEach>
</ul>
<p>
Wartość koszyka: ${basket.totalValue}
</p>
</div>
</c:if>
<h1>
Wszystkie produkty
</h1>
<h1>
Wszystkie produkty
</h1>
<c:forEach
var=
"product"
items=
"${products}"
>
<c:forEach
var=
"product"
items=
"${products}"
>
...
@@ -16,6 +29,7 @@
...
@@ -16,6 +29,7 @@
<p>
Cena:
<span
class=
"product-price"
>
${product.price}
</span></p>
<p>
Cena:
<span
class=
"product-price"
>
${product.price}
</span></p>
<p
class=
"product-description"
>
${product.description}
</p>
<p
class=
"product-description"
>
${product.description}
</p>
<div
class=
"action"
><a
href=
"/products/${product.productId}/edit"
>
Edytuj
</a></div>
<div
class=
"action"
><a
href=
"/products/${product.productId}/edit"
>
Edytuj
</a></div>
<div
class=
"action"
><a
href=
"/products/${product.productId}/add-to-basket"
>
Dodaj do koszyka
</a></div>
</div>
</div>
</c:forEach>
</c:forEach>
...
...
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