库存查询功能更新,查询库存

master
Your Name 6 months ago
parent a62d0f6a25
commit 398e7c5244

@ -1,41 +1,62 @@
package model; package model;
import java.util.HashMap; import java.time.LocalDateTime;
import java.util.Map;
public class InventoryManager { public class InventoryManager {
private Map<String, Integer> inventory; private String id;
private String goodsId;
private int quantity;
private String supplier;
private LocalDateTime inboundTime;
private LocalDateTime outboundTime;
private String operator;
private String remark;
private String customer;
public InventoryManager() { public InventoryManager(String id, String goodsId, int quantity, String supplier, LocalDateTime inboundTime, LocalDateTime outboundTime, String operator, String remark, String customer) {
inventory = new HashMap<>(); this.id = id;
this.goodsId = goodsId;
this.quantity = quantity;
this.supplier = supplier;
this.inboundTime = inboundTime;
this.operator = operator;
this.remark = remark;
this.customer = customer;
} }
public void addOrUpdateInventory(String goodsId, int quantity) { public String getId() {
inventory.put(goodsId, inventory.getOrDefault(goodsId, 0) + quantity); return id;
} }
public void removeOrUpdateInventory(String goodsId, int quantity) { public String getGoodsId() {
if (inventory.containsKey(goodsId)) { return goodsId;
int currentQuantity = inventory.get(goodsId);
if (currentQuantity >= quantity) {
inventory.put(goodsId, currentQuantity - quantity);
} else {
throw new IllegalArgumentException("库存不足");
} }
} else {
throw new IllegalArgumentException("货物不存在"); public int getQuantity() {
return quantity;
}
public String getSupplier() {
return supplier;
} }
public LocalDateTime getInboundTime() {
return inboundTime;
}
public LocalDateTime getOutboundTime() {
return outboundTime;
} }
public int getInventoryQuantity(String goodsId) { public String getOperator() {
return inventory.getOrDefault(goodsId, 0); return operator;
} }
public void processInboundRecord(InboundRecord record) { public String getRemark() {
addOrUpdateInventory(record.getGoodsId(), record.getQuantity()); return remark;
} }
public void processOutboundRecord(OutboundRecord record) { public String getCustomer() {
removeOrUpdateInventory(record.getGoodsId(), record.getQuantity()); return customer;
} }
} }

