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/InDialog.java

231 lines
9.2 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.Vector; // 导入向量类,用于存储对象集合
import javax.swing.JButton; // 导入按钮组件类
import javax.swing.JComboBox; // 导入下拉框组件类
import javax.swing.JDialog; // 导入对话框组件类
import javax.swing.JFrame; // 导入顶层窗口类
import javax.swing.JLabel; // 导入标签组件类
import javax.swing.JOptionPane; // 导入对话框工具类
import javax.swing.JPanel; // 导入面板组件类
import javax.swing.JTable; // 导入表格组件类
import javax.swing.JTextField; // 导入文本框组件类
import com.lingnan.supermarket.*; // 导入超市项目相关的所有类
import com.lingnan.supermarket.dao.UserService; // 导入用户服务接口
import com.lingnan.supermarket.dao.impl.BufferImpl; // 导入缓冲区服务实现类
import com.lingnan.supermarket.dao.impl.UserServiceImpl; // 导入用户服务实现类
import com.lingnan.supermarket.dao.impl.productionImpl; // 导入产品服务实现类
import com.lingnan.supermarket.dto.Buffer; // 导入缓冲区数据传输对象
import com.lingnan.supermarket.dto.Production; // 导入产品数据传输对象
import com.lingnan.supermarket.dto.User; // 导入用户数据传输对象
import com.lingnan.supermarket.table.OutTableModel; // 导入出库表格模型类
import com.lingnan.supermarket.view.InView; // 导入进货视图类
import com.lingnan.supermarket.view.OutView; // 导出出库视图类
import com.lingnan.supermarket.view.ProdCatalogView.MyItemListener; // 导入产品目录视图的内部类MyItemListener
public class InDialog extends JDialog implements ActionListener{ // 定义一个对话框类InDialog继承自JDialog并实现ActionListener接口
private JPanel prodIdPanel,sumPanel,phonePanel,opePanel; // 声明面板变量,用于不同功能的面板
private JLabel prodIdLabel,sumLabel; // 声明标签变量,用于显示文本
private JTextField prodIdTF,sumTF; // 声明文本框变量,用于输入数据
private JButton addBtn,cancelBtn; // 声明按钮变量,用于添加和取消操作
private OutTableModel outTableModel = new OutTableModel(); // 创建出库表格模型实例
private Production production; // 声明产品数据传输对象
private productionImpl productionImpl; // 声明产品服务实现类
private Vector<Production> v; // 声明产品集合变量
private User user; // 声明用户数据传输对象
private JFrame JFramparent; // 声明顶层窗口变量,用于存储父窗口
private JComboBox<String> combo; // 声明下拉框组件变量
private String allProdId[] = null; // 声明字符串数组用于存储所有产品ID
private Vector<Production> vAll; // 声明产品集合变量,用于存储所有产品
private static String catalog; // 声明静态字符串变量,用于存储分类信息
public InDialog(JFrame parent,Vector<Production> v,User user) { // 构造方法,接收父窗口、产品集合和用户对象
super(parent,"添加商品"); // 调用父类构造方法,设置对话框标题
setSize(250,200); // 设置对话框大小
setLocationRelativeTo(null); // 设置对话框居中显示
setModal(true); // 设置对话框为模态
setResizable(false); // 设置对话框不可调整大小
this.setLayout(new FlowLayout()); // 设置对话框布局为流式布局
JFramparent=parent; // 初始化父窗口变量
this.v=v; // 初始化产品集合变量
this.user = user; // 初始化用户对象
initView(); // 初始化视图
}
private void initView() {
prodIdPanel = new JPanel(); // 创建一个面板用于显示产品编号
prodIdLabel = new JLabel("编号"); // 创建一个标签显示"编号"
productionImpl= new productionImpl(); // 实例化产品服务实现类
vAll=productionImpl.findAllproduction(); // 调用方法获取所有产品信息
allProdId = new String[vAll.size()]; // 初始化字符串数组用于存储所有产品ID
for(int i=0;i<vAll.size();i++) { // 遍历产品集合将ID存入数组
allProdId[i]=vAll.elementAt(i).getId();
}
catalog = allProdId[0]; // 设置默认分类为第一个产品ID
System.out.println(allProdId[0]); // 打印第一个产品ID
combo = new JComboBox<String>(allProdId); // 创建下拉框并使用产品ID数组初始化
combo.addItemListener(new MyItemListener()); // 为下拉框添加项目监听器
prodIdPanel.add(prodIdLabel); // 将标签添加到编号面板
prodIdPanel.add(combo); // 将下拉框添加到编号面板
sumPanel = new JPanel(); // 创建一个面板用于显示数量
sumLabel = new JLabel("数量"); // 创建一个标签显示"数量"
sumTF = new JTextField(10); // 创建一个文本框用于输入数量宽度为10
sumPanel.add(sumLabel); // 将标签添加到数量面板
sumPanel.add(sumTF); // 将文本框添加到数量面板
opePanel = new JPanel(); // 创建一个面板用于显示操作按钮
addBtn = new JButton("添加"); // 创建一个按钮显示"添加"
cancelBtn = new JButton("取消"); // 创建一个按钮显示"取消"
addBtn.addActionListener(this); // 为添加按钮添加动作监听器
cancelBtn.addActionListener(this); // 为取消按钮添加动作监听器
opePanel.add(addBtn); // 将添加按钮添加到操作面板
opePanel.add(cancelBtn); // 将取消按钮添加到操作面板
Container container = getContentPane(); // 获取内容面板
container.add(prodIdPanel); // 将编号面板添加到内容面板
container.add(sumPanel); // 将数量面板添加到内容面板
container.add(opePanel); // 将操作面板添加到内容面板
}
/*将数组传到inview的刷新方法里面再刷新*/
public Vector<Production> getVector(){ // 提供一个方法获取产品集合
return v;
}
//下拉框监听
static class MyItemListener implements ItemListener{ // 定义一个内部类实现ItemListener接口
@Override
public void itemStateChanged(ItemEvent e) { // 实现项目状态改变时的处理方法
JComboBox cb=(JComboBox)e.getSource(); // 获取事件源下拉框
String select=(String) cb.getSelectedItem(); // 获取选中项的值
catalog=select; // 更新分类信息为选中项的值
}
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource(); // 获取事件源
if(source==addBtn){ // 如果事件源是添加按钮
//1.判断是否存在这个商品
//2.如果存在就获取这条商品记录为一个对象
//3.判断购物缓冲区是否有这个记录
//3.1如果有update数量和price
//3.2如果没有就insert这条记录把sum更新
//保存到数据库
//关闭对话框
//刷新table
String prodId =catalog; // 获取选中的商品ID
System.out.println("proId="+prodId); // 打印商品ID
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*/
JOptionPane.showMessageDialog(this,"请输入大于0的数量","提示",JOptionPane.ERROR_MESSAGE);
return;
}
//TODO 参数校验
/*/判断是已添加,未添加还是不存在*/
productionImpl productionImpl = new productionImpl(); // 实例化产品服务实现类
production = new Production(); // 创建产品对象
production = productionImpl.findByIdProduction(prodId); // 根据ID查找产品
if(production!=null) {/*商品库有这个商品存在*/
int mark = 0; // 标记变量,用于判断商品是否已存在于购物缓冲区
for(Production p:v) { // 遍历购物缓冲区
if(p.getId().equals(prodId)){/*如果数组中存在相同商品就更新数量和价格*/
sum=p.getSum()+sum;/*数量*/
p.setSum(sum);
p.setPrice(sum*p.getInPrice());/*进货价格*/
mark = 1; // 更新标记
break;
}
}
if(mark==0) {/*插入新的*/
System.out.println("缓存区不存在,插入新的数据");
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(); // 关闭对话框
}
}
}