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

📄 notinpermitted.java

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

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

public class NotInPermitted extends JPanel implements ActionListener {

	private JLabel cGrade = new JLabel("客户级别:");
	private JLabel cause = new JLabel("未按时入住原因:");
	private JLabel seed = new JLabel("比例系数:");
	private JComboBox cGradeBox = new JComboBox();
	private JComboBox causeBox = new JComboBox();
	private JTextField seedTF = new JTextField(10);
	private JPanel p1 = new JPanel();
	private JTable recTable = null;
	private String[] head = { "客户级别", "未按时入住原因", "比例系数" };
	private int headNum = 0;
	private String[][] data = null;
	private JScrollPane recScrollPane = null;
	private JPanel p2 = new JPanel();
	private JButton add = new JButton("增加记录");
	private JButton delete = new JButton("删除记录");
	private String[] causeIDs;// 所有未入住原因的id
	private String[] cGrade_ids = null;// 所有客户级别的id

	public NotInPermitted() {

		headNum = head.length;
		seedTF.setBorder(null);
		String index = getGradesAndCauses();
		if (index == null) {
			return;
		}
		p1.setLayout(new FlowLayout(FlowLayout.CENTER, 30, 5));
		p1.add(cGrade);
		p1.add(cGradeBox);
		p1.add(cause);
		p1.add(causeBox);
		p1.add(seed);
		p1.add(seedTF);
		add.setBorder(null);
		add.setBackground(Color.LIGHT_GRAY);
		delete.setBorder(null);
		delete.setBackground(Color.LIGHT_GRAY);
		p2.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 5));
		p2.add(add);
		p2.add(delete);
		doIt();
		add.addActionListener(this);
		delete.addActionListener(this);
	}

	private String getGradesAndCauses() {
		try {
			DBConnection con = new DBConnection();
			String query = "select id,grade from CGrade";
			CachedRowSet crs = con.getResultSet(query);
			int count = 0;
			while (crs.next()) {
				count++;
			}
			if (count == 0) {
				JOptionPane.showMessageDialog(null, "没有客户级别,请您先添加客户级别!", "",
						JOptionPane.INFORMATION_MESSAGE);
				return null;
			}
			cGrade_ids = new String[count];
			crs.beforeFirst();
			count = 0;
			while (crs.next()) {
				cGrade_ids[count++] = String.valueOf(crs.getInt(1));
				cGradeBox.addItem(crs.getString(2));
			}
			cGradeBox.setSelectedIndex(-1);
			crs = null;
			query = "select* from Cause";
			crs = con.getResultSet(query);
			count = 0;
			while (crs.next()) {
				count++;
			}
			if (count == 0) {
				JOptionPane.showMessageDialog(null, "没有未入住原因,请您先添加未入住原因!", "",
						JOptionPane.INFORMATION_MESSAGE);
				return null;
			}
			causeIDs = new String[count];
			count = 0;
			crs.beforeFirst();
			while (crs.next()) {
				causeIDs[count++] = String.valueOf(crs.getInt(1));
				causeBox.addItem(crs.getString(2));
			}
			causeBox.setSelectedIndex(-1);
		} catch (SQLException sqle) {
			sqle.printStackTrace();
			return null;
		} catch (ClassNotFoundException cnfe) {
			cnfe.printStackTrace();
			return null;
		}
		return "success";
	}

	private void doIt() {
		try {
			DBConnection con = new DBConnection();
			String sql = "select grade,cause,timesPermitted from NotInPermitted,Cause,CGrade where causeID=Cause.ID and cGradeID=CGrade.ID";
			CachedRowSet crs = con.getResultSet(sql);
			int count = 0;
			while (crs.next()) {
				count++;
			}
			if (count == 0) {
				delete.setEnabled(false);
			} else {
				delete.setEnabled(true);
			}
			data = new String[count][headNum];
			crs.beforeFirst();
			count = 0;
			while (crs.next()) {
				data[count][0] = crs.getString(1);
				data[count][1] = crs.getString(2);
				data[count][2] = String.valueOf(crs.getInt(3));
				count++;
			}
			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) {
			int grade_id = cGradeBox.getSelectedIndex();
			if (grade_id == -1) {
				JOptionPane.showMessageDialog(null, "客户级别不能为空!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			int causeID = causeBox.getSelectedIndex();
			if (causeID == -1) {
				JOptionPane.showMessageDialog(null, "原因不能为空!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			try {
				String seed = seedTF.getText().trim();
				int seedGet = Integer.valueOf(seed);
				if (seedGet <= 0) {
					JOptionPane.showMessageDialog(null, "比例系数必须为正整数!", "提示",
							JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				DBConnection con = new DBConnection();
				String query = "select* from NotInPermitted where cGradeID="
						+ cGrade_ids[grade_id] + " and causeID="
						+ causeIDs[causeID];
				CachedRowSet crs = con.getResultSet(query);
				if (crs.next()) {
					JOptionPane.showMessageDialog(null, "该记录已经存在!", "提示",
							JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				String insert = "insert into NotInPermitted values("
						+ cGrade_ids[grade_id] + "," + causeIDs[causeID] + ","
						+ seedGet + ")";
				con.addSql(insert);
				con.doDML();
				doIt();
			} catch (NumberFormatException nfe) {
				JOptionPane.showMessageDialog(null, "比例系数必须为正整数!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			} catch (ClassNotFoundException cnfe) {
				cnfe.printStackTrace();
			}
		}
		if (e.getSource() == delete) {
			int grade_id = cGradeBox.getSelectedIndex();
			if (grade_id == -1) {
				JOptionPane.showMessageDialog(null, "客户级别不能为空!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			int causeID = causeBox.getSelectedIndex();
			if (causeID == -1) {
				JOptionPane.showMessageDialog(null, "原因不能为空!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			try {
				DBConnection con = new DBConnection();
				int confirm = JOptionPane.showConfirmDialog(null, "您真的确认删除吗?",
						"", JOptionPane.YES_NO_OPTION);
				if (confirm == JOptionPane.YES_OPTION) {

					StringBuffer deleteSB = new StringBuffer(
							"delete from NotInPermitted where cGradeID=");
					deleteSB.append(grade_id).append(" and causeID=").append(
							causeIDs[causeID]);
					con.addSql(deleteSB.toString());
					con.doDML();
					doIt();
				}
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			} catch (ClassNotFoundException cnfe) {
				cnfe.printStackTrace();
			}
		}
	}
}

⌨️ 快捷键说明

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