Skip to content

Commit 260d934

Browse files
authored
Merge pull request #39 from CodecoolGlobal/development
Development
2 parents 297385b + 47e6c38 commit 260d934

41 files changed

Lines changed: 1105 additions & 283 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
# Maven folders
66

77
target/
8-
test/
98

109
# Miscellaneous
1110

Binary file not shown.

pom.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
<properties>
1212
<maven.compiler.source>1.8</maven.compiler.source>
1313
<maven.compiler.target>1.8</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1415
</properties>
1516

1617
<dependencies>
1718
<dependency>
1819
<groupId>junit</groupId>
1920
<artifactId>junit</artifactId>
20-
<version>3.8.1</version>
21+
<version>4.12</version>
2122
<scope>test</scope>
2223
</dependency>
2324

@@ -26,6 +27,15 @@
2627
<artifactId>sqlite-jdbc</artifactId>
2728
<version>3.25.2</version>
2829
</dependency>
30+
31+
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
32+
<dependency>
33+
<groupId>org.junit.jupiter</groupId>
34+
<artifactId>junit-jupiter-api</artifactId>
35+
<version>5.4.0</version>
36+
<scope>test</scope>
37+
</dependency>
38+
2939
</dependencies>
3040
<build>
3141
<plugins>

src/main/java/com/codecool/onlineshop/controller/AdminController.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,25 @@ public void handleAdminInput() {
5959
view.displayNotImplementedMessage();
6060
break;
6161
case 9:
62+
adminService.displayAllCustomers();
63+
adminService.updateUserDetails();
64+
view.getEmptyInput();
65+
break;
66+
case 10:
67+
adminService.displayAllCustomers();
68+
adminService.deleteUser();
69+
view.getEmptyInput();
70+
break;
71+
case 11:
72+
adminService.displayOrdersHistory();
73+
adminService.updateOrderStatus();
74+
view.getEmptyInput();
75+
break;
76+
case 12:
6277
isRunning = false;
6378
break;
6479
default:
6580
break;
66-
6781
}
6882
}
69-
7083
}

