仓库管理部分已注释

pull/46/head
郑亦歆 9 months ago
parent 50f85cd555
commit 5639047a02

@ -1,5 +1,7 @@
// 定义包名表明该类属于com.lingnan.supermarket.dialog包
package com.lingnan.supermarket.dialog;
// 导入所需的java.awt、java.awt.event、javax.swing包中的类
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
@ -18,6 +20,7 @@ import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
// 导入com.lingnan.supermarket包及其子包中的类
import com.lingnan.supermarket.*;
import com.lingnan.supermarket.dao.UserService;
import com.lingnan.supermarket.dao.impl.BufferImpl;
@ -31,195 +34,377 @@ import com.lingnan.supermarket.view.InView;
import com.lingnan.supermarket.view.OutView;
import com.lingnan.supermarket.view.ProdCatalogView.MyItemListener;
// 定义InDialog类继承JDialog并实现ActionListener接口
public class InDialog extends JDialog implements ActionListener{
// 声明面板变量
private JPanel prodIdPanel,sumPanel,phonePanel,opePanel;
// 声明标签变量
private JLabel prodIdLabel,sumLabel;
// 声明文本框变量
private JTextField prodIdTF,sumTF;
// 声明按钮变量
private JButton addBtn,cancelBtn;
// 实例化OutTableModel对象
private OutTableModel outTableModel = new OutTableModel();
// 声明Production对象
private Production production;
// 声明productionImpl对象
private productionImpl productionImpl;
// 声明Vector<Production>对象
private Vector<Production> v;
// 声明User对象
private User user;
// 声明JFrame对象
private JFrame JFramparent;
// 声明JComboBox<String>对象
private JComboBox<String> combo;
// 声明字符串数组
private String allProdId[] = null;
// 声明Vector<Production>对象
private Vector<Production> vAll;
// 声明静态字符串变量
private static String catalog;
// 构造方法接收JFrame、Vector<Production>和User对象作为参数
public InDialog(JFrame parent,Vector<Production> v,User user) {
super(parent,"添加商品");
setSize(250,200);
setLocationRelativeTo(null);
// 设置对话框大小
setSize(250,200);
// 设置对话框相对于父窗口居中
setLocationRelativeTo(null);
// 设置对话框为模态
setModal(true);
setResizable(false);
// 设置对话框不可调整大小
setResizable(false);
// 设置对话框布局为FlowLayout
this.setLayout(new FlowLayout());
// 初始化父窗口引用
JFramparent=parent;
// 初始化Vector<Production>引用
this.v=v;
// 初始化User引用
this.user = user;
// 初始化视图
initView();
}
// 初始化视图的私有方法
private void initView() {
// 创建编号面板
prodIdPanel = new JPanel();
// 创建编号标签
prodIdLabel = new JLabel("编号");
// 实例化productionImpl对象
productionImpl= new productionImpl();
// 获取所有产品的列表
vAll=productionImpl.findAllproduction();
// 初始化字符串数组,长度与产品列表相同
allProdId = new String[vAll.size()];
// 遍历产品列表,将产品编号存入数组
for(int i=0;i<vAll.size();i++) {
allProdId[i]=vAll.elementAt(i).getId();
}
// 设置默认目录为第一个产品编号
catalog = allProdId[0];
// 打印第一个产品编号
System.out.println(allProdId[0]);
// 创建下拉列表,使用产品编号数组作为数据源
combo = new JComboBox<String>(allProdId);
// 为下拉列表添加ItemListener监听器
combo.addItemListener(new MyItemListener());
// 将编号标签和下拉列表添加到编号面板
prodIdPanel.add(prodIdLabel);
prodIdPanel.add(combo);
// 创建数量面板
sumPanel = new JPanel();
// 创建数量标签
sumLabel = new JLabel("数量");
// 创建数量文本框
sumTF = new JTextField(10);
// 将数量标签和文本框添加到数量面板
sumPanel.add(sumLabel);
sumPanel.add(sumTF);
// 创建操作按钮面板
opePanel = new JPanel();
// 创建添加按钮
addBtn = new JButton("添加");
// 创建取消按钮
cancelBtn = new JButton("取消");
// 为添加按钮添加ActionListener监听器
addBtn.addActionListener(this);
// 为取消按钮添加ActionListener监听器
cancelBtn.addActionListener(this);
// 将添加和取消按钮添加到操作按钮面板
opePanel.add(addBtn);
opePanel.add(cancelBtn);
// 获取内容面板
Container container = getContentPane();
// 将所有面板添加到内容面板
container.add(prodIdPanel);
container.add(sumPanel);
container.add(opePanel);
}
/*将数组传到inview的刷新方法里面再刷新*/
// 提供一个方法来获取Vector<Production>对象
public Vector<Production> getVector(){
// 返回v对象
return v;
}
//下拉框监听
// 实现ItemListener接口的静态内部类用于监听下拉列表的事件
static class MyItemListener implements ItemListener{
@Override
// 当下拉列表的选项状态改变时触发此方法
public void itemStateChanged(ItemEvent e) {
// 将事件源转换为JComboBox
JComboBox cb=(JComboBox)e.getSource();
// 获取选中的选项
String select=(String) cb.getSelectedItem();
// 更新目录为选中的选项
catalog=select;
}
}
@Override
// 实现ActionListener接口的actionPerformed方法用于处理按钮点击事件
public void actionPerformed(ActionEvent e) {
// 获取事件源对象
Object source = e.getSource();
// 如果事件源是添加按钮
if(source==addBtn){
//1.判断是否存在这个商品
//2.如果存在就获取这条商品记录为一个对象
//3.判断购物缓冲区是否有这个记录
//3.1如果有update数量和price
//3.2如果没有就insert这条记录把sum更新
//保存到数据库
//关闭对话框
//刷新table
// 1.判断是否存在这个商品
// 2.如果存在就获取这条商品记录为一个对象
// 3.判断购物缓冲区是否有这个记录
// 3.1如果有update数量和price
// 3.2如果没有就insert这条记录把sum更新
// 保存到数据库
// 关闭对话框
// 刷新table
// 获取当前选中的商品编号
String prodId =catalog;
// 打印商品编号
System.out.println("proId="+prodId);
// 打印当前目录(应该是商品编号)
System.out.println("vatalog="+catalog);
// 判断数量文本框是否为空
if(sumTF.getText().equals("")) {
// 显示错误提示信息
JOptionPane.showMessageDialog(this,"请输入完整","提示",JOptionPane.ERROR_MESSAGE);
return;
}
// 将文本框中的字符串转换为整数
int sum = Integer.parseInt(sumTF.getText()) ;
if(sum<0) {/*判断输入大于0*/
// 判断输入的数量是否大于0
if(sum<0) {
// 显示错误提示信息
JOptionPane.showMessageDialog(this,"请输入大于0的数量","提示",JOptionPane.ERROR_MESSAGE);
return;
}
//TODO 参数校验
/*/判断是已添加,未添加还是不存在*/
// TODO 参数校验
// 判断商品是已添加、未添加还是不存在
productionImpl productionImpl = new productionImpl();
production = new Production();
// 根据商品编号查找商品
production = productionImpl.findByIdProduction(prodId);
if(production!=null) {/*商品库有这个商品存在*/
// 如果商品库中有这个商品存在
if(production!=null) {
// 标记变量,用于判断是否在购物缓冲区找到相同商品
int mark = 0;
// 遍历购物缓冲区
for(Production p:v) {
if(p.getId().equals(prodId)){/*如果数组中存在相同商品就更新数量和价格*/
sum=p.getSum()+sum;/*数量*/
// 如果找到相同商品编号
if(p.getId().equals(prodId)){
// 更新数量
sum=p.getSum()+sum;
p.setSum(sum);
p.setPrice(sum*p.getInPrice());/*进货价格*/
// 更新价格
p.setPrice(sum*p.getInPrice());
// 设置标记为找到
mark = 1;
break;
}
}
if(mark==0) {/*插入新的*/
// 如果购物缓冲区中没有找到相同商品
if(mark==0) {
// 打印信息
System.out.println("缓存区不存在,插入新的数据");
production.setSum(sum);/*更新价格和数量后插入心的*/
// 更新商品的数量和价格
production.setSum(sum);
production.setPrice(sum*production.getInPrice());
// 将商品添加到购物缓冲区
v.add(production);
}
}
// 打印购物缓冲区的大小
System.out.println("插入后v的大小"+v.size());
// 关闭对话框
this.dispose();
// 显示添加成功的提示信息
JOptionPane.showMessageDialog(this,"添加成功","提示",JOptionPane.ERROR_MESSAGE);
}
else {/*商品库没有这个商品*/
JOptionPane.showMessageDialog(this,"商品不存在","提示",JOptionPane.ERROR_MESSAGE);
}
}
else if(source == cancelBtn) {
this.dispose();
}
}
// 如果商品库中没有这个商品
else {/*商品库没有这个商品*/
// 显示商品不存在的错误提示信息
JOptionPane.showMessageDialog(this,"商品不存在","提示",JOptionPane.ERROR_MESSAGE);
}
// 如果事件源是取消按钮
else if(source == cancelBtn) {
// 关闭当前对话框
this.dispose();
}
}
}

@ -1,5 +1,7 @@
// 定义包名表明该类属于com.lingnan.supermarket.dialog包
package com.lingnan.supermarket.dialog;
// 导入所需的java.awt和javax.swing包中的类
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
@ -17,6 +19,7 @@ import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
// 导入com.lingnan.supermarket包中的相关类
import com.lingnan.supermarket.*;
import com.lingnan.supermarket.dao.SupplierInfService;
import com.lingnan.supermarket.dao.productionService;
@ -31,24 +34,31 @@ import com.lingnan.supermarket.view.StorageView;
import com.lingnan.supermarket.view.SupplierView;
import com.lingnan.supermarket.view.ProdCatalogView.MyItemListener;
// 定义ProductionDialog类继承JDialog并实现ActionListener接口
public class ProductionDialog extends JDialog implements ActionListener {
// 声明多个JPanel对象用于布局
private JPanel namePanel, addressPanel, contactPanel,
opePanel,idPanel,inpricePanel,outpricePanel,lifePanel,
sumPanel,supplyidPanel,id2Panel,name2Panel;
opePanel,idPanel,inpricePanel,outpricePanel,lifePanel,
sumPanel,supplyidPanel,id2Panel,name2Panel;
// 声明多个JLabel对象用于显示文本
private JLabel nameLabel, addressLabel, contactLabel,
idLabel,inpriceLabel,outpriceLabel,lifeLabel,sumLabel,
supplyidLabel,id2Label,name2Label;
idLabel,inpriceLabel,outpriceLabel,lifeLabel,sumLabel,
supplyidLabel,id2Label,name2Label;
// 声明多个JTextField对象用于文本输入
private JTextField nameTF, addressTF, contactTF,
idTF,inpriceTF,outpriceTF,lifeTF,sumTF,
supplyidTF,id2TF,name2TF;
idTF,inpriceTF,outpriceTF,lifeTF,sumTF,
supplyidTF,id2TF,name2TF;
// 声明按钮对象,用于保存和取消操作
private JButton saveBtn, cancelBtn;
// 实例化productionService对象用于操作产品数据
private productionService productionService = new productionImpl();
//下拉类别
// 下拉类别相关变量
private String log[]=null;
private ArrayList<String>alog=null;
private ProdCatalogTM prodCatalogTM;
@ -57,8 +67,8 @@ public class ProductionDialog extends JDialog implements ActionListener {
private JComboBox<String> combo;
private String id2;
private String name2;
//下拉供应商类别
// 下拉供应商类别相关变量
private String superlier[]=null;
private ArrayList<String>asuperlier=null;
private SupplierInf si;
@ -66,135 +76,199 @@ public class ProductionDialog extends JDialog implements ActionListener {
private JComboBox<String> combo1;
private int supplyid;
// StorageView对象用于界面更新
private StorageView storageView;
// 声明Production对象用于存储产品信息
private Production production;
// 构造方法接收一个JFrame对象作为父窗口并设置对话框标题为"添加"
public ProductionDialog(JFrame parent) {
super(parent, "添加");
// 设置对话框的大小为350x500像素
setSize(350, 500);
// 设置对话框相对于屏幕居中
setLocationRelativeTo(null);
// 设置对话框为模态,即不关闭此对话框无法操作其他窗口
setModal(true);
// 设置对话框不可调整大小
setResizable(false);
// 设置对话框的布局管理器为FlowLayout
this.setLayout(new FlowLayout());
// 初始化视图
initView();
}
// 初始化视图的方法
private void initView() {
// 创建商品编号输入面板
idPanel = new JPanel();
// 创建商品编号标签
idLabel = new JLabel("商品编号");
// 创建商品编号文本框
idTF = new JTextField(15);
// 将标签和文本框添加到商品编号输入面板
idPanel.add(idLabel);
idPanel.add(idTF);
// 创建名称输入面板
namePanel = new JPanel();
// 创建名称标签
nameLabel = new JLabel("名称");
// 创建名称文本框
nameTF = new JTextField(15);
// 将标签和文本框添加到名称输入面板
namePanel.add(nameLabel);
namePanel.add(nameTF);
// 创建进货单价输入面板
inpricePanel = new JPanel();
// 创建进货单价标签
inpriceLabel = new JLabel("进货单价");
// 创建进货单价文本框
inpriceTF = new JTextField(15);
// 将标签和文本框添加到进货单价输入面板
inpricePanel.add(inpriceLabel);
inpricePanel.add(inpriceTF);
// 创建购买单价输入面板
outpricePanel = new JPanel();
// 创建购买单价标签
outpriceLabel = new JLabel("购买单价");
// 创建购买单价文本框
outpriceTF = new JTextField(15);
// 将标签和文本框添加到购买单价输入面板
outpricePanel.add(outpriceLabel);
outpricePanel.add(outpriceTF);
// 创建保质期输入面板
lifePanel = new JPanel();
// 创建保质期标签
lifeLabel = new JLabel("保质期(月份数)");
// 创建保质期文本框
lifeTF = new JTextField(15);
// 将标签和文本框添加到保质期输入面板
lifePanel.add(lifeLabel);
lifePanel.add(lifeTF);
// 创建库存输入面板
sumPanel = new JPanel();
// 创建库存标签
sumLabel = new JLabel("库存");
// 创建库存文本框
sumTF = new JTextField(15);
// 将标签和文本框添加到库存输入面板
sumPanel.add(sumLabel);
sumPanel.add(sumTF);
//供应商名下拉框 传递supplyid
// 创建供应商选择面板
supplyidPanel = new JPanel();
// 创建供应商标签
supplyidLabel = new JLabel("供应商");
// supplyidTF = new JTextField(15);
// 注释掉的代码,原本可能用于创建供应商文本框,但现在使用下拉框
// supplyidTF = new JTextField(15);
// 实例化SupplierInfImpl对象用于操作供应商信息
sii=new SupplierInfImpl();
// 调用findNameSupplier方法获取所有供应商的名称并存储到asuperlier列表中
this.asuperlier=sii.findNameSupplier();
// 根据asuperlier列表的大小创建superlier字符串数组
this.superlier=new String[asuperlier.size()];
// 将asuperlier列表中的元素复制到superlier数组中
for(int i=0;i<asuperlier.size();i++)
superlier[i]=asuperlier.get(i);
// 遍历superlier数组并打印每个供应商的名称
for(int i=0;i<superlier.length;i++)
{
System.out.println(superlier[i]);
System.out.println(superlier[i]);
}
// 创建一个新的JComboBox使用superlier数组作为下拉列表的数据源
combo1 = new JComboBox<String>(superlier);
// 为combo1添加一个ItemListener用于监听下拉列表选项的变化
combo1.addItemListener(new MyItemListener1());
// 将供应商标签和下拉列表添加到supplyidPanel面板中
supplyidPanel.add(supplyidLabel);
supplyidPanel.add(combo1);
/* id2Panel = new JPanel();
id2Label = new JLabel("分类id");
id2TF = new JTextField(id2,15);
id2Panel.add(id2Label);
id2Panel.add(id2TF);*/
//类名下拉框
// 注释掉的代码原本可能用于创建分类id的输入面板
/* id2Panel = new JPanel();
id2Label = new JLabel("分类id");
id2TF = new JTextField(id2,15);
id2Panel.add(id2Label);
id2Panel.add(id2TF);*/
// 创建类名输入面板
name2Panel = new JPanel();
// 创建类名标签
name2Label = new JLabel("类名");
// 实例化prodCatalogImpl对象用于操作产品目录信息
pci=new prodCatalogImpl();
// 调用findNameProdCatalog方法获取所有产品目录的名称并存储到alog列表中
this.alog=pci.findNameProdCatalog();
// 根据alog列表的大小创建log字符串数组
this.log=new String[alog.size()];
// 将alog列表中的元素复制到log数组中
for(int i=0;i<alog.size();i++)
log[i]=alog.get(i);
log[i]=alog.get(i);
// 创建一个新的JComboBox使用log数组作为下拉列表的数据源
combo = new JComboBox<String>(log);
// 为combo添加一个ItemListener用于监听下拉列表选项的变化
combo.addItemListener(new MyItemListener());
// 将类名标签和下拉列表添加到name2Panel面板中
name2Panel.add(name2Label);
name2Panel.add(combo);
// 创建地址输入面板
addressPanel = new JPanel();
// 创建地址标签
addressLabel = new JLabel("地址");
// 创建地址文本框
addressTF = new JTextField(15);
// 将标签和文本框添加到地址输入面板
addressPanel.add(addressLabel);
addressPanel.add(addressTF);
// 创建电话输入面板
contactPanel = new JPanel();
// 创建电话标签
contactLabel = new JLabel("电话");
// 创建电话文本框
contactTF = new JTextField(15);
// 将标签和文本框添加到电话输入面板
contactPanel.add(contactLabel);
contactPanel.add(contactTF);
// 创建操作按钮面板
opePanel = new JPanel();
// 创建保存按钮
saveBtn = new JButton("保存");
// 创建取消按钮
cancelBtn = new JButton("取消");
// 为保存按钮添加ActionListener监听按钮点击事件
saveBtn.addActionListener(this);
// 为取消按钮添加ActionListener监听按钮点击事件
cancelBtn.addActionListener(this);
// 将保存和取消按钮添加到操作按钮面板
opePanel.add(saveBtn);
opePanel.add(cancelBtn);
// 获取内容面板
Container container = getContentPane();
// 将所有面板添加到内容面板中
container.add(idPanel);
container.add(namePanel);
container.add(inpricePanel);
@ -202,72 +276,97 @@ public class ProductionDialog extends JDialog implements ActionListener {
container.add(lifePanel);
container.add(sumPanel);
container.add(supplyidPanel);
// container.add(id2Panel);
// 注释掉的代码,可能原本用于添加另一个面板
//container.add(id2Panel);
container.add(name2Panel);
container.add(opePanel);
}
@Override
public void actionPerformed(ActionEvent e) {
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) {
if(supplyid==-1){
JOptionPane.showMessageDialog(this, "商品检索出错", "提示",
JOptionPane.ERROR_MESSAGE);
return;
}
if(supplyid==0){
JOptionPane.showMessageDialog(this, "请选择商品名", "提示",
JOptionPane.ERROR_MESSAGE);
return;
}
if(id2.equals("0")){
JOptionPane.showMessageDialog(this, "请选择商品类", "提示",
JOptionPane.ERROR_MESSAGE);
return;
}
Production production = new Production();
production.setId(id);
production.setName(name);
production.setInPrice(inprice);
production.setOutPrice(outprice);
production.setLife(life);
production.setSum(sum);
production.setSupplyId(supplyid);
production.setId2(id2);
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){
JOptionPane.showMessageDialog(this, "已存在该商品", "提示",
JOptionPane.ERROR_MESSAGE);
}
else {
JOptionPane.showMessageDialog(this, "出错!添加失败", "提示",
JOptionPane.ERROR_MESSAGE);
}
// 重写actionPerformed方法以处理按钮点击事件
@Override
public void actionPerformed(ActionEvent e) {
// 获取事件源
Object source = e.getSource();
// 如果事件源是保存按钮
if (source == saveBtn) {
// 获取文本框中的数据
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 参数校验
// 如果production对象为null表示是添加新商品
if (this.production == null) {
// 如果supplyid为-1表示商品检索出错
if(supplyid==-1){
JOptionPane.showMessageDialog(this, "商品检索出错", "提示",
JOptionPane.ERROR_MESSAGE);
return; // 退出方法
}
// 如果supplyid为0表示用户没有选择商品名
if(supplyid==0){
JOptionPane.showMessageDialog(this, "请选择商品名", "提示",
JOptionPane.ERROR_MESSAGE);
return; // 退出方法
}
// 如果id2为"0",表示用户没有选择商品类
if(id2.equals("0")){
JOptionPane.showMessageDialog(this, "请选择商品类", "提示",
JOptionPane.ERROR_MESSAGE);
return; // 退出方法
}
// 创建一个新的Production对象
Production production = new Production();
// 设置商品ID
production.setId(id);
// 设置商品名称
production.setName(name);
// 设置商品进价
production.setInPrice(inprice);
// 设置商品售价
production.setOutPrice(outprice);
// 设置商品保质期
production.setLife(life);
// 设置商品库存
production.setSum(sum);
// 设置供应商ID
production.setSupplyId(supplyid);
// 设置商品类别ID
production.setId2(id2);
// 设置商品类别名称
production.setName2(name2);
// 调用productionService的addProduction方法添加商品并获取返回结果
int result = productionService.addProduction(production);
// int result = 1; // 注释掉的代码,可能是用于测试的硬编码结果
// 如果添加成功
if (result == 1) {
// 显示添加成功的消息框
JOptionPane.showMessageDialog(this, "添加成功", "提示",
JOptionPane.INFORMATION_MESSAGE);
// 关闭当前对话框
this.dispose();
}
// 如果返回结果为2表示已存在该商品
else if(result == 2){
JOptionPane.showMessageDialog(this, "已存在该商品", "提示",
JOptionPane.ERROR_MESSAGE);
}
// 如果添加失败
else {
// 显示添加失败的消息框
JOptionPane.showMessageDialog(this, "出错!添加失败", "提示",
JOptionPane.ERROR_MESSAGE);
}
}/*else{
//更新
SupplierInf supplierInf= new SupplierInf();
@ -283,39 +382,99 @@ public class ProductionDialog extends JDialog implements ActionListener {
}
}*/
} else if (source == cancelBtn) {
// 如果事件源是取消按钮
else if (source == cancelBtn) {
// 关闭当前对话框
this.dispose();
}
this.dispose();
}
}
public class MyItemListener implements ItemListener {
// 定义一个内部类MyItemListener实现ItemListener接口用于监听下拉列表选项的变化
public class MyItemListener implements ItemListener {
@Override
public void itemStateChanged(ItemEvent e) {
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);
}
}
// 实现接口的itemStateChanged方法
@Override
public void itemStateChanged(ItemEvent e) {
// 将事件源转换为JComboBox
JComboBox cb = (JComboBox) e.getSource();
// 获取选中的下拉列表项,并转换为字符串
name2 = (String) cb.getSelectedItem();
// 创建prodCatalogImpl对象用于查询商品类别信息
pci =new prodCatalogImpl();
// 遍历log数组寻找与选中项匹配的商品类别名称
for(int i=0;i<log.length;i++){
// 如果找到匹配的商品类别名称
if(name2.equals(log[i]))
// 调用findProdCatalogByname方法获取商品类别ID
id2=pci.findProdCatalogByname(name2);
}
}
}
}
public class MyItemListener1 implements ItemListener {
@Override
public void itemStateChanged(ItemEvent e) {
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);
}
// 定义另一个内部类MyItemListener1实现ItemListener接口用于监听供应商下拉列表选项的变化
public class MyItemListener1 implements ItemListener {
// 实现接口的itemStateChanged方法
@Override
public void itemStateChanged(ItemEvent e) {
// 将事件源转换为JComboBox
JComboBox cb = (JComboBox) e.getSource();
// 获取选中的下拉列表项,并转换为字符串
String suppliername = (String) cb.getSelectedItem();
// 创建SupplierInfImpl对象用于查询供应商信息
sii =new SupplierInfImpl();
// 遍历superlier数组寻找与选中项匹配的供应商名称
for(int i=0;i<superlier.length;i++){
// 如果找到匹配的供应商名称
if(suppliername.equals(superlier[i]))
// 调用findIdSupplierByName方法获取供应商ID
supplyid=sii.findIdSupplierByName(suppliername);
}
}
}
}

@ -1,89 +1,144 @@
// 定义包名表明该类属于com.lingnan.supermarket.dialog包
package com.lingnan.supermarket.dialog;
// 导入java.awt.Container类用于布局管理
import java.awt.Container;
// 导入java.awt.FlowLayout类用于流式布局
import java.awt.FlowLayout;
// 导入java.awt.event.ActionEvent类用于处理动作事件
import java.awt.event.ActionEvent;
// 导入java.awt.event.ActionListener接口用于实现事件监听
import java.awt.event.ActionListener;
// 导入javax.swing.JButton类用于创建按钮
import javax.swing.JButton;
// 导入javax.swing.JDialog类用于创建对话框
import javax.swing.JDialog;
// 导入javax.swing.JFrame类用于创建主窗口框架
import javax.swing.JFrame;
import javax.swing.JLabel;
// 导入javax.swing.JLabel类用于创建标签
import javax.swing.JOptionPane;
// 导入javax.swing.JPanel类用于创建面板
import javax.swing.JPanel;
import javax.swing.JTextField;
// 导入javax.swing.JTextField类用于创建文本框
// 导入com.lingnan.supermarket包可能包含其他相关的类和接口
import com.lingnan.supermarket.*;
// 导入com.lingnan.supermarket.dao.SupplierInfService接口用于定义供应商信息操作服务
import com.lingnan.supermarket.dao.SupplierInfService;
// 导入com.lingnan.supermarket.dao.impl.SupplierInfImpl类用于实现SupplierInfService接口
import com.lingnan.supermarket.dao.impl.SupplierInfImpl;
// 导入com.lingnan.supermarket.dto.SupplierInf类用于表示供应商信息数据传输对象
import com.lingnan.supermarket.dto.SupplierInf;
import com.lingnan.supermarket.view.SupplierView;
// 导入com.lingnan.supermarket.view.SupplierView类用于供应商信息视图界面
// 定义SupplierInfDialog类该类继承自JDialog并实现ActionListener接口用于创建供应商信息对话框
public class SupplierInfDialog extends JDialog implements ActionListener {
private JPanel namePanel, addressPanel, contactPanel,emailPanel, opePanel;
// 声明面板变量,用于不同的输入区域
private JPanel namePanel, addressPanel, contactPanel, emailPanel, opePanel;
private JLabel nameLabel, addressLabel, contactLabel,emailLabel;
private JTextField nameTF, addressTF, contactTF,emailTF;
// 声明标签变量,用于显示文本信息
private JLabel nameLabel, addressLabel, contactLabel, emailLabel;
// 声明文本框变量,用于输入信息
private JTextField nameTF, addressTF, contactTF, emailTF;
// 声明按钮变量,用于执行保存和取消操作
private JButton saveBtn, cancelBtn;
// 声明SupplierInfService服务对象用于操作供应商信息
private SupplierInfService supplierInfService = new SupplierInfImpl();
// 声明SupplierView视图对象用于更新供应商信息视图
private SupplierView supplierView;
// 声明SupplierInf对象用于存储供应商信息
private SupplierInf supplierInf;
// 构造方法,接收父窗口和供应商视图对象作为参数
public SupplierInfDialog(JFrame parent, SupplierView supplierView) {
// 调用父类构造方法,设置对话框标题
super(parent, "添加");
// 将传入的供应商视图对象赋值给成员变量
this.supplierView = supplierView;
// 设置对话框大小
setSize(350, 300);
// 设置对话框相对于父窗口居中
setLocationRelativeTo(null);
// 设置对话框为模态,阻止其他窗口操作
setModal(true);
// 设置对话框不可调整大小
setResizable(false);
// 设置对话框的布局管理器为FlowLayout
this.setLayout(new FlowLayout());
// 初始化视图组件
initView();
}
// 初始化视图组件的方法
private void initView() {
// 创建名称输入面板
namePanel = new JPanel();
// 创建名称标签
nameLabel = new JLabel("名称");
// 创建名称文本框
nameTF = new JTextField(15);
// 将标签和文本框添加到名称输入面板
namePanel.add(nameLabel);
namePanel.add(nameTF);
// 创建地址输入面板
addressPanel = new JPanel();
// 创建地址标签
addressLabel = new JLabel("地址");
// 创建地址文本框
addressTF = new JTextField(15);
// 将标签和文本框添加到地址输入面板
addressPanel.add(addressLabel);
addressPanel.add(addressTF);
// 创建电话输入面板
contactPanel = new JPanel();
// 创建电话标签
contactLabel = new JLabel("电话");
// 创建电话文本框
contactTF = new JTextField(15);
// 将标签和文本框添加到电话输入面板
contactPanel.add(contactLabel);
contactPanel.add(contactTF);
// 创建邮箱输入面板
emailPanel = new JPanel();
// 创建邮箱标签
emailLabel = new JLabel("邮箱");
// 创建邮箱文本框
emailTF = new JTextField(15);
// 将标签和文本框添加到邮箱输入面板
emailPanel.add(emailLabel);
emailPanel.add(emailTF);
// 创建操作按钮面板
opePanel = new JPanel();
// 创建保存按钮
saveBtn = new JButton("保存");
// 创建取消按钮
cancelBtn = new JButton("取消");
// 为保存按钮添加事件监听器当前类实现了ActionListener接口
saveBtn.addActionListener(this);
// 为取消按钮添加事件监听器当前类实现了ActionListener接口
cancelBtn.addActionListener(this);
// 将保存和取消按钮添加到操作按钮面板
opePanel.add(saveBtn);
opePanel.add(cancelBtn);
// 获取对话框的内容面板
Container container = getContentPane();
// 将各个输入面板和操作按钮面板添加到内容面板
container.add(namePanel);
container.add(addressPanel);
container.add(contactPanel);
@ -91,56 +146,83 @@ public class SupplierInfDialog extends JDialog implements ActionListener {
container.add(opePanel);
}
// 实现ActionListener接口的actionPerformed方法用于处理按钮点击事件
@Override
public void actionPerformed(ActionEvent e) {
// 获取事件源,即被点击的按钮
Object source = e.getSource();
// 判断如果事件源是保存按钮
if (source == saveBtn) {
// 思路获取数据
// 保存到数据库
// 关闭对话框
// 刷新table
// 获取用户输入的数据
String name = nameTF.getText();
String address = addressTF.getText();
String contact = contactTF.getText();
String email = emailTF.getText();
// TODO 参数校验
// TODO 参数校验,此处应有对输入数据的校验逻辑
// 如果supplierInf为null则进行添加操作
if (this.supplierInf == null) {
SupplierInf supplierInf = new SupplierInf();
supplierInf.setName(name);
supplierInf.setAddress(address);
supplierInf.setContact(contact);
supplierInf.setEmail(email);
// 调用服务层方法添加供应商信息,并获取操作结果
int result = supplierInfService.addSupplierInf(supplierInf);
// int result = 1;
// 如果添加成功
if (result == 1) {
// 显示添加成功的信息对话框
JOptionPane.showMessageDialog(this, "添加成功", "提示",
JOptionPane.INFORMATION_MESSAGE);
// 关闭当前对话框
this.dispose();
} 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);
}
}*/
// 如果supplierInf不为null则进行更新操作(此部分代码被注释掉)
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();
}
}
}

Loading…
Cancel
Save