package com.market6;
/*
* 优惠券界面
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.*;
/*
* jc1=优惠券当现金使用,jc2=输入商品编号,jc3=输入优惠券金额,jc4=商品编号,jc5=商品名称,jc6=商品价格,
* jc7=打折信息;jc8=总价,jc9=总价额,jc10=优惠金额,jc11=优惠额,jc12=应收,jc13=应收额,jc14=实收,
* jc15=找零,jc16=找零额,jc17=空;
* jpc1=商品信息栏,jpc2=输入栏,jpc3=商品价格栏;jpc4=商品信息列表;
* jbc1=完成,jbc2=清空;jbc3=清空;
* jtfc=输入商品编号处,jtfc1=输入优惠券金额处;jtfc2=输入实收金额;
* jsc=滚动条,jtac=显示商品信息栏
* sum1=总价, sum2=优惠后总价, sum3=应收, sum4=实收, sum5=找零, sum6=优惠券输入的面值;
* time=货物数量;a=获得商品编号;
*/
public class Privilege implements KeyListener, ActionListener {
JLabel jc1, jc2, jc3, jc4, jc5, jc6, jc7, jc8, jc9, jc10, jc11, jc12, jc13,
jc14, jc15, jc16, jc17;
JPanel jpc1, jpc2, jpc3, jpc4;
JButton jbc1, jbc2, jbc3;
JTextField jtfc, jtfc1, jtfc2;
JTextArea jta1, jta2;
JScrollPane jsc = null;
JTextArea jtac = null;
float sum1, sum2, sum3, sum4, sum5, sum6;
float f[];
float ff[];
double d[];
double dd[];
float fff[];
String n;
String[] ss;
String sss[];
int time = 1;
int a, b;
ResultSet rs1, rs2, rs3, rs4;
Statement stmt = null;
Connection conn = null;
// 持有MarketClient的引用
MarketClient mc = null;
public Privilege(MarketClient mc) {
this.mc = mc;
f = new float[200];
ff = new float[200];
d = new double[200];
dd = new double[200];
fff = new float[200];
ss = new String[200];
sss = new String[6];
}
// 优惠券窗口
public void privilege() {
mc.jp3.setLayout(new GridLayout(2, 2));
// 定义JLabel
jc1 = new JLabel("优惠券当现金使用", JLabel.CENTER);
jc1.setFont(new Font("隶书", Font.ITALIC, 26));
jc2 = new JLabel("输入商品编号", JLabel.CENTER);
jc2.setFont(new Font("楷书", Font.BOLD, 16));
jc3 = new JLabel("输入优惠券金额", JLabel.CENTER);
jc3.setFont(new Font("楷书", Font.BOLD, 16));
jc4 = new JLabel("商品编号", JLabel.CENTER);
jc5 = new JLabel("商品名称", JLabel.CENTER);
jc6 = new JLabel("商品价格", JLabel.CENTER);
jc7 = new JLabel("打折信息", JLabel.CENTER);
jc8 = new JLabel("总价", JLabel.CENTER);
jc9 = new JLabel("", JLabel.CENTER);
jc10 = new JLabel("优惠金额", JLabel.CENTER);
jc11 = new JLabel("", JLabel.CENTER);
jc12 = new JLabel("应收", JLabel.CENTER);
jc13 = new JLabel("", JLabel.CENTER);
jc14 = new JLabel("实收", JLabel.CENTER);
jc15 = new JLabel("找零", JLabel.CENTER);
jc16 = new JLabel("", JLabel.CENTER);
jc17 = new JLabel();
// 定义JPanel
jpc1 = new JPanel();
jpc1.setBackground(Color.GRAY);
jpc1.setLayout(new BorderLayout());
jpc2 = new JPanel();
jpc2.setLayout(new GridLayout(3, 3, 10, 10));
// jpc2.setBackground(Color.WHITE);
jpc3 = new JPanel();
jpc3.setLayout(new GridLayout(3, 3));
// jpc3.setBackground(Color.blue);
jpc4 = new JPanel();
jpc4.setBackground(Color.PINK);
jpc4.setLayout(new GridLayout(1, 4));
// 定义JButton
jbc1 = new JButton("完成");
jbc1.addActionListener(this);
jbc1.setActionCommand("a");
jbc2 = new JButton("清空");
jbc2.addActionListener(this);
jbc2.setActionCommand("b");
jbc3 = new JButton("确定");
jbc3.addActionListener(this);
jbc3.setActionCommand("c");
// 定义JTextField
jtfc = new JTextField();
jtfc.addKeyListener(this);
jtfc1 = new JTextField();
jtfc1.addKeyListener(this);
jtfc2 = new JTextField();
// 定义JTextArea
jtac = new JTextArea();
jtac.setEditable(false);
// 定义JScrollPane
jsc = new JScrollPane(jtac);
// 往商品信息栏添加组件
jpc4.add(jc4);
jpc4.add(jc5);
jpc4.add(jc6);
jpc4.add(jc7);
jpc1.add(jpc4, BorderLayout.NORTH);
jpc1.add(jsc, BorderLayout.CENTER);
// 往输入栏里添加组件
jpc2.add(jc3);
jpc2.add(jtfc1);
jpc2.add(jc2);
jpc2.add(jtfc);
jpc2.add(jbc1);
jpc2.add(jbc2);
// 往价格信息栏添加组件
jpc3.add(jc8);
jpc3.add(jc9);
jpc3.add(jc10);
jpc3.add(jc11);
jpc3.add(jc12);
jpc3.add(jc13);
jpc3.add(jc14);
jpc3.add(jtfc2);
jpc3.add(jc15);
jpc3.add(jc16);
jpc3.add(jc17);
jpc3.add(jbc3);
// 向优惠券界面加入组件
mc.jp3.add(jc1);
mc.jp3.add(jpc1);
mc.jp3.add(jpc2);
mc.jp3.add(jpc3);
}
@Override
public void keyPressed(KeyEvent e) {
this.getClass();
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
sum6 = Float.valueOf(jtfc1.getText());
// System.out.print("connect!!");
// 获得输入的商品编号
a = Integer.valueOf(jtfc.getText());
// 将输入处清空
jtfc.setText("");
if (a < 100|| a > 104) {
//System.out.println("输入编号错误");
final Dialog dl= new Dialog(mc,null,true,null);
dl.setBackground(Color.WHITE);
dl.setBounds(300, 300,200, 100);
JLabel jj= new JLabel("",JLabel.CENTER);
jj.setText("输入错误!!请重新输入");
jj.setFont(new Font("楷书", Font.BOLD, 14));
dl.add(jj);
dl.addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dl.setVisible(false);
}
});
dl.setVisible(true);
//System.exit(-1);
} else {
int i = time++;
// 连接数据库
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager
.getConnection("jdbc:mysql://localhost/goods?"
+ "user=root&password=root");
Statement stmt = conn.createStatement();
// 在文本中显示处商品信息
rs1 = stmt.executeQuery("select * from allgoods where id="
+ a + "");
while (rs1.next()) {
b = rs1.getInt("id");
// System.out.println(b);
n = rs1.getString("name");
d[i] = rs1.getDouble("price");
dd[i] = rs1.getDouble("information");
}
ss[1] = String.valueOf(b);
ss[2] = String.valueOf(d[i]);
ss[3] = String.valueOf(dd[i]);
f[i] = Float.valueOf(ss[2]);
ff[i] = Float.valueOf(ss[3]);
ss[i] = " " + ss[1] + " " + n
+ " " + ss[2]
+ " " + ss[3] + "折";
jtac.setText(jtac.getText() + "\n" + ss[i]);
// System.out.println("第"+i);
// 在数据库中减一
//stmt.executeUpdate("update allgoods set amout=amout-1 where id="+a+"");
// 计算商品最终价格
fff[i] = (float) (f[i] * ff[i] * 0.1);
// System.out.println(ddd[i]);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
} finally {
try {
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
}
}
// 对键盘的监听处理方法
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
// 对按钮的监听处理方法
public void actionPerformed(ActionEvent e) {
// 按下"完成"键
if (e.getActionCommand().equals("a")) {
// 总价
for (int k = 0; k < f.length; k++) {
sum1 += f[k];
}
// 应收
for (int j = 0; j < fff.length; j++) {
sum3 += fff[j];
}
// 优惠的价钱
sum3 = sum3 - sum6;
sum2 = sum1 - sum3;
// 显示价格信息
sss[0] = String.valueOf(sum1);
jc9.setText(sss[0]);
sss[1] = String.valueOf(sum2