H5W3
当前位置:H5W3 > 其他技术问题 > 正文

java酒店管理系统

实体类

一共3个实体类,每个实体类有各自的属性和get,set方法,没什么好说的
管理员实体类:
/*    管理员实体 */package cn.edu.shengda;/** * * @author Administrator */public class Admin {    private String id;                 //编号    private String name;           //姓名    private String password;      //密码    private String extend;         //权限    void setID(String id) {        this.id=id;    }    void setName(String name) {        this.name=name;    }    void setPassword(String password) {        this.password=password;    }    void setExtend(String extend) {        this.extend=extend;    }    String getID() {        return this.id;    }    String getName() {        return this.name;    }    String getPassword() {        return this.password;    }    String getExtend() {        return this.extend;    }    }

客户实体类:

/*    客户实体 */package cn.edu.shengda;import java.sql.Date;/** * * @author Administrator */public class Customer {    private int id;           //编号    private String name;     //姓名    private String card;     //身份证    private int room;         //房间号    private Float money;      //押金    private Date time;        //入住时间    private String sex;       //性别    private String hometown;  //籍贯    int getId() {        return this.id;    }    String getName() {        return this.name;    }    String getCard() {        return this.card;    }    int getRoom() {        return this.room;    }    Float getMoney() {        return this.money;    }    Date getTime() {        return this.time;    }    String getSex() {        return this.sex;    }    String getHometown() {        return this.hometown;    }}

客房实体:

/*    客房实体 */package cn.edu.shengda;/** * * @author Administrator */public class Room {    private int id;               //编号    private String grade ;      //级别    private float price;        //价格    private int state;           //状态    private String remarks;    //备注        void setID(int ID) {    	this.id = ID;    }        void setGrade(String grade) {    	this.grade = grade;    }        void setPrice(float price) {    	this.price = price;    }        void setState(int state) {    	this.state = state;    }        void setRemarks(String remarks) {    	this.remarks = remarks;    }        int getID() {    	return this.id;    }        String getGrade() {    	return this.grade;    }        float getPrice() {    	return this.price;    }        int getState() {    	return this.state;    }        String getRemarks() {    	return this.remarks;    }}

图形化界面及逻辑实现

登录界面:

包含两个文本框,分别是用户名和密码

两个按钮,登录和注册

点击登录,将会判断用户名和密码是否正确,然后提示不同的结果

注册页面

点击注册,将会跳转到注册页面

点击注册后也会有相应的提示

判断注册是否符合规则

import java.sql.PreparedStatement;import java.sql.SQLException;import javax.swing.JOptionPane;public class Register {    String name;    String ID;    String password;    String confirmpassword;        void setName(String name) {        this.name = name;    }    void setID(String ID) {        this.ID = ID;    }    void setPassword(String password) {        this.password = password;    }    void setconfirmpasswd(String confirmpassword) {        this.confirmpassword = confirmpassword;    }        private PreparedStatement getPreparedStatement(String sql) throws SQLException {		return new DBConnecter().getConnection().prepareStatement(sql);	}        //判断注册的账号是否符合规则    boolean JudgeRegister() throws SQLException {                if(this.name.equals("")) {            JOptionPane.showMessageDialog(null, "用户名不能为空!",
                     "用户名", JOptionPane.ERROR_MESSAGE);            return false;        }                if(this.ID.equals("")) {            JOptionPane.showMessageDialog(null, "账号不能为空!", 
                        "账号为空", JOptionPane.ERROR_MESSAGE);            return false;        }                if(this.password.equals("")) {            JOptionPane.showMessageDialog(null, "密码不能为空!", 
                        "密码为空", JOptionPane.ERROR_MESSAGE);            return false;        }                if(!this.password.equals(this.confirmpassword)) {            JOptionPane.showMessageDialog(null, "两次输入的密码不一致!",
                         "密码不一致", JOptionPane.ERROR_MESSAGE);            return false;        }                AdminManager am = new AdminManager();        if(am.JudgeAdminID(this.ID)) {        	 JOptionPane.showMessageDialog(null, "账号已存在", 
                        "账号已存在", JOptionPane.ERROR_MESSAGE);             return false;        }        //符合规则,弹出注册成功的窗口,并将账号添加数据库        JOptionPane.showMessageDialog(null, "注册成功");        addAdmin();        return true;    }        //向数据库添加Admin账户    void addAdmin() throws SQLException {    	String sql="insert into admin (id, name, password) values (?,?,?)";        PreparedStatement ps = getPreparedStatement(sql);        ps.setString(1, this.ID);        ps.setString(2, this.name);        ps.setString(3, this.password);        ps.executeUpdate();        ps.close();    }}

注册成功后就可以点击登录了

主页面:

登录成功后来到主页面

主页面下面的表格,我先把数据库中的房间数据读出,然后转换成ArrayList,最后放到表格中

class MyDefaultTableModel extends DefaultTableModel {				public MyDefaultTableModel(Object[][] data, Object[] columnNames) {			super(data, columnNames);		}				public boolean isCellEditable(int rowIndex, int columnIndex) {						return false;		}	}		private DefaultTableModel toDefaultTableModel(ArrayList<Room> al) {		int row = al.size();		Object [][] o = new Object[row+1][5];		Room room = null;		o[0][0]="房间号";		o[0][1]="级别";		o[0][2]="价格";		o[0][3]="状态";		o[0][4]="备注";		for(int i=0; i<row; i++) {			room = al.get(i);			for(int j=0; j<5; j++) {				if(j==0) {					o[i+1][j] = Integer.valueOf(room.getID());				} else if(j==1) {					o[i+1][j] = room.getGrade();				} else if(j==2) {					o[i+1][j] = Float.valueOf(room.getPrice());				} else if(j==3) {					o[i+1][j] = room.getState()==1 ? "有人" 
                                                        : "空";				} else if(j==4) {					o[i+1][j] = room.getRemarks();				}							}		}		String col[]= {"房间号", "级别", "价格", "状态", "备注"};		return new MyDefaultTableModel(o, col);	}		private ArrayList<Room> toArrayList(ResultSet rs) {		try {			ArrayList<Room> al = new ArrayList<>();			Room room = new Room();			while (rs.next()) {				room = new Room();				room.setID(rs.getInt("id"));				room.setGrade(rs.getString("grade"));				room.setPrice(rs.getFloat("price"));				room.setState(rs.getInt("state"));				room.setRemarks(rs.getString("remarks"));				al.add(room);			}			rs.close();			return al;		} catch(SQLException ex) {			return null;		}	}

系统管理包含两个选项:刷新和退出系统,点击刷新将重新加载表格,点击退出系统则整个程序结束

基本管理有两个选项:住宿登记和退房结账,住宿登记是登记顾客的信息和选择房间

退房结账,输入房间号之后会进行退房。

点击住宿登记会跳出窗口

房间管理有两个选项:增加房间和查询房间

点击添加房间,输入房间号,价格,级别,点击添加,添加成功;点击取消,返回主页面

查询房间,输入房间号,将会显示房间的所有信息

JDBC连接数据库:

import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;/** * * @author Administrator */public class DBConnecter {    /*    	连接数据库    */    private String driver = "com.mysql.cj.jdbc.Driver";    private String url = "jdbc:mysql://localhost:3306/hotelsql?
                                serverTimezone=UTC&characterEncoding=utf-8";    private String user = "root";    private String password = "12481632";        public Connection getConnection() throws SQLException {        try {            Class.forName(driver);        } catch (java.lang.ClassNotFoundException cnfe) {            cnfe.printStackTrace();            System.out.println("驱动失败");        }        Connection conn = null;        //conn=DriverManager.getConnection(URL, "root", "12481632");        try {        	conn = DriverManager.getConnection(url, user, password);        	        }catch(SQLException ex) {        	System.out.println("数据库登录失败!");        }         return conn;    }    /*        */}

算是一个非常简易的酒店管理系统,还有许多需要完善和改进的地方

本文地址:H5W3 » java酒店管理系统

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址