入库功能

master
王城 18 hours ago
parent 0184185764
commit 3eceda3c3b

@ -6,6 +6,7 @@ import javafx.scene.Scene;
import javafx.scene.control.*; import javafx.scene.control.*;
import javafx.scene.layout.*; import javafx.scene.layout.*;
import javafx.stage.Stage; import javafx.stage.Stage;
import view.InboundView;
public class Main extends Application { public class Main extends Application {
@ -129,6 +130,9 @@ public class Main extends Application {
MenuItem logItem = new MenuItem("操作日志"); MenuItem logItem = new MenuItem("操作日志");
reportMenu.getItems().addAll(inventoryReportItem, logItem); reportMenu.getItems().addAll(inventoryReportItem, logItem);
// 为入库管理菜单项添加事件处理
inboundItem.setOnAction(e -> showInboundView());
menuBar.getMenus().addAll(systemMenu, inventoryMenu, goodsMenu, reportMenu); menuBar.getMenus().addAll(systemMenu, inventoryMenu, goodsMenu, reportMenu);
return menuBar; return menuBar;
} }
@ -162,9 +166,23 @@ public class Main extends Application {
root.getChildren().addAll(inventory, goods, report); root.getChildren().addAll(inventory, goods, report);
TreeView<String> tree = new TreeView<>(root); TreeView<String> tree = new TreeView<>(root);
// 为导航树添加事件处理
tree.setOnMouseClicked(event -> {
TreeItem<String> item = tree.getSelectionModel().getSelectedItem();
if (item != null && item.getValue().equals("入库管理")) {
showInboundView();
}
});
return tree; return tree;
} }
private void showInboundView() {
InboundView inboundView = new InboundView();
contentArea.setCenter(inboundView);
}
public static void main(String[] args) { public static void main(String[] args) {
launch(args); launch(args);
} }

@ -0,0 +1,97 @@
package model;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DataManager {
private static DataManager instance;
private Map<String, Goods> goodsMap;
private List<String> suppliers;
private Map<String, Integer> inventory;
private DataManager() {
goodsMap = new HashMap<>();
suppliers = new ArrayList<>();
inventory = new HashMap<>();
initializeData();
}
public static DataManager getInstance() {
if (instance == null) {
instance = new DataManager();
}
return instance;
}
private void initializeData() {
// 电子产品类
addGoodsWithStock("G001", "苹果手机", "iPhone 15", "台", 6999.00, "电子产品", 50);
addGoodsWithStock("G002", "联想笔记本", "ThinkPad T14", "台", 5999.00, "电子产品", 30);
addGoodsWithStock("G003", "华为平板", "MatePad Pro", "台", 3999.00, "电子产品", 40);
addGoodsWithStock("G004", "戴尔显示器", "27寸 4K", "台", 2399.00, "电子产品", 25);
// 办公家具类
addGoodsWithStock("G005", "办公桌", "1.4米", "张", 799.00, "办公家具", 100);
addGoodsWithStock("G006", "办公椅", "人体工学", "把", 599.00, "办公家具", 150);
addGoodsWithStock("G007", "文件柜", "三层", "个", 459.00, "办公家具", 80);
addGoodsWithStock("G008", "会议桌", "2.4米", "张", 1599.00, "办公家具", 20);
// 办公用品类
addGoodsWithStock("G009", "打印纸", "A4 70g", "箱", 89.00, "办公用品", 200);
addGoodsWithStock("G010", "签字笔", "0.5mm黑色", "盒", 15.00, "办公用品", 500);
addGoodsWithStock("G011", "订书机", "标准型", "个", 35.00, "办公用品", 300);
addGoodsWithStock("G012", "文件夹", "A4蓝色", "个", 8.00, "办公用品", 1000);
// 添加供应商
suppliers.add("苹果官方旗舰店");
suppliers.add("联想专卖店");
suppliers.add("华为授权店");
suppliers.add("戴尔官方店");
suppliers.add("京东自营");
suppliers.add("天猫超市");
suppliers.add("办公伙伴专营店");
suppliers.add("文具批发商");
suppliers.add("家具城");
suppliers.add("办公用品直营店");
}
private void addGoodsWithStock(String id, String name, String specification,
String unit, double price, String category, int initialStock) {
Goods goods = new Goods(id, name, specification, unit, price, category);
goodsMap.put(id, goods);
inventory.put(id, initialStock);
}
public void addGoods(Goods goods) {
goodsMap.put(goods.getId(), goods);
if (!inventory.containsKey(goods.getId())) {
inventory.put(goods.getId(), 0);
}
}
public Goods getGoods(String id) {
return goodsMap.get(id);
}
public List<Goods> getAllGoods() {
return new ArrayList<>(goodsMap.values());
}
public List<String> getSuppliers() {
return new ArrayList<>(suppliers);
}
public int getStock(String goodsId) {
return inventory.getOrDefault(goodsId, 0);
}
public void updateStock(String goodsId, int quantity) {
inventory.put(goodsId, inventory.getOrDefault(goodsId, 0) + quantity);
}
public Map<String, Integer> getAllStock() {
return new HashMap<>(inventory);
}
}