@ -1,24 +1,28 @@
package view; package view;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Insets; import javafx.geometry.Insets;
import javafx.geometry.Pos; import javafx.geometry.Pos;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.*; import javafx.scene.layout.*;
import model.Goods;
import model.InventoryManager; import model.InventoryManager;
public class InventoryView extends BorderPane { import java.sql.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class InventoryView extends BorderPane {
private TextField goodsField; private TextField goodsField;
private Label resultLabel;
private TableView<InventoryManager> tableView;
private TextField customerField;
private TextField quantityField; private TextField quantityField;
private TextField remarkField; private TextField remarkField;
private TableView<Goods> tableView;
private Label totalValue;
private final InventoryManager inventoryManager;
public InventoryView() { public InventoryView() {
inventoryManager = new InventoryManager();
initializeUI(); initializeUI();
} }
@ -27,165 +31,143 @@ public class InventoryView extends BorderPane {
VBox topArea = createTopArea(); VBox topArea = createTopArea();
setTop(topArea); setTop(topArea);
// 中间表格区域 // 中间结果显示区域
tableView = createTableView(); tableView = createTableView();
setCenter(tableView); setCenter(tableView);
}
private TableView<InventoryManager> createTableView() {
TableView<InventoryManager> table = new TableView<>();
TableColumn<InventoryManager, String> idCol = new TableColumn<>("单号");
idCol.setCellValueFactory(new PropertyValueFactory<>("id"));
idCol.setPrefWidth(150);
TableColumn<InventoryManager, String> goodsCol = new TableColumn<>("货物名称");
goodsCol.setCellValueFactory(new PropertyValueFactory<>("goodsId"));
goodsCol.setPrefWidth(120);
// 底部汇总区域 TableColumn<InventoryManager, Integer> quantityCol = new TableColumn<>("数量");
HBox bottomArea = createBottomArea(); quantityCol.setCellValueFactory(new PropertyValueFactory<>("quantity"));
setBottom(bottomArea); quantityCol.setPrefWidth(80);
TableColumn<InventoryManager, String> supplierCol = new TableColumn<>("供应商");
supplierCol.setCellValueFactory(new PropertyValueFactory<>("supplier"));
supplierCol.setPrefWidth(120);
TableColumn<InventoryManager, String> operatorCol = new TableColumn<>("操作人员");
operatorCol.setCellValueFactory(new PropertyValueFactory<>("operator"));
operatorCol.setPrefWidth(100);
TableColumn<InventoryManager, String> remarkCol = new TableColumn<>("备注");
remarkCol.setCellValueFactory(new PropertyValueFactory<>("remark"));
remarkCol.setPrefWidth(150);
table.getColumns().addAll(idCol, goodsCol, quantityCol, supplierCol, operatorCol, remarkCol);
table.setOnMouseClicked(event -> {
if (event.getClickCount() == 2) {
InventoryManager selectedRecord = table.getSelectionModel().getSelectedItem();
if (selectedRecord != null) {
fillFields(selectedRecord);
}
} }
});
return table;
}
private VBox createTopArea() { private VBox createTopArea() {
VBox topArea = new VBox(10); VBox topArea = new VBox(10);
topArea.setPadding(new Insets(10)); topArea.setPadding(new Insets(10));
// 第一行:基本信息 // 基本信息
GridPane basicInfo = new GridPane(); GridPane basicInfo = new GridPane();
basicInfo.setHgap(10); basicInfo.setHgap(10);
basicInfo.setVgap(10); basicInfo.setVgap(10);
// 第一行:货物名称
Label goodsLabel = new Label("货物名称:"); Label goodsLabel = new Label("货物名称:");
goodsField = new TextField(); goodsField = new TextField();
goodsField.setPromptText("请输入货物名称"); goodsField.setPromptText("请输入货物名称");
Label quantityLabel = new Label("数量:"); basicInfo.addRow(0, goodsLabel, goodsField);
quantityField = new TextField();
quantityField.setPromptText("请输入数量");
Label remarkLabel = new Label("备注:");
remarkField = new TextField();
remarkField.setPromptText("请输入备注信息");
basicInfo.addRow(0, goodsLabel, goodsField, quantityLabel, quantityField);
basicInfo.addRow(1, remarkLabel, remarkField);
// 设置列宽 // 设置列宽
ColumnConstraints column1 = new ColumnConstraints(); ColumnConstraints column1 = new ColumnConstraints();
column1.setMinWidth(80); column1.setMinWidth(80);
ColumnConstraints column2 = new ColumnConstraints(); ColumnConstraints column2 = new ColumnConstraints();
column2.setMinWidth(200); column2.setMinWidth(200);
ColumnConstraints column3 = new ColumnConstraints();
column3.setMinWidth(80);
ColumnConstraints column4 = new ColumnConstraints();
column4.setMinWidth(200);
basicInfo.getColumnConstraints().addAll(column1, column2, column3, column4); basicInfo.getColumnConstraints().addAll(column1, column2);
// 操作按钮 // 操作按钮
HBox buttonBox = new HBox(10); HBox buttonBox = new HBox(10);
buttonBox.setAlignment(Pos.CENTER_RIGHT); buttonBox.setAlignment(Pos.CENTER_RIGHT);
buttonBox.setPadding(new Insets(10, 0, 0, 0)); buttonBox.setPadding(new Insets(10, 0, 0, 0));
Button addButton = new Button("添加库存"); Button queryButton = new Button("查询库存");
addButton.setOnAction(e -> handleAdd()); queryButton.setOnAction(e -> handleQuery());
Button clearButton = new Button("清空"); buttonBox.getChildren().add(queryButton);
clearButton.setOnAction(e -> handleClear());
buttonBox.getChildren().addAll(addButton, clearButton);
topArea.getChildren().addAll(basicInfo, buttonBox); topArea.getChildren().addAll(basicInfo, buttonBox);
return topArea; return topArea;
} }
private TableView<Goods> createTableView() { private void handleQuery() {
TableView<Goods> table = new TableView<>();
TableColumn<Goods, String> idCol = new TableColumn<>("单号");
idCol.setCellValueFactory(new PropertyValueFactory<>("id"));
TableColumn<Goods, String> nameCol = new TableColumn<>("货物名称");
nameCol.setCellValueFactory(new PropertyValueFactory<>("name"));
TableColumn<Goods, Integer> quantityCol = new TableColumn<>("数量");
quantityCol.setCellValueFactory(new PropertyValueFactory<>("quantity"));
TableColumn<Goods, String> remarkCol = new TableColumn<>("备注");
remarkCol.setCellValueFactory(new PropertyValueFactory<>("remark"));
table.getColumns().addAll(idCol, nameCol, quantityCol, remarkCol);
table.getItems().addListener((javafx.collections.ListChangeListener.Change<? extends Goods> c) -> {
updateTotal();
});
return table;
}
private HBox createBottomArea() {
HBox bottomArea = new HBox(10);
bottomArea.setPadding(new Insets(10));
bottomArea.setAlignment(Pos.CENTER_RIGHT);
Label totalLabel = new Label("库存总数量:");
totalValue = new Label("0");
Label unitLabel = new Label("件");
bottomArea.getChildren().addAll(totalLabel, totalValue, unitLabel);
return bottomArea;
}
private void updateTotal() {
int total = tableView.getItems().stream()
.mapToInt(Goods::getQuantity)
.sum();
totalValue.setText(String.valueOf(total));
}
private void handleAdd() {
try {
String goodsName = goodsField.getText().trim(); String goodsName = goodsField.getText().trim();
String quantityText = quantityField.getText().trim(); if (goodsName.isEmpty()) {
String remark = remarkField.getText().trim(); showAlert("错误", "请输入货物名称!");
// 输入验证
if (goodsName.isEmpty() || quantityText.isEmpty()) {
showAlert("错误", "请填写完整的库存信息!");
return; return;
} }
int quantity; Platform.runLater(() -> {
try { try {
quantity = Integer.parseInt(quantityText); String url = "jdbc:sqlite:db/dbuml.db3";
if (quantity <= 0) { try (Connection conn = DriverManager.getConnection(url)) {
showAlert("错误", "数量必须大于0"); // 查询当前库存
return; String sql = "SELECT * FROM warehouse WHERE goods_id = ?";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, goodsName);
ResultSet rs = stmt.executeQuery();
// 获取当前表格中的数据
ObservableList<InventoryManager> existingData = tableView.getItems();
existingData.clear(); // 清空现有数据
boolean found = false;
while (rs.next()) {
String id = rs.getString("id");
String goodsId = rs.getString("goods_id");
int quantity = rs.getInt("quantity");
String supplier = rs.getString("supplier");
String operator = rs.getString("operator");
String remark = rs.getString("remark");
InventoryManager record = new InventoryManager(
id,
goodsId,
quantity,
supplier,
null, // 入库时间为 null
null, // 出库时间为 null
operator,
remark,
null // 客户为 null
);
// 将新的记录添加到现有数据中
existingData.add(record);
found = true;
} }
} catch (NumberFormatException e) { if (!found) {
showAlert("错误", "请输入有效的数量!"); showAlert("错误", "未找到该货物!");
return;
} }
// 创建货物对象
String id = String.format("NO%014d", System.currentTimeMillis());
Goods goods = new Goods(id, goodsName, quantity, remark);
// 添加到库存管理器
inventoryManager.addOrUpdateInventory(goods.getId(), quantity);
// 添加到表格
tableView.getItems().add(goods);
// 更新总计
updateTotal();
// 清空输入
handleClear();
showAlert("成功", "库存记录已保存!");
} catch (Exception e) {
showAlert("错误", "保存失败:" + e.getMessage());
} }
} }
} catch (Exception e) {
private void handleClear() { e.printStackTrace();
goodsField.clear(); resultLabel.setText("查询失败:" + e.getMessage());
quantityField.clear(); }
remarkField.clear(); });
} }
private void showAlert(String title, String content) { private void showAlert(String title, String content) {
Alert alert = new Alert(Alert.AlertType.INFORMATION); Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(title); alert.setTitle(title);
@ -193,4 +175,12 @@ public class InventoryView extends BorderPane {
alert.setContentText(content); alert.setContentText(content);
alert.showAndWait(); alert.showAndWait();
} }
// 加填充字段的方法
private void fillFields(InventoryManager record) {
goodsField.setText(record.getGoodsId());
customerField.setText(record.getCustomer());
quantityField.setText(String.valueOf(record.getQuantity()));
remarkField.setText(record.getRemark());
}
} }

Loading…
Cancel
Save