-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransactionService.java
More file actions
79 lines (68 loc) · 2.8 KB
/
Copy pathTransactionService.java
File metadata and controls
79 lines (68 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class TransactionService {
private StudentDAO studentDAO;
private ProductDAO productDAO;
public TransactionService() {
this.studentDAO = new StudentDAO();
this.productDAO = new ProductDAO();
}
public synchronized Response processPurchase(String studentId, String machineId,
String productId) {
Connection conn = null;
try {
conn = DatabaseManager.getConnection();
conn.setAutoCommit(false);
BigDecimal balance = studentDAO.getBalance(studentId);
ProductDTO product = productDAO.getProduct(productId);
if (product == null) {
conn.rollback();
return new Response(false, "Product not found");
}
if (balance.compareTo(product.getPrice()) < 0) {
conn.rollback();
return new Response(false, "Insufficient funds");
}
if (!productDAO.decrementInventory(machineId, productId)) {
conn.rollback();
return new Response(false, "Product out of stock");
}
BigDecimal newBalance = balance.subtract(product.getPrice());
if (!studentDAO.updateBalance(studentId, newBalance)) {
conn.rollback();
return new Response(false, "Failed to update balance");
}
String sql = "INSERT INTO transactions (student_id, machine_id, product_id, amount, status) " +
"VALUES (?, ?, ?, ?, 'COMPLETED')";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, studentId);
stmt.setString(2, machineId);
stmt.setString(3, productId);
stmt.setBigDecimal(4, product.getPrice());
stmt.executeUpdate();
}
conn.commit();
Response response = new Response(true, "Purchase successful");
response.setData("newBalance", newBalance);
response.setData("productName", product.getName());
return response;
} catch (SQLException e) {
if (conn != null) {
try { conn.rollback(); } catch (SQLException ex) { ex.printStackTrace(); }
}
e.printStackTrace();
return new Response(false, "Transaction failed: " + e.getMessage());
} finally {
if (conn != null) {
try {
conn.setAutoCommit(true);
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}