-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSalesManager.java
More file actions
46 lines (38 loc) · 1.69 KB
/
Copy pathSalesManager.java
File metadata and controls
46 lines (38 loc) · 1.69 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
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class SalesManager {
// Method to add a sale to the database
public void addSale(int cashierID, double saleAmount) {
String sql = "INSERT INTO Sales (cashierID, saleAmount, saleDate) VALUES (?, ?, NOW())";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, cashierID); // Set the cashier ID
pstmt.setDouble(2, saleAmount); // Set the sale amount
pstmt.executeUpdate();
System.out.println("Sale added successfully!");
} catch (SQLException e) {
e.printStackTrace();
}
}
// Method to view sales records
public void viewSales() {
String sql = "SELECT * FROM Sales";
try (Connection conn = DatabaseConnection.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery()) {
System.out.println("Sales Records:");
while (rs.next()) {
int saleID = rs.getInt("saleID");
int cashierID = rs.getInt("cashierID");
double saleAmount = rs.getDouble("saleAmount");
String saleDate = rs.getString("saleDate");
System.out.printf("Sale ID: %d | Cashier ID: %d | Amount: %.2f | Date: %s%n",
saleID, cashierID, saleAmount, saleDate);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}