Skip to content

Commit 0f0f64c

Browse files
author
sarah
committed
First commit
0 parents  commit 0f0f64c

25 files changed

+1824
-0
lines changed

.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Eclipse
2+
.classpath
3+
.project
4+
.settings/
5+
6+
# Server stuff
7+
Servers/
8+
9+
# Intellij
10+
.idea/
11+
*.iml
12+
*.iws
13+
14+
# Mac
15+
.DS_Store
16+
17+
# Maven
18+
log/
19+
target/
20+
21+
# Default
22+
/build/
23+
/target/
24+
/.metadata/
25+
/.recommenders/

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Java-BookStoreShoppingCart

book_store.db

8 KB
Binary file not shown.

pom.xml

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.pluralsight</groupId>
5+
<artifactId>bookstore</artifactId>
6+
<packaging>war</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>bookstore Maven Webapp</name>
9+
<url>http://maven.apache.org</url>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
14+
<maven.compiler.source>1.8</maven.compiler.source>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
<failOnMissingWebXml>false</failOnMissingWebXml>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>javax.servlet</groupId>
22+
<artifactId>javax.servlet-api</artifactId>
23+
<version>3.1.0</version>
24+
<scope>provided</scope>
25+
</dependency>
26+
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
27+
<dependency>
28+
<groupId>javax.servlet</groupId>
29+
<artifactId>jstl</artifactId>
30+
<version>1.2</version>
31+
</dependency>
32+
<!-- https://mvnrepository.com/artifact/javax.inject/javax.inject -->
33+
<dependency>
34+
<groupId>javax.inject</groupId>
35+
<artifactId>javax.inject</artifactId>
36+
<version>1</version>
37+
</dependency>
38+
39+
40+
<!-- https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc -->
41+
<dependency>
42+
<groupId>org.xerial</groupId>
43+
<artifactId>sqlite-jdbc</artifactId>
44+
<version>3.21.0.1</version>
45+
</dependency>
46+
47+
48+
<!-- Unit and Integration Tests -->
49+
<dependency>
50+
<groupId>junit</groupId>
51+
<artifactId>junit</artifactId>
52+
<version>4.12</version>
53+
<scope>test</scope>
54+
</dependency>
55+
<dependency>
56+
<groupId>net.sourceforge.htmlunit</groupId>
57+
<artifactId>htmlunit</artifactId>
58+
<version>2.15</version>
59+
<scope>test</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.mockito</groupId>
63+
<artifactId>mockito-core</artifactId>
64+
<version>2.18.0</version>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>org.powermock</groupId>
69+
<artifactId>powermock-api-mockito2</artifactId>
70+
<version>2.0.0-beta.5</version>
71+
</dependency>
72+
<dependency>
73+
<groupId>org.powermock</groupId>
74+
<artifactId>powermock-module-junit4</artifactId>
75+
<version>2.0.0-beta.5</version>
76+
</dependency>
77+
78+
</dependencies>
79+
80+
<build>
81+
<finalName>bookstore</finalName>
82+
<plugins>
83+
<plugin>
84+
<groupId>org.apache.tomcat.maven</groupId>
85+
<artifactId>tomcat7-maven-plugin</artifactId>
86+
<version>2.2</version>
87+
<configuration>
88+
<port>8080</port>
89+
<path>/</path>
90+
</configuration>
91+
</plugin>
92+
</plugins>
93+
</build>
94+
95+
<profiles>
96+
<profile>
97+
<id>integration</id>
98+
<build>
99+
<!-- Integration Tests -->
100+
<plugins>
101+
<plugin>
102+
<groupId>org.apache.maven.plugins</groupId>
103+
<artifactId>maven-failsafe-plugin</artifactId>
104+
<version>2.18.1</version>
105+
<executions>
106+
<execution>
107+
<goals>
108+
<goal>integration-test</goal>
109+
<goal>verify</goal>
110+
</goals>
111+
<configuration>
112+
<systemPropertyVariables>
113+
<!-- Remember to deploy the app and start the server in advance -->
114+
<integration.base.url>http://localhost:8080</integration.base.url>
115+
</systemPropertyVariables>
116+
</configuration>
117+
</execution>
118+
</executions>
119+
</plugin>
120+
</plugins>
121+
</build>
122+
</profile>
123+
</profiles>
124+
125+
</project>