src/main/java/com/codecool/onlineshop/controller/CustomerController.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ public void handleCustomerinput() {
3939
customerService.handleAddProduct();
4040
break;
4141
case 3:
42+
customerService.DisplayAllProductsInBasket();
4243
customerService.handleDeleteProduct();
4344
break;
4445
case 4:
45-
OrdersDaoImpl orders = new OrdersDaoImpl();
46-
orders.addOrder(customer);
46+
customerService.placeOrderIfBasketNotEmpty();
4747
break;
4848
case 5:
4949
customerService.displayOrdersHistory();
@@ -56,14 +56,22 @@ public void handleCustomerinput() {
5656
view.getEmptyInput();
5757
break;
5858
case 7:
59-
customerService.displayProductsBycategory();
59+
customerService.displayProductsByCategory();
6060
view.getEmptyInput();
6161
break;
6262
case 8:
6363
customerService.DisplayAllProductsInBasket();
6464
customerService.editProductQuantity();
6565
break;
6666
case 9:
67+
customerService.displayAllProductsInShop();
68+
customerService.handleRateProduct();
69+
break;
70+
case 10:
71+
customerService.changePassword();
72+
view.getEmptyInput();
73+
break;
74+
case 11:
6775
isRunning = false;
6876
break;
6977
default:

src/main/java/com/codecool/onlineshop/controller/LoginController.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public void handleLoginController() {
2929
int userInput = view.getIntegerInput();
3030
switch (userInput) {
3131
case 1:
32-
chooseController(handleLogin());;
32+
List<User> users = usersDao.getUserData();
33+
chooseController(handleLogin(users));
3334
break;
3435
case 2:
3536
handleAddNewUser();
@@ -44,10 +45,10 @@ public void handleLoginController() {
4445
}
4546
}
4647

47-
public User handleLogin() {
48+
public User handleLogin(List<User> usersList) {
4849
boolean isLogging = true;
4950
this.usersDao = new UsersDaoImpl();
50-
List<User> users = usersDao.getUserData();
51+
List<User> users = usersList;
5152
while (isLogging) {
5253
view.showMessage("Name: ");
5354
String name = view.getStringInput();

src/main/java/com/codecool/onlineshop/dao/Connector.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import java.sql.SQLException;
66

77
public class Connector {
8-
Connection connection;
9-
String databasePath;
8+
private Connection connection;
9+
private String databasePath;
1010

1111
public Connector(String databasePath) {
1212
this.databasePath = databasePath;

src/main/java/com/codecool/onlineshop/dao/OrdersDao.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ public interface OrdersDao {
99

1010
public List<Order> getOrderData();
1111
public void addOrder(User user);
12+
public void updateOrderStatus(int orderId, String status);
1213
}

src/main/java/com/codecool/onlineshop/dao/OrdersDaoImpl.java

Lines changed: 80 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,22 @@
77
import java.time.LocalDate;
88

99
import java.sql.Connection;
10+
import java.sql.PreparedStatement;
1011
import java.sql.ResultSet;
1112
import java.sql.Statement;
1213
import java.sql.SQLException;
1314

14-
import com.codecool.onlineshop.dao.Connector;
1515
import com.codecool.onlineshop.model.Order;
16+
import com.codecool.onlineshop.model.OrderStatus;
1617
import com.codecool.onlineshop.model.Product;
1718
import com.codecool.onlineshop.model.User;
1819

1920
public class OrdersDaoImpl implements OrdersDao {
20-
private Connector connector;
21+
2122
private Connection connection;
22-
private Statement statement;
23-
private ResultSet resultSet;
24-
private Order order;
2523
private List<Order> orders;
2624

2725
public OrdersDaoImpl() {
28-
connector = new Connector("src/main/resources/databases/OnlineShop.db");
29-
connection = connector.getDatabaseConnection();
3026
orders = new ArrayList<>();
3127
addOrderData();
3228
}
@@ -40,48 +36,93 @@ public List<Order> getOrderData() {
4036
public void addOrder(User user) {
4137
Iterator<Product> basketIterator = user.getBasket().getIterator();
4238
LocalDate localDate = LocalDate.now();
43-
String date = localDate.getDayOfMonth() + "-" + localDate.getMonthValue() + "-" + localDate.getYear();
44-
Product currentProduct;
45-
int productId;
46-
int productAmount;
47-
int productAmountPrice;
48-
int userId;
49-
Integer orderId;
50-
String productName;
5139

52-
if (orders.isEmpty()) {
53-
orderId = 1;
54-
} else {
55-
orders.sort(Order::compareTo);
56-
orderId = orders.get(orders.size()-1).getOrderId() + 1;
57-
}
40+
connection = initializeConnection();
41+
PreparedStatement insertOrder;
42+
String insertOrderString = "INSERT INTO OrderDetails"
43+
+ "(OrderID, ProductID, ProductName, ProductAmount, ProductAmountPrice, UserID, Date, Status)"
44+
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
5845

46+
String date = localDate.getDayOfMonth() + "-" + localDate.getMonthValue() + "-" + localDate.getYear();
47+
int orderId = getIncrementedOrderId();
5948
while (basketIterator.hasNext()) {
60-
currentProduct = basketIterator.next();
61-
productId = currentProduct.getId();
62-
productName = currentProduct.getName();
63-
productAmount = currentProduct.getAmount();
64-
productAmountPrice = currentProduct.getAmount() * currentProduct.getPrice();
65-
userId = user.getId();
49+
Product currentProduct = basketIterator.next();
50+
String productName = currentProduct.getName();
51+
int productId = currentProduct.getId();
52+
int productAmount = currentProduct.getAmount();
53+
int productAmountPrice = currentProduct.getAmount() * currentProduct.getPrice();
54+
int userId = user.getId();
6655

6756
try {
68-
statement = connection.createStatement();
69-
String insertInto = "INSERT INTO OrderDetails (OrderID, ProductID, ProductName, ProductAmount, ProductAmountPrice, UserID, Date) VALUES ("
70-
+ orderId + "," + productId + ",\"" + productName + "\"," + productAmount + "," + productAmountPrice + "," + userId
71-
+ ",\"" + date + "\");";
72-
statement.executeUpdate(insertInto);
73-
statement.close();
57+
insertOrder = connection.prepareStatement(insertOrderString);
58+
insertOrder.setInt(1, orderId);
59+
insertOrder.setInt(2, productId);
60+
insertOrder.setString(3, productName);
61+
insertOrder.setInt(4, productAmount);
62+
insertOrder.setInt(5, productAmountPrice);
63+
insertOrder.setInt(6, userId);
64+
insertOrder.setString(7, date);
65+
insertOrder.setString(8, OrderStatus.PENDING.name());
66+
insertOrder.executeUpdate();
67+
insertOrder.close();
68+
connection.close();
7469
} catch (SQLException e) {
7570
e.printStackTrace();
7671
}
7772
}
7873
}
7974

80-
private List<Order> addOrderData() {
75+
@Override
76+
public void updateOrderStatus(int orderId, String status) {
77+
connection = initializeConnection();
78+
PreparedStatement updateOrderStatus;
79+
String updateOrderStatusString = "UPDATE OrderDetails SET Status = ? WHERE OrderID = ?";
80+
try {
81+
updateOrderStatus = connection.prepareStatement(updateOrderStatusString);
82+
updateOrderStatus.setInt(2, orderId);
83+
updateOrderStatus.setString(1, status);
84+
updateOrderStatus.executeUpdate();
85+
updateOrderStatus.close();
86+
connection.close();
87+
} catch (SQLException e) {
88+
e.printStackTrace();
89+
}
90+
}
91+
92+
public boolean isValid(int orderId) {
93+
for (Order order : orders) {
94+
if (order.getOrderId() == orderId) {
95+
return true;
96+
}
97+
}
98+
return false;
99+
}
100+
101+
private int getIncrementedOrderId() {
102+
int orderId;
103+
104+
if (orders.isEmpty()) {
105+
orderId = 1;
106+
} else {
107+
orders.sort(Order::compareTo);
108+
orderId = orders.get(orders.size()-1).getOrderId() + 1;
109+
}
110+
return orderId;
111+
}
112+
113+
private Connection initializeConnection() {
114+
final String DATABASEPATH = "src/main/resources/databases/OnlineShop.db";
115+
Connector connector = new Connector(DATABASEPATH);
116+
return connector.getDatabaseConnection();
117+
}
118+
119+
private void addOrderData() {
120+
connection = initializeConnection();
121+
Order order;
81122

82123
try {
83-
statement = connection.createStatement();
84-
resultSet = statement.executeQuery("SELECT * FROM OrderDetails;");
124+
Statement statement = connection.createStatement();
125+
ResultSet resultSet = statement.executeQuery("SELECT * FROM OrderDetails;");
85126
while (resultSet.next()) {
86127
int orderId = resultSet.getInt("OrderID");
87128
int productId = resultSet.getInt("ProductID");
@@ -90,6 +131,7 @@ private List<Order> addOrderData() {
90131
int productAmountPrice = resultSet.getInt("ProductAmountPrice");
91132
int userId = resultSet.getInt("UserID");
92133
String date = resultSet.getString("Date");
134+
String status = resultSet.getString("Status");
93135

94136
order = new Order.Builder()
95137
.withOrderId(orderId)
@@ -99,14 +141,15 @@ private List<Order> addOrderData() {
99141
.withProductAmountPrice(productAmountPrice)
100142
.withUserId(userId)
101143
.withDate(date)
144+
.withStatus(status)
102145
.build();
103146
orders.add(order);
104147
}
105148
resultSet.close();
106149
statement.close();
150+
connection.close();
107151
} catch (SQLException e) {
108152
e.printStackTrace();
109153
}
110-
return orders;
111154
}
112155
}

0 commit comments

Comments
 (0)