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

📄 housebookanduse.java

📁 JAVA实现的酒店管理系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package file1;

/*
 * 功能描述:酒店客房的预定与入住登记以及客房分配入口
 * Author:黄顺武
 * Time:---
 * Last Modified:2007-12-15
 * Modified Content:数据库连接类DBConnection
 * Modify Reason:数据库连接类DBConnection 的内部结构设计得到优化
 */
import java.sql.*;
import sun.jdbc.rowset.*;
import java.util.Date;
import java.util.StringTokenizer;
import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class HouseBookAndUse extends JPanel implements ActionListener,
		ItemListener {
	private JLabel cGrade = new JLabel("客户级别:", SwingConstants.CENTER);
	private JLabel name = new JLabel("客户名字:", SwingConstants.CENTER);
	private JLabel hGrade = new JLabel("客房级别:", SwingConstants.CENTER);
	private JLabel bookNum = new JLabel("订房数量:", SwingConstants.CENTER);
	private JLabel checkIn = new JLabel("入住日期:", SwingConstants.CENTER);
	private JLabel checkOut = new JLabel("退房日期:", SwingConstants.CENTER);
	private JLabel type = new JLabel("预付比例:", SwingConstants.CENTER);
	private JLabel state = new JLabel("记录状态:", SwingConstants.CENTER);
	private JButton autoResearch = new JButton("自动分房");
	private JLabel firstPay = new JLabel("预付金额:", SwingConstants.CENTER);
	private JComboBox cGradeBox = new JComboBox();
	private JTextField nameTF = new JTextField(12);
	private JComboBox hGradeBox = new JComboBox();
	private JTextField hNumTF = new JTextField(10);
	private JTextField checkInTF = new JTextField(10);
	private JTextField checkOutTF = new JTextField(10);
	private JTextField typeTF = new JTextField(10);
	private JComboBox stateBox = new JComboBox(new String[] { "是", "否" });
	private JTextField autoHNoTF = new JTextField(10);
	private JTextField firstPayTF = new JTextField(10);
	private JPanel p1 = new JPanel();
	private JTable recordTable;
	private JScrollPane recScrollPane;
	private String[] title = { "ID", "客户名字", "客户级别", "数量", "客房号", "入住日期",
			"退房日期", "酒水消费金额", "状态" };
	private int titleNum = 0;
	private String tableData[][] = null;
	private JPanel p3 = new JPanel();
	private JButton add = new JButton("增加记录");
	private JButton modify = new JButton("修改记录");
	private JButton delete = new JButton("删除记录");
	private float[] hPrice;
	private String[] hUseAndBookID;// 记录订房记录的ID
	private String[] cGrade_ids = null;// 所有客户级别的id
	private String id = null;
	private GetDate dateGet;
	private AddSomeDays addDay;
	private Formatter format = new Formatter();
	private String hG = null;// 修改记录时记录修改前的客户级别
	private int num;// 修改记录时记录修改前的订房数量
	private String hNo = null;// 修改记录时的客房号

	public HouseBookAndUse() {

		autoResearch.setBorder(null);
		autoResearch.setBackground(Color.LIGHT_GRAY);
		nameTF.setBorder(null);
		hNumTF.setBorder(null);
		checkInTF.setBorder(null);
		checkOutTF.setBorder(null);
		typeTF.setBorder(null);
		autoHNoTF.setBorder(null);
		firstPayTF.setBorder(null);
		String index = getTitles();
		if (index.equals("failture")) {
			return;
		}
		titleNum = title.length;
		dateGet = new GetDate();
		addDay = new AddSomeDays();
		p1.setLayout(new GridLayout(2, 10, 2, 10));
		p1.add(cGrade);
		p1.add(cGradeBox);
		p1.add(name);
		p1.add(nameTF);
		p1.add(hGrade);
		p1.add(hGradeBox);
		p1.add(bookNum);
		p1.add(hNumTF);
		p1.add(autoResearch);
		p1.add(autoHNoTF);
		p1.add(checkIn);
		p1.add(checkInTF);
		p1.add(checkOut);
		p1.add(checkOutTF);
		p1.add(type);
		p1.add(typeTF);
		p1.add(state);
		p1.add(stateBox);
		p1.add(firstPay);
		p1.add(firstPayTF);
		p3.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 5));
		add.setBorder(null);
		add.setBackground(Color.LIGHT_GRAY);
		modify.setBorder(null);
		modify.setBackground(Color.LIGHT_GRAY);
		delete.setBorder(null);
		delete.setBackground(Color.LIGHT_GRAY);
		p3.add(add);
		p3.add(modify);
		p3.add(delete);
		this.setLayout(new BorderLayout(0, 5));
		String returnValue = doIt();
		if (returnValue == null) {
			return;
		}
		cGradeBox.addItemListener(this);
		autoResearch.addActionListener(this);
		modify.addActionListener(this);
		add.addActionListener(this);
		delete.addActionListener(this);
	}

	private String getTitles() {
		String sql = "select id,grade from CGrade";
		CachedRowSet crs = null;
		int count = 0;
		try {
			DBConnection con = new DBConnection();
			crs = con.getResultSet(sql);
			while (crs.next()) {
				count++;
			}
			if (count == 0) {
				JOptionPane.showMessageDialog(null, "数据库中没客户级别记录,请先添加级别!",
						"提示", JOptionPane.INFORMATION_MESSAGE);
				return "failture";
			}
			crs.beforeFirst();
			cGrade_ids = new String[count];
			count = 0;
			while (crs.next()) {
				cGrade_ids[count++] = String.valueOf(crs.getInt(1));
				cGradeBox.addItem(crs.getString(2));
			}
			cGradeBox.setSelectedIndex(-1);
			crs = null;
			sql = "select* from HGrade";
			crs = con.getResultSet(sql);
			count = 0;
			while (crs.next()) {
				count++;
			}
			if (count == 0) {
				JOptionPane.showMessageDialog(null, "数据库中没客房级别记录,请先添加客房级别!",
						"提示", JOptionPane.INFORMATION_MESSAGE);
				return "failture";
			}
			hPrice = new float[count];
			crs.beforeFirst();
			count = 0;
			while (crs.next()) {
				hGradeBox.addItem(crs.getString(1));
				hPrice[count] = crs.getFloat(2);
				count++;
			}
			hGradeBox.setSelectedIndex(-1);
		} catch (SQLException sqle) {
			sqle.printStackTrace();
			return "failture";
		} catch (ClassNotFoundException cnfe) {
			cnfe.printStackTrace();
			return "failture";
		}
		return "success";
	}

	private String doIt() {
		try {
			String sql = "select HUseBook.ID,cName,grade,bookNum,hNo,dateIn,dateOut,consume,state"
					+ " from HUseBook,Client,CGrade where rendererID=Client.ID and cGradeID=CGrade.ID";
			DBConnection con = new DBConnection();
			CachedRowSet crs = con.getResultSet(sql);
			int count = 0;
			while (crs.next()) {
				count++;
			}
			if (count == 0) {
				JOptionPane.showMessageDialog(null, "数据库中没有入住记录!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				modify.setEnabled(false);
				delete.setEnabled(false);
			} else {
				modify.setEnabled(true);
				delete.setEnabled(true);
			}
			tableData = new String[count][titleNum];
			hUseAndBookID = new String[count];
			crs.beforeFirst();
			count = 0;
			int row = 0;
			while (crs.next()) {
				hUseAndBookID[count] = String.valueOf(crs.getInt(1));
				tableData[row][0] = String.valueOf(crs.getInt(1));
				tableData[row][1] = crs.getString(2);
				tableData[row][2] = crs.getString(3);
				tableData[row][3] = String.valueOf(crs.getInt(4));
				tableData[row][4] = crs.getString(5);
				tableData[row][5] = crs.getString(6);
				tableData[row][6] = crs.getString(7);
				tableData[row][7] = String.valueOf(crs.getFloat(8));
				tableData[row][8] = crs.getString(9);
				row++;
				count++;
			}
			recordTable = new JTable(tableData, title);
			recScrollPane = new JScrollPane(recordTable);
			this.add(recScrollPane, BorderLayout.CENTER);
			this.add(p1, BorderLayout.NORTH);
			this.add(recScrollPane, BorderLayout.CENTER);
			this.add(p3, BorderLayout.SOUTH);
			this.validate();
		} catch (SQLException sqle) {
			sqle.printStackTrace();
			return null;
		} catch (ClassNotFoundException cnfe) {
			cnfe.printStackTrace();
			return null;
		}
		return "success";
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == autoResearch) {
			String index = research();
			if (index == null) {
				return;
			}
		}
		if (e.getSource() == add) {
			if (modify.getText().equals("确认修改")) {
				modify.setText("修改记录");
			}
			int cGradeIndex = cGradeBox.getSelectedIndex();
			if (cGradeIndex == -1) {
				JOptionPane.showMessageDialog(null, "客户级别不能为空!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			String name = nameTF.getText();
			if (name.equals("")) {
				JOptionPane.showMessageDialog(null, "名字不能为空!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			int houseGradeIndex = hGradeBox.getSelectedIndex();
			if (houseGradeIndex == -1) {
				JOptionPane.showMessageDialog(null, "客房级别不能为空!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			int numOfHouse;
			try {
				numOfHouse = Integer.valueOf(hNumTF.getText());
				if (numOfHouse <= 0) {
					JOptionPane.showMessageDialog(null, "订房数量必须为正整数!", "提示",
							JOptionPane.INFORMATION_MESSAGE);
					return;
				}
			} catch (NumberFormatException nfe) {
				JOptionPane.showMessageDialog(null, "订房数量必须为正整数!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			String type = typeTF.getText();
			if (type.equals("")) {
				JOptionPane.showMessageDialog(null, "预付款比例不能为空!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			String checkIn = dateGet.getDate(checkInTF.getText());
			String checkOut = dateGet.getDate(checkOutTF.getText());
			int stateIndex = stateBox.getSelectedIndex();
			if (stateIndex == -1 || stateIndex == 1) {
				JOptionPane.showMessageDialog(null, "状态不能为空且必须选择'是'!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			String state = (String) stateBox.getSelectedItem();
			String bookedHNo = autoHNoTF.getText();
			if (bookedHNo.equals("")) {
				JOptionPane.showMessageDialog(null, "客房号不能为空!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			if (autoResearch.isEnabled()) {
				JOptionPane.showMessageDialog(null, "请先点击自动分房按钮分配客房!", "提示",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			try {
				DBConnection con = new DBConnection();
				String queryStr = null;
				String grade = null;
				CachedRowSet crs = null;
				if (bookedHNo.indexOf(",") == -1) {
					queryStr = "select hGrade from House where HouseNo='"
							+ bookedHNo + "'";
					crs = con.getResultSet(queryStr);
					if (crs.next()) {
						grade = crs.getString(1);
					}
				} else {
					StringTokenizer st = new StringTokenizer(bookedHNo, ",");
					String sHNO = null;
					int base = 0;
					while (st.hasMoreTokens()) {
						sHNO = st.nextToken();
						if (base == 0) {
							break;
						}
					}
					queryStr = "select hGrade from House where HouseNo='"
							+ sHNO + "'";
					crs = con.getResultSet(queryStr);
					if (crs.next()) {
						grade = crs.getString(1);
					}
				}
				crs = null;
				String query = "select* from Client where cName='" + name + "'";
				crs = con.getResultSet(query);
				if (!crs.next()) {
					int confirm = JOptionPane.showConfirmDialog(null,
							"该客户是新客户吗?", "", JOptionPane.YES_NO_OPTION);
					if (confirm == -1) {
						return;
					}
					if (confirm == JOptionPane.YES_OPTION) {
						String insert = "insert into Client values("
								+ cGrade_ids[cGradeIndex] + ",'" + name
								+ "','')";
						DBConnection conn = new DBConnection();
						conn.addSql(insert);
						conn.doDML();
					}
				}
				crs = null;
				crs = con.getResultSet("select* from Client");
				int renderid = -1;
				while (crs.next()) {
					if (crs.isLast()) {
						renderid = crs.getInt(1);
					}
				}
				String insert = "insert into HUseBook values(" + renderid + ","
						+ numOfHouse + ",'" + bookedHNo + "','" + grade + "','"
						+ checkIn + "','" + checkOut + "',0,'" + state + "')";
				con.addSql(insert);
				if (!bookedHNo.contains(",")) {
					String update = "update House set state='否',beginUseable='"
							+ addDay.addDays(checkOut, 1) + "' where HouseNo='"
							+ bookedHNo + "'";
					con.addSql(update);
				} else {
					StringTokenizer st = new StringTokenizer(bookedHNo, ",");
					while (st.hasMoreTokens()) {
						String hNo = st.nextToken();
						if (hNo != null) {
							String update = "update House set state='否',beginUseable='"
									+ addDay.addDays(checkOut, 1)
									+ "' where HouseNo='" + hNo + "'";
							con.addSql(update);
						}
					}

				}
				con.doDML();
				String returnValue = doIt();
				if (returnValue == null) {
					return;
				}
				buttonsSet();
				hNumTF.setEditable(true);
				autoResearch.setEnabled(true);
				modify.setEnabled(true);
				delete.setEnabled(true);
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			} catch (ClassNotFoundException cnfe) {
				cnfe.printStackTrace();
			}
		}
		if (e.getSource() == modify) {

⌨️ 快捷键说明

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