projects-cli.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "tagPattern": "_\\w+" }
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.pluralsight;
2+
3+
public class Book {
4+
int id;
5+
String title;
6+
String author;
7+
float price;
8+
9+
public Book(String title, String author, float price) {
10+
this.title = title;
11+
this.author = author;
12+
this.price = price;
13+
}
14+
15+
public Book(int id, String title, String author, float price) {
16+
this.id = id;
17+
this.title = title;
18+
this.author = author;
19+
this.price = price;
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return "(" + title + ", " + author + ", " + price + ")";
25+
}
26+
27+
public int getId() {
28+
return id;
29+
}
30+
public void setId(int id) {
31+
this.id = id;
32+
}
33+
public String getTitle() {
34+
return title;
35+
}
36+
public void setTitle(String title) {
37+
this.title = title;
38+
}
39+
public String getAuthor() {
40+
return author;
41+
}
42+
public void setAuthor(String author) {
43+
this.author = author;
44+
}
45+
public float getPrice() {
46+
return price;
47+
}
48+
public void setPrice(float price) {
49+
this.price = price;
50+
}
51+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package com.pluralsight;
2+
3+
import java.sql.Connection;
4+
import java.sql.DatabaseMetaData;
5+
import java.sql.DriverManager;
6+
import java.sql.PreparedStatement;
7+
import java.sql.ResultSet;
8+
import java.sql.SQLException;
9+
import java.sql.Statement;
10+
11+
import java.util.ArrayList;
12+
13+
public class BookDAO {
14+
private Connection jdbcConnection;
15+
public BookDAO(Connection connection)
16+
{
17+
jdbcConnection = connection;
18+
}
19+
20+
public Book getBook(int id) {
21+
Book book = null;
22+
String sql = "SELECT * FROM book WHERE id = ?";
23+
24+
try {
25+
PreparedStatement statement = jdbcConnection.prepareStatement(sql);
26+
statement.setInt(1, id);
27+
28+
ResultSet resultSet = statement.executeQuery();
29+
30+
if (resultSet.next()) {
31+
String title = resultSet.getString("title");
32+
String author = resultSet.getString("author");
33+
float price = resultSet.getFloat("price");
34+
35+
book = new Book(id, title, author, price);
36+
}
37+
38+
resultSet.close();
39+
statement.close();
40+
} catch (SQLException e) {
41+
e.printStackTrace();
42+
}
43+
44+
return book;
45+
}
46+
47+
public ArrayList<Book> listAllBooks() {
48+
ArrayList<Book> listBook = new ArrayList<>();
49+
50+
String sql = "SELECT * FROM book";
51+
52+
try {
53+
Statement statement = jdbcConnection.createStatement();
54+
55+
ResultSet resultSet = statement.executeQuery(sql);
56+
57+
while (resultSet.next()) {
58+
int id = resultSet.getInt("id");
59+
String title = resultSet.getString("title");
60+
String author = resultSet.getString("author");
61+
float price = resultSet.getFloat("price");
62+
63+
Book book = new Book(id, title, author, price);
64+
listBook.add(book);
65+
}
66+
67+
resultSet.close();
68+
statement.close();
69+
} catch (SQLException e) {
70+
e.printStackTrace();
71+
}
72+
return listBook;
73+
}
74+
75+
public boolean insertBook(Book book) {
76+
String sql = "INSERT INTO book (title, author, price) VALUES (?, ?, ?)";
77+
78+
try {
79+
PreparedStatement statement = jdbcConnection.prepareStatement(sql);
80+
statement.setString(1, book.getTitle());
81+
statement.setString(2, book.getAuthor());
82+
statement.setFloat(3, book.getPrice());
83+
84+
boolean rowInserted = statement.executeUpdate() > 0;
85+
statement.close();
86+
return rowInserted;
87+
} catch (SQLException e) {
88+
e.printStackTrace();
89+
}
90+
91+
return false;
92+
}
93+
94+
public void deleteBook(int id) {
95+
String sql = "DELETE FROM book WHERE id = ?";
96+
97+
try {
98+
PreparedStatement statement = jdbcConnection.prepareStatement(sql);
99+
statement.setInt(1, id);
100+
statement.executeUpdate();
101+
102+
statement.close();
103+
} catch (SQLException e) {
104+
e.printStackTrace();
105+
}
106+
}
107+
108+
public void updateBook(Book book) {
109+
String sql = "UPDATE book SET title = ?, author = ?, price = ?" +
110+
" WHERE id = ?";
111+
112+
try {
113+
PreparedStatement statement = jdbcConnection.prepareStatement(sql);
114+
statement.setString(1, book.getTitle());
115+
statement.setString(2, book.getAuthor());
116+
statement.setFloat(3, book.getPrice());
117+
statement.setInt(4, book.getId());
118+
119+
statement.executeUpdate();
120+
statement.close();
121+
} catch(SQLException e) {
122+
e.printStackTrace();
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)