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.
test/branch_zyx/My_branch/ProductionDialog.java

321 lines
16 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.lingnan.supermarket.dialog; // 声明当前类所在的包
import java.awt.Container; // 导入AWT容器类
import java.awt.FlowLayout; // 导入流式布局类
import java.awt.event.ActionEvent; // 导入动作事件类
import java.awt.event.ActionListener; // 导入动作监听器接口
import java.awt.event.ItemEvent; // 导入项目事件类
import java.awt.event.ItemListener; // 导入项目监听器接口
import java.util.ArrayList; // 导入ArrayList类
import javax.swing.JButton; // 导入Swing的按钮组件
import javax.swing.JComboBox; // 导入Swing的下拉框组件
import javax.swing.JDialog; // 导入Swing的对话框组件
import javax.swing.JFrame; // 导入Swing的窗口框架组件
import javax.swing.JLabel; // 导入Swing的标签组件
import javax.swing.JOptionPane; // 导入Swing的对话框工具类
import javax.swing.JPanel; // 导入Swing的面板组件
import javax.swing.JTextField; // 导入Swing的文本框组件
import com.lingnan.supermarket.*; // 导入supermarket包下的所有类
import com.lingnan.supermarket.dao.SupplierInfService; // 导入供应商信息服务接口
import com.lingnan.supermarket.dao.productionService; // 导入产品服务接口
import com.lingnan.supermarket.dao.impl.SupplierInfImpl; // 导入供应商信息服务的实现类
import com.lingnan.supermarket.dao.impl.prodCatalogImpl; // 导入产品目录服务的实现类
import com.lingnan.supermarket.dao.impl.productionImpl; // 导入产品服务的实现类
import com.lingnan.supermarket.dto.ProdCatalog; // 导入产品目录数据传输对象
import com.lingnan.supermarket.dto.Production; // 导入产品数据传输对象
import com.lingnan.supermarket.dto.SupplierInf; // 导入供应商信息数据传输对象
import com.lingnan.supermarket.table.ProdCatalogTM; // 导入产品目录表格模型
import com.lingnan.supermarket.view.StorageView; // 导入库存视图
import com.lingnan.supermarket.view.SupplierView; // 导入供应商视图
import com.lingnan.supermarket.view.ProdCatalogView.MyItemListener; // 导入产品目录视图的内部类MyItemListener
public class ProductionDialog extends JDialog implements ActionListener { // 定义一个对话框类ProductionDialog继承自JDialog并实现ActionListener接口
private JPanel namePanel, addressPanel, contactPanel, // 声明多个面板变量
opePanel,idPanel,inpricePanel,outpricePanel,lifePanel, // 用于不同信息的布局
sumPanel,supplyidPanel,id2Panel,name2Panel; //
private JLabel nameLabel, addressLabel, contactLabel, // 声明多个标签变量
idLabel,inpriceLabel,outpriceLabel,lifeLabel,sumLabel, // 用于显示不同信息的标签
supplyidLabel,id2Label,name2Label; //
private JTextField nameTF, addressTF, contactTF, // 声明多个文本框变量
idTF,inpriceTF,outpriceTF,lifeTF,sumTF, // 用于输入不同信息
supplyidTF,id2TF,name2TF; //
private JButton saveBtn, cancelBtn; // 声明保存和取消按钮变量
private productionService productionService = new productionImpl(); // 实例化产品服务
// 下拉类别相关变量
private String log[]=null; // 用于存储类别信息的数组
private ArrayList<String>alog=null; // 用于存储类别信息的列表
private ProdCatalogTM prodCatalogTM; // 产品目录表格模型
private ProdCatalog pc; // 产品目录数据传输对象
private prodCatalogImpl pci; // 产品目录服务实现类
private JComboBox<String> combo; // 下拉框组件
private String id2; // 产品目录ID
private String name2; // 产品目录名称
// 下拉供应商类别相关变量
private String superlier[]=null; // 用于存储供应商信息的数组
private ArrayList<String>asuperlier=null; // 用于存储供应商信息的列表
private SupplierInf si; // 供应商信息数据传输对象
private SupplierInfImpl sii; // 供应商信息服务实现类
private JComboBox<String> combo1; // 供应商下拉框组件
private int supplyid; // 供应商ID
private StorageView storageView; // 库存视图引用
private Production production; // 产品数据传输对象
public ProductionDialog(JFrame parent) { // 构造函数,接收一个父窗口作为参数
super(parent, "添加"); // 调用父类构造函数,设置对话框标题为"添加"
setSize(350, 500); // 设置对话框大小
setLocationRelativeTo(null); // 设置对话框相对于屏幕居中
setModal(true); // 设置对话框为模态
setResizable(false); // 设置对话框不可调整大小
this.setLayout(new FlowLayout()); // 设置对话框布局为流式布局
initView(); // 初始化视图
}
private void initView() { // 初始化视图的方法
idPanel = new JPanel(); // 创建一个面板用于显示商品编号
idLabel = new JLabel("商品编号"); // 创建一个标签显示"商品编号"
idTF = new JTextField(15); // 创建一个文本框用于输入商品编号宽度为15
idPanel.add(idLabel); // 将标签添加到面板
idPanel.add(idTF); // 将文本框添加到面板
namePanel = new JPanel(); // 创建一个面板用于显示名称
nameLabel = new JLabel("名称"); // 创建一个标签显示"名称"
nameTF = new JTextField(15); // 创建一个文本框用于输入名称宽度为15
namePanel.add(nameLabel); // 将标签添加到面板
namePanel.add(nameTF); // 将文本框添加到面板
inpricePanel = new JPanel(); // 创建一个面板用于显示进货单价
inpriceLabel = new JLabel("进货单价"); // 创建一个标签显示"进货单价"
inpriceTF = new JTextField(15); // 创建一个文本框用于输入进货单价宽度为15
inpricePanel.add(inpriceLabel); // 将标签添加到面板
inpricePanel.add(inpriceTF); // 将文本框添加到面板
outpricePanel = new JPanel(); // 创建一个面板用于显示购买单价
outpriceLabel = new JLabel("购买单价"); // 创建一个标签显示"购买单价"
outpriceTF = new JTextField(15); // 创建一个文本框用于输入购买单价宽度为15
outpricePanel.add(outpriceLabel); // 将标签添加到面板
outpricePanel.add(outpriceTF); // 将文本框添加到面板
lifePanel = new JPanel(); // 创建一个面板用于显示保质期
lifeLabel = new JLabel("保质期(月份数)"); // 创建一个标签显示"保质期(月份数)"
lifeTF = new JTextField(15); // 创建一个文本框用于输入保质期宽度为15
lifePanel.add(lifeLabel); // 将标签添加到面板
lifePanel.add(lifeTF); // 将文本框添加到面板
sumPanel = new JPanel(); // 创建一个面板用于显示库存
sumLabel = new JLabel("库存"); // 创建一个标签显示"库存"
sumTF = new JTextField(15); // 创建一个文本框用于输入库存数量宽度为15
sumPanel.add(sumLabel); // 将标签添加到面板
sumPanel.add(sumTF); // 将文本框添加到面板
//供应商名下拉框 传递supplyid
supplyidPanel = new JPanel(); // 创建一个面板用于显示供应商信息
supplyidLabel = new JLabel("供应商"); // 创建一个标签显示"供应商"
//supplyidTF = new JTextField(15); // 注释掉的代码原本可能是用于输入供应商ID的文本框
sii=new SupplierInfImpl(); // 创建供应商信息服务的实例
this.asuperlier=sii.findNameSupplier(); // 获取所有供应商名称的列表
this.superlier=new String[asuperlier.size()]; // 创建一个字符串数组用于存储供应商名称
for(int i=0;i<asuperlier.size();i++)
superlier[i]=asuperlier.get(i); // 将供应商名称填充到字符串数组中
for(int i=0;i<superlier.length;i++)
{
System.out.println(superlier[i]); // 打印供应商名称,用于调试
}
combo1 = new JComboBox<String>(superlier); // 创建一个下拉框组件,用于选择供应商
combo1.addItemListener(new MyItemListener1()); // 为下拉框添加项目监听器
supplyidPanel.add(supplyidLabel); // 将标签添加到面板
supplyidPanel.add(combo1); // 将下拉框添加到面板
/* id2Panel = new JPanel();
id2Label = new JLabel("分类id");
id2TF = new JTextField(id2,15);
id2Panel.add(id2Label);
id2Panel.add(id2TF);*/
//类名下拉框
name2Panel = new JPanel(); // 创建一个面板用于显示类名
name2Label = new JLabel("类名"); // 创建一个标签显示"类名"
pci=new prodCatalogImpl(); // 创建产品目录服务的实例
this.alog=pci.findNameProdCatalog(); // 获取所有产品目录名称的列表
this.log=new String[alog.size()]; // 创建一个字符串数组用于存储产品目录名称
for(int i=0;i<alog.size();i++)
log[i]=alog.get(i); // 将产品目录名称填充到字符串数组中
combo = new JComboBox<String>(log); // 创建一个下拉框组件,用于选择产品目录
combo.addItemListener(new MyItemListener()); // 为下拉框添加项目监听器
name2Panel.add(name2Label); // 将标签添加到面板
name2Panel.add(combo); // 将下拉框添加到面板
addressPanel = new JPanel(); // 创建一个面板用于显示地址
addressLabel = new JLabel("地址"); // 创建一个标签显示"地址"
addressTF = new JTextField(15); // 创建一个文本框用于输入地址宽度为15
addressPanel.add(addressLabel); // 将标签添加到面板
addressPanel.add(addressTF); // 将文本框添加到面板
contactPanel = new JPanel(); // 创建一个面板用于显示联系方式
contactLabel = new JLabel("电话"); // 创建一个标签显示"电话"
contactTF = new JTextField(15); // 创建一个文本框用于输入电话宽度为15
contactPanel.add(contactLabel); // 将标签添加到面板
contactPanel.add(contactTF); // 将文本框添加到面板
opePanel = new JPanel(); // 创建一个面板用于显示操作按钮
saveBtn = new JButton("保存"); // 创建一个保存按钮
cancelBtn = new JButton("取消"); // 创建一个取消按钮
saveBtn.addActionListener(this); // 为保存按钮添加动作监听器
cancelBtn.addActionListener(this); // 为取消按钮添加动作监听器
opePanel.add(saveBtn); // 将保存按钮添加到面板
opePanel.add(cancelBtn); // 将取消按钮添加到面板
Container container = getContentPane(); // 获取对话框的内容面板
container.add(idPanel); // 将商品编号面板添加到内容面板
container.add(namePanel); // 将名称面板添加到内容面板
container.add(inpricePanel); // 将进货单价面板添加到内容面板
container.add(outpricePanel); // 将购买单价面板添加到内容面板
container.add(lifePanel); // 将保质期面板添加到内容面板
container.add(sumPanel); // 将库存面板添加到内容面板
container.add(supplyidPanel); // 将供应商面板添加到内容面板
//container.add(id2Panel); // 注释掉的代码原本可能是用于添加分类ID面板
container.add(name2Panel); // 将类名面板添加到内容面板
container.add(opePanel); // 将操作按钮面板添加到内容面板
}
@Override
public void actionPerformed(ActionEvent e) { // 实现ActionListener接口的actionPerformed方法
Object source = e.getSource(); // 获取事件源
if (source == saveBtn) { // 如果事件源是保存按钮
// 思路获取数据
// 保存到数据库
// 关闭对话框
// 刷新table
String name = nameTF.getText(); // 获取名称文本框的内容
String id = idTF.getText(); // 获取商品编号文本框的内容
float inprice = Float.parseFloat((inpriceTF.getText())); // 将进货单价文本框的内容转换为浮点数
float outprice = Float.parseFloat(outpriceTF.getText()); // 将销售单价文本框的内容转换为浮点数
int life = Integer.parseInt(lifeTF.getText()); // 将保质期文本框的内容转换为整数
int sum = Integer.parseInt(sumTF.getText()); // 将库存文本框的内容转换为整数
// TODO 参数校验
if (this.production == null) { // 如果production对象为空表示是添加新商品
if(supplyid==-1){ // 如果供应商ID为-1表示商品检索出错
JOptionPane.showMessageDialog(this, "商品检索出错", "提示",
JOptionPane.ERROR_MESSAGE); // 显示错误消息
return; // 退出方法
}
if(supplyid==0){ // 如果供应商ID为0表示未选择商品名
JOptionPane.showMessageDialog(this, "请选择商品名", "提示",
JOptionPane.ERROR_MESSAGE); // 显示错误消息
return; // 退出方法
}
if(id2.equals("0")){ // 如果分类ID为"0",表示未选择商品类
JOptionPane.showMessageDialog(this, "请选择商品类", "提示",
JOptionPane.ERROR_MESSAGE); // 显示错误消息
return; // 退出方法
}
Production production = new Production(); // 创建新的Production对象
production.setId(id); // 设置商品编号
production.setName(name); // 设置商品名称
production.setInPrice(inprice); // 设置进货单价
production.setOutPrice(outprice); // 设置销售单价
production.setLife(life); // 设置保质期
production.setSum(sum); // 设置库存
production.setSupplyId(supplyid); // 设置供应商ID
production.setId2(id2); // 设置分类ID
production.setName2(name2); // 设置分类名称
int result = productionService.addProduction(production); // 调用服务添加商品
// int result = 1; // 注释掉的代码,可能是用于测试
if (result == 1) { // 如果添加成功
JOptionPane.showMessageDialog(this, "添加成功", "提示",
JOptionPane.INFORMATION_MESSAGE); // 显示成功消息
this.dispose(); // 关闭对话框
} else if(result == 2){ // 如果返回值为2表示已存在该商品
JOptionPane.showMessageDialog(this, "已存在该商品", "提示",
JOptionPane.ERROR_MESSAGE); // 显示错误消息
}
else { // 如果添加失败
JOptionPane.showMessageDialog(this, "出错!添加失败", "提示",
JOptionPane.ERROR_MESSAGE); // 显示错误消息
}
}/*else{
//更新
SupplierInf supplierInf= new SupplierInf();
supplierInf.setName(name);
supplierInf.setAddress(address);
supplierInf.setContact(contact);
supplierInf.setId(this.supplierInf.getId());
int result = supplierInfService.updateSupplierInf(supplierInf);
if(result==1){
JOptionPane.showMessageDialog(this, "更新成功", "提示",
JOptionPane.INFORMATION_MESSAGE);
}
}*/
} else if (source == cancelBtn) { // 如果事件源是取消按钮
this.dispose(); // 关闭对话框
}
}
// 定义一个内部类MyItemListener实现ItemListener接口
public class MyItemListener implements ItemListener {
@Override
public void itemStateChanged(ItemEvent e) { // 实现itemStateChanged方法
JComboBox cb = (JComboBox) e.getSource(); // 获取事件源,即下拉框组件
name2 = (String) cb.getSelectedItem(); // 获取选中的商品类名称
pci =new prodCatalogImpl(); // 创建商品分类服务实现类对象
for(int i=0;i<log.length;i++){ // 遍历商品分类数组
if(name2.equals(log[i])) // 如果找到匹配的商品类名称
id2=pci.findProdCatalogByname(name2); // 通过名称查找商品分类ID
}
}
}
// 定义另一个内部类MyItemListener1实现ItemListener接口
public class MyItemListener1 implements ItemListener {
@Override
public void itemStateChanged(ItemEvent e) { // 实现itemStateChanged方法
JComboBox cb = (JComboBox) e.getSource(); // 获取事件源,即下拉框组件
String suppliername = (String) cb.getSelectedItem(); // 获取选中的供应商名称
sii =new SupplierInfImpl(); // 创建供应商信息服务实现类对象
for(int i=0;i<superlier.length;i++){ // 遍历供应商数组
if(suppliername.equals(superlier[i])) // 如果找到匹配的供应商名称
supplyid=sii.findIdSupplierByName(suppliername); // 通过名称查找供应商ID
}
}
}
}