You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
5.3 KiB

package model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Goods {
private String id;
private String name;
private String specification;
private String unit;
private double price;
private String category;
private int quantity;
private String remark;
private LocalDateTime inboundTime;
private String operator;
private String supplier;
public Goods(String id, String name, String specification, String unit, double price, String category) {
this.id = id;
this.name = name;
this.specification = specification;
this.unit = unit;
this.price = price;
this.category = category;
this.quantity = 0;
this.remark = "";
this.inboundTime = null;
this.operator = "";
this.supplier = "";
}
public Goods(String id, String name, String specification, String unit, double price, String category, int quantity, String remark) {
this.id = id;
this.name = name;
this.specification = specification;
this.unit = unit;
this.price = price;
this.category = category;
this.quantity = quantity;
this.remark = remark;
this.inboundTime = null;
this.operator = "";
this.supplier = "";
}
public Goods(String id, String goodsName, int quantity, String remark) {
this.id = id;
this.name = goodsName;
this.quantity = quantity;
this.remark = remark;
this.inboundTime = null;
this.operator = "";
this.supplier = "";
}
public static void insertIntoDatabase(Connection conn, Goods goods) throws SQLException {
String sql = "INSERT INTO goods (id, name, specification, unit, price, category, quantity, remark, inbound_time, operator, supplier) " +
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
try {
pstmt.setString(1, goods.getId());
} catch (SQLException e) {
throw new RuntimeException(e);
}
pstmt.setString(2, goods.getName());
pstmt.setString(3, goods.getSpecification());
pstmt.setString(4, goods.getUnit());
pstmt.setDouble(5, goods.getPrice());
pstmt.setString(6, goods.getCategory());
pstmt.setInt(7, goods.getQuantity());
pstmt.setString(8, goods.getRemark());
pstmt.setString(9, goods.getInboundTime() != null ? goods.getInboundTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) : null);
pstmt.setString(10, goods.getOperator());
pstmt.setString(11, goods.getSupplier());
pstmt.executeUpdate();
}
}
public static void updateInDatabase(Connection conn, Goods goods) throws SQLException {
String sql = "UPDATE goods SET name = ?, specification = ?, unit = ?, price = ?, category = ?, " +
"quantity = ?, remark = ?, inbound_time = ?, operator = ?, supplier = ? WHERE id = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, goods.getName());
pstmt.setString(2, goods.getSpecification());
pstmt.setString(3, goods.getUnit());
pstmt.setDouble(4, goods.getPrice());
pstmt.setString(5, goods.getCategory());
pstmt.setInt(6, goods.getQuantity());
pstmt.setString(7, goods.getRemark());
pstmt.setString(8, goods.getInboundTime() != null ? goods.getInboundTime().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) : null);
pstmt.setString(9, goods.getOperator());
pstmt.setString(10, goods.getSupplier());
pstmt.setString(11, goods.getId());
pstmt.executeUpdate();
}
}
// Getters and Setters
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getSpecification() { return specification; }
public void setSpecification(String specification) { this.specification = specification; }
public String getUnit() { return unit; }
public void setUnit(String unit) { this.unit = unit; }
public double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
public String getCategory() { return category; }
public void setCategory(String category) { this.category = category; }
public int getQuantity() { return quantity; }
public void setQuantity(int quantity) { this.quantity = quantity; }
public String getRemark() { return remark; }
public void setRemark(String remark) { this.remark = remark; }
public LocalDateTime getInboundTime() { return inboundTime; }
public void setInboundTime(LocalDateTime inboundTime) { this.inboundTime = inboundTime; }
public String getOperator() { return operator; }
public void setOperator(String operator) { this.operator = operator; }
public String getSupplier() { return supplier; }
public void setSupplier(String supplier) { this.supplier = supplier; }
}