基于C#+MySQL开发的小型宾馆管理系统设计与实现

问题

本案例实现一个小型的宾馆管理系统,满足一般小型旅馆的日程管理需要。完成的功能:

  1. 客房管理、客户管理、客户入住和客户结算、系统功能。

  2. 客房(房间)管理:对宾馆拥有的房间进行登记,描述房间的详细信息,提供查询、新建、修改和删除等功能。

  3. 客户管理:对宾馆登记的客户进行管理,描述客户的详细信息,提供查询、新建、修改和删除等功能。
    3.客户入住:当顾客要求入住宾馆时,客户指定要入住的房间类型和其他要求,就可以对客房的入住情况进行查询,并展示房间的信息。选择符合自己要求的客房,便可办理入住手续。

  4. 客户结算:当入住的客户要求退房时,提供指定的房间号或用户名,用户可进行结算退房手续。

  5. 对入住的个人或者历史所有入住信息的查询。

  6. 登录系统,以及帐号密码修改功能。

分析

根据系统的功能划分和实现的方便,我们将整个程序分为三层:数据层、业务层和界面层。数据层负责对数据库进行操作,包括向表中插入数据、更新数据、查询数据和删除数据。业务层负责把界面上的各种操作传达给数据层。界面层主要负责界面的设计,包括控件的位置和外观等设置、实时反映系统的变化情况。

数据库表的设计

根据程序的功能得出数据实体,进行数据库设计分析,确定以下数据表,如图所示:

图1 客房表结构

图2 住客表结构

图3 入住表结构

图4 用户表结构

方案实现:

在解决方案中添加3个项目,分别代表数据层、业务层和界面层。因为房间和住户对象的实现方式类似,以下以客房类为示例,讲解实现过程:

HotelManagementBLL. Room类的实现

该类主要实现客房的业务逻辑,如:添加、删除、修改和查找客房。具体代码如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;


namespace HotelManagementBLL
{
    public class Room
    {
        private DataGridView _dgv;

        public Room(DataGridView dgr)
        {
            _dgv = dgr;
        }

        public Room()
        {

        } 

        public void UpdateDataGrid()
        {
            DataSet objDataSet = HotelManagementDAL.Room.FillByAllRoom(); 
            this._dgv.DataSource = objDataSet.Tables[0];
        }

