|
|
|
|
@ -11,7 +11,6 @@ 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.*;
|
|
|
|
|
@ -26,140 +25,134 @@ import com.lingnan.supermarket.table.OutTableModel;
|
|
|
|
|
import com.lingnan.supermarket.view.OutView;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class OutDialog extends JDialog implements ActionListener{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private JPanel prodIdPanel,sumPanel,phonePanel,opePanel;
|
|
|
|
|
|
|
|
|
|
private JLabel prodIdLabel,sumLabel;
|
|
|
|
|
private JTextField prodIdTF,sumTF;
|
|
|
|
|
|
|
|
|
|
private JButton addBtn,cancelBtn;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class OutDialog extends JDialog implements ActionListener {
|
|
|
|
|
|
|
|
|
|
// 定义JPanel组件,用于组织界面元素
|
|
|
|
|
private JPanel prodIdPanel, sumPanel, phonePanel, opePanel;
|
|
|
|
|
|
|
|
|
|
// 定义JLabel用于显示文本
|
|
|
|
|
private JLabel prodIdLabel, sumLabel;
|
|
|
|
|
|
|
|
|
|
// 定义JTextField用于输入商品ID和数量
|
|
|
|
|
private JTextField prodIdTF, sumTF;
|
|
|
|
|
|
|
|
|
|
// 定义JButton按钮,用于添加商品和取消操作
|
|
|
|
|
private JButton addBtn, cancelBtn;
|
|
|
|
|
|
|
|
|
|
// 创建OutTableModel对象,用于管理表格数据
|
|
|
|
|
private OutTableModel outTableModel = new OutTableModel();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建Buffer对象,用于暂存添加的商品信息
|
|
|
|
|
private Buffer buffer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 构造函数,初始化对话框,设置窗口属性
|
|
|
|
|
public OutDialog(JFrame parent) {
|
|
|
|
|
super(parent,"添加商品");
|
|
|
|
|
|
|
|
|
|
setSize(350,300);
|
|
|
|
|
setLocationRelativeTo(null);
|
|
|
|
|
setModal(true);
|
|
|
|
|
setResizable(false);
|
|
|
|
|
this.setLayout(new FlowLayout());
|
|
|
|
|
|
|
|
|
|
initView();
|
|
|
|
|
super(parent, "添加商品");
|
|
|
|
|
|
|
|
|
|
setSize(350, 300); // 设置窗口大小
|
|
|
|
|
setLocationRelativeTo(null); // 设置窗口居中
|
|
|
|
|
setModal(true); // 设置为模态对话框,阻止父窗口操作
|
|
|
|
|
setResizable(false); // 设置不可调整窗口大小
|
|
|
|
|
this.setLayout(new FlowLayout()); // 设置布局方式为流式布局
|
|
|
|
|
|
|
|
|
|
initView(); // 调用初始化界面方法
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化界面方法
|
|
|
|
|
private void initView() {
|
|
|
|
|
// 创建商品ID输入面板
|
|
|
|
|
prodIdPanel = new JPanel();
|
|
|
|
|
prodIdLabel = new JLabel("编号");
|
|
|
|
|
prodIdTF = new JTextField(15);
|
|
|
|
|
prodIdPanel.add(prodIdLabel);
|
|
|
|
|
prodIdPanel.add(prodIdTF);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
prodIdTF = new JTextField(15); // 设置商品ID的文本框长度为15
|
|
|
|
|
prodIdPanel.add(prodIdLabel); // 将标签添加到面板
|
|
|
|
|
prodIdPanel.add(prodIdTF); // 将文本框添加到面板
|
|
|
|
|
|
|
|
|
|
// 创建数量输入面板
|
|
|
|
|
sumPanel = new JPanel();
|
|
|
|
|
sumLabel = new JLabel("数量");
|
|
|
|
|
sumTF = new JTextField(15);
|
|
|
|
|
sumPanel.add(sumLabel);
|
|
|
|
|
sumPanel.add(sumTF);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sumTF = new JTextField(15); // 设置数量的文本框长度为15
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
addBtn.addActionListener(this); // 添加按钮的事件监听
|
|
|
|
|
cancelBtn.addActionListener(this); // 取消按钮的事件监听
|
|
|
|
|
opePanel.add(addBtn); // 将添加按钮添加到面板
|
|
|
|
|
opePanel.add(cancelBtn); // 将取消按钮添加到面板
|
|
|
|
|
|
|
|
|
|
// 将所有面板添加到容器中
|
|
|
|
|
Container container = getContentPane();
|
|
|
|
|
container.add(prodIdPanel);
|
|
|
|
|
container.add(sumPanel);
|
|
|
|
|
container.add(opePanel);
|
|
|
|
|
container.add(prodIdPanel); // 添加商品ID面板
|
|
|
|
|
container.add(sumPanel); // 添加数量面板
|
|
|
|
|
container.add(opePanel); // 添加操作按钮面板
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 实现ActionListener接口的actionPerformed方法,处理按钮点击事件
|
|
|
|
|
@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 =prodIdTF.getText();
|
|
|
|
|
System.out.println("proId="+prodId);
|
|
|
|
|
if(prodIdTF.getText().equals("")||sumTF.getText().equals("")) {
|
|
|
|
|
JOptionPane.showMessageDialog(this,"请输入完整","提示",JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
return;
|
|
|
|
|
Object source = e.getSource(); // 获取触发事件的源对象
|
|
|
|
|
if (source == addBtn) { // 如果点击了"添加"按钮
|
|
|
|
|
|
|
|
|
|
// 获取商品ID和数量输入框的值
|
|
|
|
|
String prodId = prodIdTF.getText();
|
|
|
|
|
System.out.println("proId=" + prodId);
|
|
|
|
|
|
|
|
|
|
// 判断输入框是否为空
|
|
|
|
|
if (prodIdTF.getText().equals("") || 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;
|
|
|
|
|
|
|
|
|
|
// 获取数量并进行格式化转换
|
|
|
|
|
int sum = Integer.parseInt(sumTF.getText());
|
|
|
|
|
|
|
|
|
|
// 判断数量是否为负数
|
|
|
|
|
if (sum < 0) {
|
|
|
|
|
JOptionPane.showMessageDialog(this, "请输入大于0的数量", "提示", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
return; // 如果为负数,弹出提示框并返回
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO 参数校验
|
|
|
|
|
/*/判断是已添加,未添加还是不存在*/
|
|
|
|
|
|
|
|
|
|
// 创建生产商品的实现对象,查询商品库
|
|
|
|
|
productionImpl productionImpl = new productionImpl();
|
|
|
|
|
Production production = new Production();
|
|
|
|
|
production = productionImpl.findByIdProduction(prodId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(production!=null) {/*商品库有这个商品存在*/
|
|
|
|
|
|
|
|
|
|
buffer = new Buffer();
|
|
|
|
|
BufferImpl BufferImpl = new BufferImpl();
|
|
|
|
|
buffer = BufferImpl.findOutBufferbyId(prodId);/*判断购物车是否有这个商品了,获得已添加的sum和价格*/
|
|
|
|
|
|
|
|
|
|
int allSum = production.getSum();
|
|
|
|
|
|
|
|
|
|
if(sum<0) {
|
|
|
|
|
JOptionPane.showMessageDialog(this,"请输入大于0的数量","提示",JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
production = productionImpl.findByIdProduction(prodId); // 根据商品ID查询商品
|
|
|
|
|
|
|
|
|
|
if (production != null) { // 如果商品存在
|
|
|
|
|
buffer = new Buffer(); // 创建缓冲区对象
|
|
|
|
|
BufferImpl bufferImpl = new BufferImpl();
|
|
|
|
|
buffer = bufferImpl.findOutBufferbyId(prodId); // 查找购物车是否已存在该商品
|
|
|
|
|
|
|
|
|
|
int allSum = production.getSum(); // 获取商品的库存数量
|
|
|
|
|
|
|
|
|
|
// 如果商品库存不足,弹出提示框并返回
|
|
|
|
|
if (sum > allSum) {
|
|
|
|
|
JOptionPane.showMessageDialog(this, "库存数量不够,库存数:" + allSum, "提示", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(buffer!=null)/*购物缓冲区已经有添加的商品*/{
|
|
|
|
|
int exeistSum = buffer.getSum();
|
|
|
|
|
if(sum+exeistSum>allSum)/*库存不够*/{
|
|
|
|
|
JOptionPane.showMessageDialog(this,"库存数量不够,库存数:"+allSum,"提示",JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
return;
|
|
|
|
|
}else
|
|
|
|
|
BufferImpl.addOutBufferExeistProd(prodId, sum, buffer);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else if(buffer==null){/*添加新的商品*/
|
|
|
|
|
if(sum>allSum)/*库存不够*/{
|
|
|
|
|
JOptionPane.showMessageDialog(this,"库存数量不够,库存数:"+allSum,"提示",JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
return;
|
|
|
|
|
}else
|
|
|
|
|
BufferImpl.addOutBufferNewProd(prodId, sum);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.dispose();
|
|
|
|
|
JOptionPane.showMessageDialog(this,"添加成功","提示",JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
// 如果购物车中已经有该商品,更新数量和价格
|
|
|
|
|
if (buffer != null) {
|
|
|
|
|
int exeistSum = buffer.getSum(); // 获取购物车中该商品已有的数量
|
|
|
|
|
if (sum + exeistSum > allSum) { // 判断购物车中商品数量加起来是否超过库存
|
|
|
|
|
JOptionPane.showMessageDialog(this, "库存数量不够,库存数:" + allSum, "提示", JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
return; // 如果超过库存,弹出提示框并返回
|
|
|
|
|
} else {
|
|
|
|
|
bufferImpl.addOutBufferExeistProd(prodId, sum, buffer); // 更新购物车中的商品数量
|
|
|
|
|
}
|
|
|
|
|
} else { // 如果购物车中没有该商品
|
|
|
|
|
bufferImpl.addOutBufferNewProd(prodId, sum); // 将商品添加到购物车
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.dispose(); // 关闭当前对话框
|
|
|
|
|
JOptionPane.showMessageDialog(this, "添加成功", "提示", JOptionPane.INFORMATION_MESSAGE); // 弹出添加成功提示
|
|
|
|
|
} else { // 如果商品不存在
|
|
|
|
|
JOptionPane.showMessageDialog(this, "商品不存在", "提示", JOptionPane.ERROR_MESSAGE); // 弹出商品不存在提示
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
JOptionPane.showMessageDialog(this,"商品不存在","提示",JOptionPane.ERROR_MESSAGE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if(source == cancelBtn) {
|
|
|
|
|
|
|
|
|
|
this.dispose();
|
|
|
|
|
} else if (source == cancelBtn) { // 如果点击了"取消"按钮
|
|
|
|
|
this.dispose(); // 关闭当前对话框
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|