-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
272 changed files
with
6,712 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
/.idea/ | ||
.DS_Store | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# [YourSmartHouse](https://gdonisi.github.io/YourSmartHouse) | ||
|
||
Demo of my University Project for "Software Technologies for the Web" class. Original source can be found [here](https://github.com/giovannidonisi/YourSmartHouse). | ||
Demo of my University Project for "Software Technologies for the Web" class. |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> | ||
<component name="FacetManager"> | ||
<facet type="web" name="Web"> | ||
<configuration> | ||
<descriptors> | ||
<deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/src/main/webapp/WEB-INF/web.xml" /> | ||
</descriptors> | ||
<webroots> | ||
<root url="file://$MODULE_DIR$/src/main/webapp" relative="/" /> | ||
</webroots> | ||
<sourceRoots> | ||
<root url="file://$MODULE_DIR$/src/main/java" /> | ||
<root url="file://$MODULE_DIR$/src/main/resources" /> | ||
</sourceRoots> | ||
</configuration> | ||
</facet> | ||
</component> | ||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_11"> | ||
<output url="file://$MODULE_DIR$/target/classes" /> | ||
<output-test url="file://$MODULE_DIR$/target/test-classes" /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" /> | ||
<excludeFolder url="file://$MODULE_DIR$/target" /> | ||
</content> | ||
<orderEntry type="jdk" jdkName="15" jdkType="JavaSDK" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="module-library" exported=""> | ||
<library> | ||
<CLASSES> | ||
<root url="file://$MODULE_DIR$/src/main/webapp/WEB-INF/lib" /> | ||
</CLASSES> | ||
<JAVADOC /> | ||
<SOURCES /> | ||
<jarDirectory url="file://$MODULE_DIR$/src/main/webapp/WEB-INF/lib" recursive="false" /> | ||
</library> | ||
</orderEntry> | ||
<orderEntry type="library" name="jquery-3.4.1" level="application" /> | ||
<orderEntry type="library" name="font-awesome" level="application" /> | ||
<orderEntry type="library" name="e176238445" level="application" /> | ||
<orderEntry type="library" name="jquery" level="application" /> | ||
<orderEntry type="library" name="all" level="application" /> | ||
<orderEntry type="library" scope="PROVIDED" name="Maven: javax.servlet:javax.servlet-api:4.0.1" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-api:5.7.1" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.apiguardian:apiguardian-api:1.1.0" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.opentest4j:opentest4j:1.2.0" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-commons:1.7.1" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.junit.jupiter:junit-jupiter-engine:5.7.1" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.junit.platform:junit-platform-engine:1.7.1" level="project" /> | ||
</component> | ||
</module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>GiovanniDonisi</groupId> | ||
<artifactId>YourSmartHouse</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
<name>YourSmartHouse</name> | ||
<packaging>war</packaging> | ||
|
||
<properties> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<junit.version>5.7.1</junit.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>javax.servlet-api</artifactId> | ||
<version>4.0.1</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-api</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.junit.jupiter</groupId> | ||
<artifactId>junit-jupiter-engine</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-war-plugin</artifactId> | ||
<version>3.3.1</version> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>11</source> | ||
<target>11</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package carrello; | ||
|
||
import prodotto.Prodotto; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Optional; | ||
|
||
public class Carrello { | ||
private ArrayList<OggettoCarrello> oggetti; | ||
|
||
public Carrello() { | ||
super(); | ||
this.oggetti = new ArrayList<>(); | ||
} | ||
|
||
public Carrello(ArrayList<OggettoCarrello> o){ | ||
this.oggetti = o; | ||
} | ||
|
||
public boolean addProduct(Prodotto p, int q) { | ||
Optional<OggettoCarrello> opt = find(p.getProductId()); | ||
if(opt.isPresent()) { | ||
opt.get().setQuantita(q); | ||
return true; | ||
} | ||
else | ||
return oggetti.add(new OggettoCarrello(p, q)); | ||
} | ||
|
||
public boolean removeProduct(int id) { | ||
return oggetti.removeIf(it -> it.getProdotto().getProductId() == id); | ||
} | ||
|
||
public Optional<OggettoCarrello> find(int id) { | ||
return oggetti.stream().filter(it -> it.getProdotto().getProductId() == id).findFirst(); | ||
} | ||
|
||
public void reset() { | ||
oggetti.clear(); | ||
} | ||
|
||
public ArrayList<OggettoCarrello> getOggetti() { | ||
return oggetti; | ||
} | ||
|
||
public void setOggetti(ArrayList<OggettoCarrello> oggetti) { | ||
this.oggetti = oggetti; | ||
} | ||
|
||
public double getTotale() { | ||
double x = 0.0; | ||
for(OggettoCarrello o : oggetti){ | ||
x += o.totale(); | ||
} | ||
x = Math.round(x * 100); | ||
return x/100; // Mi assicuro che i double abbiano 2 cifre dopo la virgola | ||
} | ||
|
||
public int getQuantita() { | ||
int a = 0; | ||
for(OggettoCarrello o : oggetti) { | ||
a += o.getQuantita(); | ||
} | ||
return a; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package carrello; | ||
|
||
import http.Controller; | ||
import http.InvalidRequestException; | ||
import prodotto.Prodotto; | ||
import prodotto.ProdottoDAO; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.annotation.WebServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.sql.SQLException; | ||
|
||
@WebServlet(name = "CartServlet", value = "/cart/*") | ||
public class CartServlet extends Controller { | ||
|
||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
String path = getPath(req); | ||
switch (path) { | ||
case "/": | ||
case "/show": | ||
Carrello c = getSessionCart(req.getSession(false)); | ||
if(c == null) { | ||
c = new Carrello(); | ||
req.getSession(false).setAttribute("utenteCarrello", c); | ||
} | ||
req.getRequestDispatcher(view("utente/carrello")).forward(req, resp); | ||
break; | ||
default: | ||
resp.sendError(404); | ||
} | ||
} | ||
|
||
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
try { | ||
String path = getPath(req); | ||
switch (path) { | ||
case "/add": | ||
add(req, resp); | ||
break; | ||
case "/remove": | ||
remove(req, resp); | ||
break; | ||
case "/empty": | ||
Carrello c = getSessionCart(req.getSession(false)); | ||
if(c != null) | ||
c.reset(); | ||
resp.sendRedirect("/YourSmartHouse/cart/show"); | ||
break; | ||
default: | ||
resp.sendError(405); | ||
break; | ||
} | ||
} catch (InvalidRequestException e) { | ||
e.handle(req, resp); | ||
} | ||
} | ||
|
||
private void add(HttpServletRequest req, HttpServletResponse resp) throws InvalidRequestException { | ||
try { | ||
String id = req.getParameter("id"); | ||
String quant = req.getParameter("quantita"); | ||
if (!isNumber(id) || !isNumber(quant)) | ||
throw new InvalidRequestException(500); | ||
|
||
ProdottoDAO dao = new ProdottoDAO(); | ||
Prodotto p = dao.fetchProduct(Integer.parseInt(id)); | ||
if(p != null) { | ||
int quantita = Integer.parseInt(quant); | ||
if(getSessionCart(req.getSession(false)) == null) | ||
req.getSession(false).setAttribute("utenteCarrello", new Carrello()); | ||
|
||
getSessionCart(req.getSession(false)).addProduct(p, quantita); | ||
resp.sendRedirect("/YourSmartHouse/products/show?id="+id); | ||
} else | ||
throw new InvalidRequestException(404); | ||
} catch (SQLException | IOException throwables) { | ||
throwables.printStackTrace(); | ||
throw new InvalidRequestException(500); | ||
} | ||
} | ||
|
||
private void remove(HttpServletRequest req, HttpServletResponse resp) throws InvalidRequestException { | ||
try { | ||
String id = req.getParameter("id"); | ||
if (!isNumber(id)) | ||
throw new InvalidRequestException(500); | ||
|
||
if(getSessionCart(req.getSession(false)).removeProduct(Integer.parseInt(id))) | ||
resp.sendRedirect("/YourSmartHouse/cart/show"); | ||
else | ||
throw new InvalidRequestException(404); | ||
} catch (IOException e) { | ||
throw new InvalidRequestException(500); | ||
} | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
YourSmartHouse/src/main/java/carrello/OggettoCarrello.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package carrello; | ||
|
||
import prodotto.Prodotto; | ||
|
||
public class OggettoCarrello { | ||
private final Prodotto prodotto; | ||
private int quantita; | ||
|
||
public OggettoCarrello(Prodotto prodotto, int quantita) { | ||
this.prodotto = prodotto; | ||
this.quantita = quantita; | ||
} | ||
|
||
public void setQuantita(int quantita) { | ||
this.quantita = quantita; | ||
} | ||
|
||
public Prodotto getProdotto() { | ||
return prodotto; | ||
} | ||
|
||
public int getQuantita() { | ||
return quantita; | ||
} | ||
|
||
public double totale() { | ||
double n = prodotto.getPrezzo()*quantita; | ||
n = Math.round(n * 100); | ||
return n/100; // Mi assicuro che i double abbiano 2 cifre dopo la virgola | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package categoria; | ||
|
||
import org.json.JSONObject; | ||
import prodotto.Prodotto; | ||
|
||
import javax.servlet.http.Part; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.file.Files; | ||
import java.util.ArrayList; | ||
|
||
public class Categoria { | ||
private int categoryId; | ||
private String nome, foto; | ||
private ArrayList<Prodotto> prodotti; | ||
|
||
public Categoria() { | ||
super(); | ||
} | ||
|
||
public int getCategoryId() { | ||
return categoryId; | ||
} | ||
|
||
public void setCategoryId(int categoryId) { | ||
this.categoryId = categoryId; | ||
} | ||
|
||
public String getNome() { | ||
return nome; | ||
} | ||
|
||
public void setNome(String nome) { | ||
this.nome = nome; | ||
} | ||
|
||
public ArrayList<Prodotto> getProdotti() { | ||
return prodotti; | ||
} | ||
|
||
public void setProdotti(ArrayList<Prodotto> prodotti) { | ||
this.prodotti = prodotti; | ||
} | ||
|
||
public String getFoto() { | ||
return foto; | ||
} | ||
|
||
public void setFoto(String foto) { | ||
this.foto = foto; | ||
} | ||
|
||
public void writeFoto(String up, Part st) throws IOException { | ||
try(InputStream filestream = st.getInputStream()) { | ||
File file = new File(up+foto); | ||
Files.copy(filestream, file.toPath()); | ||
} | ||
} | ||
|
||
public JSONObject toJSON() { | ||
JSONObject object = new JSONObject(); | ||
object.put("id", categoryId); | ||
object.put("nome", nome); | ||
object.put("foto", foto); | ||
return object; | ||
} | ||
|
||
} |
Oops, something went wrong.