        //添加房间
        public void Add(string name, decimal price, string kind, string remark)
        {
            HotelManagementDAL.Room obj = new HotelManagementDAL.Room();
            if (obj.GetDataReaderByName(name))
            {
                MessageBox.Show("该房间名已经存在" + name + ",请重新输入!", "课程已存在", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if (HotelManagementDAL.Room.Add(name, price, kind, remark) > 0)
                { 
                    UpdateDataGrid();
                    MessageBox.Show("成功添加房间信息!", "操作成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                { 
                    MessageBox.Show("添加房间信息失败!", "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }


        //修改房间信息
        public void Update(int id, string name, decimal price, string kind, string state, string remark)
        { 
            HotelManagementDAL.Room objRoom = new HotelManagementDAL.Room(name);
            objRoom.RoomID = id;
            objRoom.RoomName = name;
            objRoom.RoomPrice = price;
            objRoom.RoomKind = kind;
            objRoom.RoomState = state;
            objRoom.Remark = remark;
            if (objRoom.Update() > 0)
            {
                UpdateDataGrid();
                MessageBox.Show("修改房间信息成功!", "操作成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("更新操作失败!", "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        
        //删除房间
        public void Delete()
        {
            string roomName = this._dgv.CurrentRow.Cells["roomName"].Value.ToString();
            if (DialogResult.Yes == MessageBox.Show("确认删除房间:' " + roomName + " '吗?\n删除房间,将同时删除与该房间有相关的所有房间信息,要继续吗?", "删除确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                HotelManagementDAL.Room objRoom = new HotelManagementDAL.Room(roomName);
                if (objRoom.Delete() > 0)
                {
                    UpdateDataGrid();
                    MessageBox.Show("成功删除房间" + roomName + " '!", "操作成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("删除操作失败 " + roomName + " '!", "操作失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        //房间查询
        public void Search(string roomName,decimal roomPrice,decimal roomPrice2, string roomKind,string state, bool useMatch)
        { 
            DataSet objDataSet = HotelManagementDAL.Room.FillByAllRoom();
            if (useMatch)
                objDataSet = HotelManagementDAL.Room.SearchByFuzzy(roomName, roomPrice,roomPrice2, roomKind, state);
            else
                objDataSet = HotelManagementDAL.Room.SearchByPrecision(roomName, roomPrice,roomPrice2, roomKind, state);
            _dgv.DataSource = objDataSet.Tables[0];
            if(objDataSet.Tables[0].Rows.Count == 0)
                MessageBox.Show("没有符合的记录!", "结果", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show("搜索完毕", "结果", MessageBoxButtons.OK, MessageBoxIcon.Information);              
        } 
    }
}

HotelManagementDAL. Room类的实现

该类主要实现客房的对象的创建和数据库操作,如根据客房名查询是否有该客房存在、获得所有客房信息记录、以及对客房的添加、删除、修改和查、以及办理住退房的状态修改过操作。具体代码如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;

namespace HotelManagementDAL
{
    public class Room
    {
        private int _roomID;
        private string _roomName;  
        private string _roomKind;
        private decimal _roomPrice; 
        private string _remark;
        private string _roomState;

        #region attribute
         public int RoomID
        {
            get
            {
                return _roomID;
            }
            set
            {
                _roomID = value;
            }
        }

        public string RoomName
        {
            get
            {
                return _roomName;
            }
            set
            {
                _roomName = value;
            }
        }

        public string RoomState
        {
            get
            {
                return _roomState;
            }
            set
            {
                _roomState = value;
            }
        }

        public string RoomKind
        {
            get
            {
                return _roomKind;
            }
            set
            {
                _roomKind = value;
            }
        }

        public decimal RoomPrice
        {
            get
            {
                return _roomPrice;
            }
            set
            {
                _roomPrice = value;
            }
        }

        public string Remark
        {
            get
            {
                return _remark;
            }
            set
            {
                _remark = value;
            }
        }
        #endregion

        public Room()
        { 

        }

        public Room(string roomName)
        {
            _roomName = roomName; 
        }

        //有无该房间
        public bool GetDataReaderByName(string _roomName)
        {
            string strSql = "select * from room_management where ROOM_NAME = @roomName"; 
            return MySqlHelper.ExecuteReader(strSql, new MySqlParameter("@roomName", _roomName)).HasRows;
        }

        //添加房间信息
        public static int Add(string _name, decimal _price, string _kind, string _remark)
        {
            string strSql = "insert into room_management(ROOM_NAME,PRICE,KIND,REMARK,STATE) values(@roomName,@roomPrice,@roomKind,@roomRemark,'未入住')";
            MySqlParameter[] paras = new MySqlParameter[4];
            paras[0] = new MySqlParameter("@roomName", _name);
            paras[1] = new MySqlParameter("@roomPrice", _price);
            paras[2] = new MySqlParameter("@roomKind", _kind);
            paras[3] = new MySqlParameter("@roomRemark", _remark);
            return MySqlHelper.ExecuteNonQuery(strSql, paras);
        }

        //修改房间信息
        public int Update()
        {
            string strSql = "update room_management set ROOM_NAME = @roomName, PRICE = @roomPrice, KIND = @roomKind ,STATE = @roomState ,REMARK = @roomRemark  where ROOM_ID = @roomID";
            MySqlParameter[] paras = new MySqlParameter[6];
            paras[0] = new MySqlParameter("@roomName", _roomName);
            paras[1] = new MySqlParameter("@roomPrice", _roomPrice);
            paras[2] = new MySqlParameter("@roomKind", _roomKind);
            paras[3] = new MySqlParameter("@roomRemark", _remark);
            paras[4] = new MySqlParameter("@roomID", _roomID);
            paras[5] = new MySqlParameter("@roomState", _roomState);
            return MySqlHelper.ExecuteNonQuery(strSql, paras);
        }

        //办理入住
        public int CheckIn()
        {
            string strSql = "update room_management set STATE = '入住'  where ROOM_NAME = @roomName";
            MySqlParameter[] paras = new MySqlParameter[1];
            paras[0] = new MySqlParameter("@roomName", _roomName);
            return MySqlHelper.ExecuteNonQuery(strSql, paras);
        } 

        //办理退房
        public int CheckOut()
        {
            string strSql = "update room_management set STATE = '未入住'  where ROOM_NAME = @roomName";
            MySqlParameter[] paras = new MySqlParameter[1];
            paras[0] = new MySqlParameter("@roomName", _roomName);
            return MySqlHelper.ExecuteNonQuery(strSql, paras);
        }

        //按信息模糊查找
        public static DataSet SearchByFuzzy(string roomName, decimal roomPrice,decimal roomPrice2, string roomKind, string roomState)
        {
            string strSql = "SELECT * FROM room_management where ROOM_NAME LIKE @roomName and  PRICE  BETWEEN  @roomPrice AND @roomPrice2 and KIND LIKE @roomKind and STATE LIKE @roomState";
            MySqlParameter[] paras = new MySqlParameter[5];
            paras[0] = new MySqlParameter("@roomName",  '%' + roomName + '%' );
            paras[1] = new MySqlParameter("@roomPrice", roomPrice);
            paras[2] = new MySqlParameter("@roomKind", '%' + roomKind + '%');
            if (roomState == "")
                roomState = "%";
            paras[3] = new MySqlParameter("@roomState",  roomState );
            paras[4] = new MySqlParameter("@roomPrice2", roomPrice2);
            DataSet ds = MySqlHelper.GetDataSet(strSql,paras); 
            return ds; 
        } 

        //按信息精确查找
        public static DataSet SearchByPrecision(string roomName, decimal roomPrice, decimal roomPrice2, string roomKind, string roomState)
        {
            StringBuilder strSql = new StringBuilder("SELECT * FROM room_management where PRICE  BETWEEN  @roomPrice AND @roomPrice2 and");
            MySqlParameter[] paras = new MySqlParameter[5]; 
            if (roomName == "")
                strSql.Append(" ROOM_NAME LIKE '%' ");
            else
                strSql.Append(" ROOM_NAME = @roomName ");
            if (roomKind == "")
                strSql.Append(" AND KIND LIKE '%' ");
            else
                strSql.Append(" AND KIND = @roomKind ");
            if (roomState == "")
                strSql.Append(" AND STATE LIKE '%' ");
            else
                strSql.Append(" AND STATE = @roomState ");
            paras[0] = new MySqlParameter("@roomName", roomName);
            paras[1] = new MySqlParameter("@roomPrice", roomPrice);
            paras[2] = new MySqlParameter("@roomKind", roomKind);
            if (roomState == "")
                roomState = "%";
            paras[3] = new MySqlParameter("@roomState", roomState);
            paras[4] = new MySqlParameter("@roomPrice2", roomPrice2); 
            DataSet ds = MySqlHelper.GetDataSet(strSql.ToString(), paras);
            return ds; 
        } 

        //删除房间
        public int Delete()
        {
            //删除房间信息 
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from room_management where  ROOM_NAME = @roomNAME ;");
            //删除 入住信息
            strSql.Append("delete from check_in where ROOM_NAME = @roomNAME;");
            return MySqlHelper.ExecuteNonQuery(strSql.ToString(), new MySqlParameter("@roomNAME", _roomName));
        }
        

        //得到所有房间信息
        public static DataSet FillByAllRoom()
        {
            string strSql = "select * from room_management"; 
            DataSet ds = MySqlHelper.GetDataSet(strSql);
            return ds;
        }
        
    }
}

界面设计:

系统的界面除去主运行界面外,还有客房、住客等对象相应的管理、添加、查找和修改等界面,以及入住和退房的界面,具体的界面如下图所示:

​ 图5 登录界面

​ 图6 主界面

​ 图7 住房管理界面

​ 图7 客房录入界面

​ 图8 客房信息修改界面

​ 图9 客房信息查询界面

​ 图9 住客管理界面

​ 图10 住客信息录入界面

​ 图11 住客信息修改界面

​ 图11 住客信息查询界面

​ 图13 入住管理界面

​ 图14 退房管理界面

​ 图15 住房记录界面

​ 图16 密码修改界面

猜你喜欢

转载自blog.csdn.net/newlw/article/details/124992825