⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ctypeandpre.java

📁 JAVA实现的酒店管理系统
💻 JAVA
字号:
package file1;

/*
 * 功能描述:所有对客户级别的操作的入口类
 * @Author:黄顺武
 * Create Time:---
 * Last Modified:2007-12-15
 * Modify Reason:数据库连接类DBConnection 的内部结构设计得到优化
 */
import java.sql.*;
import javax.swing.*;
import sun.jdbc.rowset.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CTypeAndPre extends JPanel implements ActionListener {

	private JLabel cGrade = new JLabel("客户级别:");
	private JLabel pre = new JLabel("优惠标准:");
	private JLabel firstPay = new JLabel("首付比例:");
	private JTextField gradeTF = new JTextField(10);;
	private JTextField preTF = new JTextField(10);
	private JTextField firstPayTF = new JTextField(10);
	private JPanel p1 = new JPanel();
	private JTable recTable = null;
	private JScrollPane recScrollPane = null;
	private JPanel p2 = new JPanel();
	private JButton add = new JButton("增加记录");
	private JButton delete = new JButton("删除记录");
	private String[] head = { "ID", "客户级别", "优惠标准", "首付比例" };
	private int headNum = 0;
	private String[][] data = null;
	private String[] grade_ids = null;

	public CTypeAndPre() {

		headNum = head.length;
		p1.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 5));
		p1.add(cGrade);
		p1.add(gradeTF);
		gradeTF.setBorder(null);
		gradeTF.setBackground(Color.WHITE);
		preTF.setBorder(null);
		preTF.setBackground(Color.white);
		p1.add(pre);
		p1.add(preTF);
		firstPayTF.setBorder(null);
		firstPayTF.setBackground(Color.WHITE);
		p1.add(firstPay);
		p1.add(firstPayTF);
		p2.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 5));
		p2.add(add);
		p2.add(delete);
		add.setBorder(null);
		add.setBackground(Color.LIGHT_GRAY);
		delete.setBorder(null);
		delete.setBackground(Color.lightGray);
		doIt();
		add.addActionListener(this);
		delete.addActionListener(this);
	}

	private void doIt() {
		try {
			DBConnection con = new DBConnection();
			String sql = "select* from CGrade";
			CachedRowSet crs = con.getResultSet(sql);
			int row = 0;
			while (crs.next()) {
				row++;
			}
			if(row==0){
				delete.setEnabled(false);
			}else{
				delete.setEnabled(true);
			}
			grade_ids = new String[row];
			data = new String[row][headNum];
			crs.beforeFirst();
			row = 0;
			while (crs.next()) {
				grade_ids[row] = String.valueOf(crs.getInt(1));
				data[row][0] = String.valueOf(crs.getInt(1));
				data[row][1] = crs.getString(2);
				data[row][2] = String.valueOf(crs.getFloat(3));
				data[row][3] = String.valueOf(crs.getFloat(4));
				row++;
			}
			recTable = new JTable(data, head);
			recScrollPane = new JScrollPane(recTable);
			this.setLayout(new BorderLayout(0, 15));
			this.add(p1, BorderLayout.NORTH);
			this.add(recScrollPane, BorderLayout.CENTER);
			this.add(p2, BorderLayout.SOUTH);
			this.validate();
		} catch (SQLException sqle) {
			sqle.printStackTrace();
		} catch (ClassNotFoundException cnfe) {
			cnfe.printStackTrace();
		}
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == add) {
			String grade = gradeTF.getText().trim();
			if (grade.equals("")) {
				JOptionPane.showMessageDialog(null, "客户级别不能为空!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			if (preTF.getText().trim().equals("")) {
				JOptionPane.showMessageDialog(null, "优惠标准不能为空!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			if (firstPayTF.getText().trim().equals("")) {
				JOptionPane.showMessageDialog(null, "首付比例不能为空!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			float pre = 0;
			float ficrstPay = 0;
			try {
				pre = Float.valueOf(preTF.getText().trim());
				ficrstPay = Float.valueOf(firstPayTF.getText().trim());
			} catch (NumberFormatException nfe) {
				JOptionPane.showMessageDialog(null, "优惠标准和首付比例均必须为小数或1!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			if (pre <= 0 || ficrstPay <= 0) {
				JOptionPane.showMessageDialog(null, "优惠标准和首付比例均必须为正数!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;

			}
			String query = "select* from CGrade where grade='" + grade + "'";
			CachedRowSet crs = null;
			try {
				DBConnection con = new DBConnection();
				crs = con.getResultSet(query);
				if (crs.next()) {
					JOptionPane.showMessageDialog(null, "该级别已经存在!", "提示",
							JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				String insert = "insert into CGrade values('" + grade + "',"
						+ pre + "," + ficrstPay + ")";
				con.addSql(insert);
				con.doDML();
				doIt();
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			} catch (ClassNotFoundException cnfe) {
				cnfe.printStackTrace();
			}

		}
		if (e.getSource() == delete) {
			String gradeidGet = (String) JOptionPane.showInputDialog(null,
					"请选择要删除的客护级别ID!", "", JOptionPane.INFORMATION_MESSAGE,
					null, grade_ids, grade_ids[0]);
			if (gradeidGet == null) {
				return;
			}
			try {
				DBConnection con = new DBConnection();
				int confirm = JOptionPane.showConfirmDialog(null, "您真的确认删除吗?",
						"", JOptionPane.YES_NO_OPTION);
				if (confirm == JOptionPane.YES_OPTION) {
					String delete = "delete from CGrade where id="
							+ gradeidGet;
					con.addSql(delete);
					con.doDML();
					doIt();
				}
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			} catch (ClassNotFoundException cnfe) {
				cnfe.printStackTrace();
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -