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

📄 checkout.java

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

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

public class CheckOut extends JPanel implements ActionListener {

	private JLabel cName = new JLabel("客户名称:");
	private JComboBox nameBox = new JComboBox();
	private JButton query = new JButton("       查询       ");
	private JButton checkOut = new JButton("       结帐       ");
	private JLabel moneyLB = new JLabel("租金:");
	private JTextField moneyTF = new JTextField(8);
	private JLabel consumeLB = new JLabel("消费金额:  ");
	private JTextField consumeTF = new JTextField(8);
	private JLabel preLB = new JLabel("优惠标准:");
	private JTextField preTF = new JTextField(10);
	private JLabel firstPayLB = new JLabel("预付金额:");
	private JTextField firstPayTF = new JTextField(8);
	private JLabel remained = new JLabel("本次应付总金额:");
	private JTextField remainedTF = new JTextField(8);
	private JPanel p1 = new JPanel();
	private JPanel p2 = new JPanel();
	private JPanel p3 = new JPanel();
	private JTable resultTable = null;
	private String[] title = { "客户名称", "订房数量", "所订客房号", "客房级别", "入住日期",
			"原定退房日期", "实际退房时间", "酒水消费金额", "记录目前状态" };
	private int titleNum = 0;
	private String[][] data = null;
	private JScrollPane resultPane = new JScrollPane();
	private DBConnection con = null;
	private GetDate dateGet = null;
	private Formatter format = null;
	private String[] IDS = null;
	private Date d = new Date();
	private String value = null;
	private String todayStr = null;
	private AddSomeDays addDay = null;
	private String[] client_ids = null;// 存储用户的所有ID

	public CheckOut() {
		cName.setBackground(Color.LIGHT_GRAY);
		cName.setBorder(null);
		titleNum = title.length;
		String index = getNameOfClients();
		if (index == null) {
			return;
		}
		dateGet = new GetDate();
		format = new Formatter();
		addDay = new AddSomeDays();
		query.setBackground(Color.LIGHT_GRAY);
		query.setBorder(null);
		query.setEnabled(true);
		checkOut.setEnabled(false);
		checkOut.setBackground(Color.LIGHT_GRAY);
		checkOut.setBorder(null);
		moneyTF.setBackground(Color.LIGHT_GRAY);
		moneyTF.setBorder(null);
		moneyTF.setEditable(false);
		consumeTF.setBackground(Color.LIGHT_GRAY);
		consumeTF.setBorder(null);
		consumeTF.setEditable(false);
		preTF.setBackground(Color.LIGHT_GRAY);
		preTF.setBorder(null);
		preTF.setEditable(false);
		firstPayTF.setBackground(Color.LIGHT_GRAY);
		firstPayTF.setBorder(null);
		firstPayTF.setEditable(false);
		remainedTF.setBackground(Color.LIGHT_GRAY);
		remainedTF.setBorder(null);
		remainedTF.setEditable(false);
		remained.setForeground(Color.red);
		p1.setLayout(new FlowLayout(FlowLayout.CENTER, 35, 0));
		p1.add(cName);
		p1.add(nameBox);
		p1.add(query);
		p1.add(remained);
		p1.add(remainedTF);
		p1.add(checkOut);
		p2.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 0));
		p2.add(moneyLB);
		p2.add(moneyTF);
		p2.add(consumeLB);
		p2.add(consumeTF);
		p2.add(preLB);
		p2.add(preTF);
		p2.add(firstPayLB);
		p2.add(firstPayTF);
		p3.setLayout(new BorderLayout(0, 20));
		p3.add(p1, BorderLayout.NORTH);
		p3.add(p2, BorderLayout.CENTER);
		this.setLayout(new BorderLayout(0, 20));
		this.add(p3, BorderLayout.NORTH);
		query.addActionListener(this);
		checkOut.addActionListener(this);
	}

	private String getNameOfClients() {
		try {
			con = new DBConnection();
			String queryStr = "select distinct rendererID,cName from HUseBook,Client where state='是' and rendererID=Client.id";
			CachedRowSet crs = con.getResultSet(queryStr);
			int count = 0;
			while (crs.next()) {
				count++;
			}
			crs.beforeFirst();
			if (count == 0) {
				JOptionPane.showMessageDialog(null, "没有未结帐的客户!", "",
						JOptionPane.INFORMATION_MESSAGE);
				return null;
			}
			client_ids = new String[count];
			count = 0;
			while (crs.next()) {
				client_ids[count++] = String.valueOf(crs.getInt(1));
				nameBox.addItem(crs.getString(2));
			}
			nameBox.setSelectedIndex(-1);
		} catch (SQLException sqle) {
			sqle.printStackTrace();
			return null;
		} catch (ClassNotFoundException cnfe) {
			cnfe.printStackTrace();
		}
		return "success";
	}

	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource() == query) {
			int name_index = nameBox.getSelectedIndex();
			if (name_index == -1) {
				JOptionPane.showMessageDialog(null, "请选择客户名称!", "",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			String queryStr = "select HUseBook.ID,cName,bookNum,hNo,hGrade,dateIn,dateOut,consume,state  from HUseBook,Client where rendererID="
					+ client_ids[name_index] + " and state='是' and rendererID=Client.ID";
			CachedRowSet crs = null;
			try {
				crs = con.getResultSet(queryStr);
				int row = 0;
				while (crs.next()) {
					row++;
				}
				if (row == 0) {
					JOptionPane.showMessageDialog(null, "该客户没有订房记录!请您确认!", "",
							JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				IDS = new String[row];
				data = new String[row][titleNum];
				crs.beforeFirst();
				row = 0;
				String today = format.valueConverted(d);
				while (crs.next()) {
					IDS[row] = String.valueOf(crs.getInt(1));
					data[row][0] = crs.getString(2);
					data[row][1] = String.valueOf(crs.getInt(3));
					data[row][2] = crs.getString(4);
					data[row][3] = crs.getString(5);
					data[row][4] = crs.getString(6);
					data[row][5] = crs.getString(7);
					data[row][6] = today;
					data[row][7] = String.valueOf(crs.getFloat(8));
					data[row][8] = crs.getString(9);
					row++;
				}
				resultTable = new JTable(data, title);
				resultPane = new JScrollPane(resultTable);
				this.add(resultPane, BorderLayout.CENTER);
				this.validate();
				crs = null;
				row = resultTable.getModel().getRowCount();
				float firstPayPercent = 0;
				float firstPay = 0;
				float standard = 0;
				float moneyOfBooked = 0;// 订房的钱的数目
				float moneyOfConsume = 0;
				String nameFirst = (String) nameBox.getSelectedItem();
				if (nameFirst == null) {
					JOptionPane.showMessageDialog(null, "请选择客户名称!", "",
							JOptionPane.INFORMATION_MESSAGE);
					return;
				}
				queryStr = "select standard,firstPayPercent from CGrade,Client where CGrade.id=cGradeID and Client.ID="
						+ client_ids[name_index];

				crs = con.getResultSet(queryStr);
				while (crs.next()) {
					standard = crs.getFloat(1);
					firstPayPercent = crs.getFloat(2);
				}
				crs = null;
				String in = null;
				String inAfterAdded = null;
				String checkOutStr = null;
				value = format.valueConverted(d);
				todayStr = dateGet.getDate(value);
				for (int count = 0; count < row; count++) {
					// demand modify here
					in = (String) resultTable.getModel().getValueAt(count, 5);
					checkOutStr = (String) resultTable.getModel().getValueAt(
							count, 6);
					int dayNumActual = 1;
					for (;; dayNumActual++) {
						inAfterAdded = addDay.addDays(in, dayNumActual);
						if (inAfterAdded.equals(todayStr)) {
							break;
						}
					}
					dayNumActual++;// 加一后才符合逻辑
					int dayNumIni = 1;
					for (;; dayNumIni++) {
						inAfterAdded = addDay.addDays(in, dayNumIni);
						if (inAfterAdded.equals(checkOutStr)) {
							break;
						}
					}
					dayNumIni++;// 加一后才符合逻辑
					String numStr = (String) resultTable.getModel().getValueAt(
							count, 1);
					int num = Integer.valueOf(numStr);
					String hGrade = (String) resultTable.getModel().getValueAt(
							count, 3);
					String consumeStr = (String) resultTable.getModel()
							.getValueAt(count, 7);
					float consume = Float.valueOf(consumeStr);
					queryStr = "select Sprice from HGrade where grade='"
							+ hGrade + "'";
					crs = con.getResultSet(queryStr);
					float price = 0;
					if (crs.next()) {
						price = crs.getFloat(1);
					}
					moneyOfConsume += consume;
					moneyOfBooked += (num * price * dayNumActual * standard);
					firstPay += (num * price * dayNumIni * standard * firstPayPercent);
				}
				float moneyStillToPay = moneyOfBooked + moneyOfConsume
						- firstPay;
				moneyTF.setText(String.valueOf(moneyOfBooked));
				consumeTF.setText(String.valueOf(moneyOfConsume));
				preTF.setText(String.valueOf(standard));
				firstPayTF.setText(String.valueOf(firstPay));
				remainedTF.setText(String.valueOf(moneyStillToPay));
				if (row != 0) {
					checkOut.setEnabled(true);
				}
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			} catch (ClassNotFoundException cnfe) {
				cnfe.printStackTrace();
			}
		}
		if (ae.getSource() == checkOut) {
			String nameFirst = (String) nameBox.getSelectedItem();
			if (nameFirst == null) {
				JOptionPane.showMessageDialog(null, "请选择客户名称!", "",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			String nameSecond = resultTable.getModel().getValueAt(0, 0)
					.toString().trim();
			if (!nameFirst.equals(nameSecond)) {
				JOptionPane.showMessageDialog(null, "请您先点击查询按钮!", "",
						JOptionPane.INFORMATION_MESSAGE);
				return;
			}
			for (String id : IDS) {
				String queryS = "update HUseBook set state='否',dateOut='"
						+ todayStr + "' where ID=" + id;
				con.addSql(queryS);
				try {
					con.doDML();
				} catch (SQLException sqle) {
					sqle.printStackTrace();
				} catch (ClassNotFoundException cnfe) {
					cnfe.printStackTrace();
				}
			}
			int row = resultTable.getModel().getRowCount();
			String hno = null;
			for (int count = 0; count < row; count++) {
				hno = (String) resultTable.getModel().getValueAt(count, 2);
				String update = null;
				String today = dateGet.getDate(format.valueConverted(d));
				if (!hno.contains(",")) {
					update = "update House set state='是',beginUseable='"
							+ addDay.addDays(today, 1) + "' where HouseNo='"
							+ hno + "'";
					con.addSql(update);
					try {
						con.doDML();
					} catch (SQLException sqle) {
						sqle.printStackTrace();
					} catch (ClassNotFoundException cnfe) {
						cnfe.printStackTrace();
					}
				} else {
					StringTokenizer st = new StringTokenizer(hno, ",");
					while (st.hasMoreTokens()) {
						String sHNo = st.nextToken();
						if (sHNo != null && !sHNo.equals("")) {
							update = "update House set state='是',beginUseable='"
									+ addDay.addDays(today, 1)
									+ "' where HouseNo='" + sHNo + "'";
							con.addSql(update);
							try {
								con.doDML();
							} catch (SQLException sqle) {
								sqle.printStackTrace();
							} catch (ClassNotFoundException cnfe) {
								cnfe.printStackTrace();
							}
						}
					}
				}
			}
			for (int base = 0; base < row; base++) {
				data[base][8] = "否";
			}
			resultPane.updateUI();
			checkOut.setEnabled(false);
		}
	}
}

⌨️ 快捷键说明

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