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

187 lines
6.6 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.util.Vector; // 导入向量类,用于存储对象集合
import javax.swing.JButton; // 导入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.JTable; // 导入Swing表格组件
import javax.swing.JTextField; // 导入Swing文本框组件
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.OutView; // 导入出库视图类
public class ChangeSumDialog extends JDialog implements ActionListener{ // 定义一个对话框类ChangeSumDialog继承自JDialog并实现ActionListener接口
private JPanel prodIdPanel,sumPanel,phonePanel,opePanel,titlePanel; // 声明多个面板变量,用于布局不同的组件
private JLabel prodIdLabel,sumLabel,titleLabel; // 声明标签变量,用于显示文本
private JTextField prodIdTF,sumTF; // 声明文本框变量,用于输入数据
private JButton UpdateBtn,cancelBtn; // 声明按钮变量,用于执行更新和取消操作
private OutTableModel outTableModel = new OutTableModel(); // 创建出库表格模型实例
private Buffer buffer; // 声明缓冲区对象变量
private String prodId,mark; /*mark用来标记是进货还是出货系统*/ // 声明字符串变量用于存储产品ID和标记
private Vector<Production> v; // 声明向量变量,用于存储产品集合
public ChangeSumDialog(JFrame parent,String prodId,String mark,Vector<Production> v) { // 构造方法接收父窗口、产品ID、标记和产品集合作为参数
super(parent,"更改商品数量"); // 调用父类构造方法,设置对话框标题
setSize(350,200); // 设置对话框大小
setLocationRelativeTo(null); // 设置对话框相对于父窗口居中显示
setModal(true); // 设置对话框为模态
setResizable(false); // 设置对话框不可调整大小
this.setLayout(new FlowLayout()); // 设置对话框布局为流式布局
this.prodId=prodId; // 将参数prodId赋值给成员变量prodId
this.mark=mark; // 将参数mark赋值给成员变量mark
this.v = v; // 将参数v赋值给成员变量v
initView(); // 初始化视图
}
public ChangeSumDialog(JFrame parent,String prodId,String mark) { // 另一个构造方法接收父窗口、产品ID和标记作为参数
super(parent,"更改商品数量"); // 调用父类构造方法,设置对话框标题为"更改商品数量"
setSize(350,200); // 设置对话框大小为350x200像素
setLocationRelativeTo(null); // 设置对话框位置相对于父窗口居中显示
setModal(true); // 设置对话框为模态,即用户必须处理完此对话框才能操作其他窗口
setResizable(false); // 设置对话框大小不可变
this.setLayout(new FlowLayout()); // 设置对话框布局为流式布局
this.prodId=prodId; // 将传入的产品ID赋值给成员变量prodId
this.mark=mark; // 将传入的标记赋值给成员变量mark
initView(); // 调用方法初始化视图
}
private void initView() {
titlePanel = new JPanel();
titleLabel = new JLabel("修改商品id为"+prodId+"的数量");
titlePanel.add(titleLabel);
sumPanel = new JPanel();
sumLabel = new JLabel("数量");
sumTF = new JTextField(15);
sumPanel.add(sumLabel);
sumPanel.add(sumTF);
opePanel = new JPanel();
UpdateBtn = new JButton("更改");
cancelBtn = new JButton("取消");
UpdateBtn.addActionListener(this);
cancelBtn.addActionListener(this);
opePanel.add(UpdateBtn);
opePanel.add(cancelBtn);
Container container = getContentPane();
container.add(titlePanel);
container.add(sumPanel);
container.add(opePanel);
}
public Vector<Production> getVector(){
return v;
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source==UpdateBtn){
//TODO 参数校验
/*/返回这个记录的信息*/
int sum = Integer.parseInt(sumTF.getText());/*获得数量*/
System.out.println("所要修改的数量sum="+sum);
if(sumTF.getText().equals("")) {
JOptionPane.showMessageDialog(this,"请输入完整","提示",JOptionPane.ERROR_MESSAGE);
return;
}
if(sum<0) {/*判断输入大于0*/
JOptionPane.showMessageDialog(this,"请输入大于0的数量","提示",JOptionPane.ERROR_MESSAGE);
return;
}
BufferImpl bufferImpl = new BufferImpl();
productionImpl productionImpl = new productionImpl();
Production production = new Production();
production = productionImpl.findByIdProduction(prodId);
Buffer buffer = new Buffer();
boolean flag = false;
if(mark=="In") {/*进货界面*/
for(Production p:v) {
if(p.getId().equals(prodId))
p.setSum(sum);
}
}
else if(mark=="Out") {/*出货界面*/
buffer = bufferImpl.findInBufferbyId(prodId);
if(buffer!=null) {/*记录有这条数据*/
if(sum>production.getSum())/*修改数量超过库存*/
JOptionPane.showMessageDialog(this,"库存数量为:"+production.getSum()+",修改数量请勿超过库存","提示",JOptionPane.ERROR_MESSAGE);
else
flag = bufferImpl.UpdateInBufferById(prodId, sum);
}
}
if(flag = true) {/*如果修改成功*/
JOptionPane.showMessageDialog(this,"修改成功","提示",JOptionPane.INFORMATION_MESSAGE);
dispose();
}else {
JOptionPane.showMessageDialog(this,"修改失败","提示",JOptionPane.ERROR_MESSAGE);
dispose();
}
}
else if(source == cancelBtn) {
this.dispose();
}
}
}