@ -0,0 +1,33 @@
package model;
public class Goods {
private String id;
private String name;
private String specification;
private String unit;
private double price;
private String category;
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;
}
// 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; }
}

@ -0,0 +1,40 @@
package model;
import java.time.LocalDateTime;
public class InboundRecord {
private String id;
private String goodsId;
private int quantity;
private String supplier;
private LocalDateTime inboundTime;
private String operator;
private String remark;
public InboundRecord(String id, String goodsId, int quantity, String supplier,
LocalDateTime inboundTime, String operator, String remark) {
this.id = id;
this.goodsId = goodsId;
this.quantity = quantity;
this.supplier = supplier;
this.inboundTime = inboundTime;
this.operator = operator;
this.remark = remark;
}
// Getters and Setters
public String getId() { return id; }
public void setId(String id) { this.id = id; }
public String getGoodsId() { return goodsId; }
public void setGoodsId(String goodsId) { this.goodsId = goodsId; }
public int getQuantity() { return quantity; }
public void setQuantity(int quantity) { this.quantity = quantity; }
public String getSupplier() { return supplier; }
public void setSupplier(String supplier) { this.supplier = supplier; }
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 getRemark() { return remark; }
public void setRemark(String remark) { this.remark = remark; }
}

@ -0,0 +1,224 @@
package view;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.control.cell.PropertyValueFactory;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import model.InboundRecord;
import model.DataManager;
import model.Goods;
import javafx.collections.FXCollections;
import java.util.List;
import java.util.stream.Collectors;
public class InboundView extends BorderPane {
private TextField goodsField;
private TextField supplierField;
private TextField quantityField;
private TextField remarkField;
private TableView<InboundRecord> tableView;
private Label totalValue;
public InboundView() {
initializeUI();
}
private void initializeUI() {
// 顶部操作区域
VBox topArea = createTopArea();
setTop(topArea);
// 中间表格区域
tableView = createTableView();
setCenter(tableView);
// 底部汇总区域
HBox bottomArea = createBottomArea();
setBottom(bottomArea);
}
private VBox createTopArea() {
VBox topArea = new VBox(10);
topArea.setPadding(new Insets(10));
// 第一行:基本信息
GridPane basicInfo = new GridPane();
basicInfo.setHgap(10);
basicInfo.setVgap(10);
Label goodsLabel = new Label("货物名称:");
goodsField = new TextField();
goodsField.setPromptText("请输入货物名称");
Label supplierLabel = new Label("供应商:");
supplierField = new TextField();
supplierField.setPromptText("请输入供应商");
basicInfo.addRow(0, goodsLabel, goodsField, supplierLabel, supplierField);
// 第二行:数量和备注
Label quantityLabel = new Label("数量:");
quantityField = new TextField();
quantityField.setPromptText("请输入数量");
Label remarkLabel = new Label("备注:");
remarkField = new TextField();
remarkField.setPromptText("请输入备注信息");
basicInfo.addRow(1, quantityLabel, quantityField, remarkLabel, remarkField);
// 设置列宽
ColumnConstraints column1 = new ColumnConstraints();
column1.setMinWidth(80);
ColumnConstraints column2 = new ColumnConstraints();
column2.setMinWidth(200);
ColumnConstraints column3 = new ColumnConstraints();
column3.setMinWidth(80);
ColumnConstraints column4 = new ColumnConstraints();
column4.setMinWidth(200);
basicInfo.getColumnConstraints().addAll(column1, column2, column3, column4);
// 操作按钮
HBox buttonBox = new HBox(10);
buttonBox.setAlignment(Pos.CENTER_RIGHT);
buttonBox.setPadding(new Insets(10, 0, 0, 0));
Button saveButton = new Button("保存");
saveButton.setOnAction(e -> handleSave());
Button clearButton = new Button("清空");
clearButton.setOnAction(e -> handleClear());
buttonBox.getChildren().addAll(saveButton, clearButton);
topArea.getChildren().addAll(basicInfo, buttonBox);
return topArea;
}
private TableView<InboundRecord> createTableView() {
TableView<InboundRecord> table = new TableView<>();
TableColumn<InboundRecord, String> idCol = new TableColumn<>("入库单号");
idCol.setCellValueFactory(new PropertyValueFactory<>("id"));
TableColumn<InboundRecord, String> goodsCol = new TableColumn<>("货物名称");
goodsCol.setCellValueFactory(new PropertyValueFactory<>("goodsId"));
TableColumn<InboundRecord, Integer> quantityCol = new TableColumn<>("数量");
quantityCol.setCellValueFactory(new PropertyValueFactory<>("quantity"));
TableColumn<InboundRecord, String> supplierCol = new TableColumn<>("供应商");
supplierCol.setCellValueFactory(new PropertyValueFactory<>("supplier"));
TableColumn<InboundRecord, LocalDateTime> timeCol = new TableColumn<>("入库时间");
timeCol.setCellValueFactory(new PropertyValueFactory<>("inboundTime"));
TableColumn<InboundRecord, String> operatorCol = new TableColumn<>("操作员");
operatorCol.setCellValueFactory(new PropertyValueFactory<>("operator"));
TableColumn<InboundRecord, String> remarkCol = new TableColumn<>("备注");
remarkCol.setCellValueFactory(new PropertyValueFactory<>("remark"));
table.getColumns().addAll(idCol, goodsCol, quantityCol, supplierCol,
timeCol, operatorCol, remarkCol);
table.getItems().addListener((javafx.collections.ListChangeListener.Change<? extends InboundRecord> 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(InboundRecord::getQuantity)
.sum();
totalValue.setText(String.valueOf(total));
}
private void handleSave() {
try {
String goodsName = goodsField.getText().trim();
String supplier = supplierField.getText().trim();
String quantityText = quantityField.getText().trim();
String remark = remarkField.getText().trim();
// 输入验证
if (goodsName.isEmpty() || supplier.isEmpty() || quantityText.isEmpty()) {
showAlert("错误", "请填写完整的入库信息!");
return;
}
int quantity;
try {
quantity = Integer.parseInt(quantityText);
if (quantity <= 0) {
showAlert("错误", "数量必须大于0");
return;
}
} catch (NumberFormatException e) {
showAlert("错误", "请输入有效的数量!");
return;
}
// 创建入库记录
String id = "IN" + System.currentTimeMillis();
InboundRecord record = new InboundRecord(
id,
goodsName,
quantity,
supplier,
LocalDateTime.now(),
"当前用户",
remark
);
// 添加到表格
tableView.getItems().add(record);
// 更新总计
updateTotal();
// 清空输入
handleClear();
showAlert("成功", "入库记录已保存!");
} catch (Exception e) {
showAlert("错误", "保存失败:" + e.getMessage());
}
}
private void handleClear() {
goodsField.clear();
supplierField.clear();
quantityField.clear();
remarkField.clear();
}
private void showAlert(String title, String content) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle(title);
alert.setHeaderText(null);
alert.setContentText(content);
alert.showAndWait();
}
}
Loading…
Cancel
Save