Java简单小程序:酒店管理系统

本文介绍了酒店管理系统的两个核心组件:Room类用于表示房间,包括房间编号、类型和状态;Hotel类作为容器,包含动态生成的房间数组并提供订房和退房操作。主测试类演示了用户界面和基本功能流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、房间类:

package 酒店管理系统;

/*
  酒店房间
*/

//只要写了一个类,那么必须写出setter和getter方法并且重写equals和toString方法(无论是否用得上)
//目的是如果不重写equals方法,那么它默认继承Object类,那么比较的就是内存地址,而实际开发中不比较内存地址比较类
public class Room {
    /*
    房间编号
    101 102 103 104...
    201 202 203 204...
    301 302 303 304...
    */
    private int no;
    /*
    房间类型
    标准间 单人间 总统套房
    */
    private String type;
    /*
    房间状态
    true表示空闲,可预订
    false表示占用,不可预订
    */
    private boolean status;

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    //idea对于boolean类型变量生成的getter方法的方法名是isXXX()
    //可以修改为getXXX
    public boolean getStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    public Room(int no, String type, boolean status) {
        this.no = no;
        this.type = type;
        this.status = status;
    }

    //equals方法重写
    //equals用来比较两个对象是否相同
    //至于如何比较,程序员自己决定(比如编号相同决定房间相同,那么写代码比较房间编号就行)
    public boolean equals(Object obj) {
        if(obj == null || !(obj instanceof Room)) return false;
        if(this == obj) return true;
        Room room = (Room) obj;
        //当前房间编号等于传过来的房间对象的房间编号(认为是同一个房间)
        if(this.no == room.getNo()) return true;
        return false;
    }

    //toString方法重写
    //toString方法目的是将Java对象转换成字符串形式
    //怎么转或者转成什么格式由程序员自己决定
    //不看内存地址,而是
    public String toString() {
        return "[" + no + "," + type + "," + (status ? "空闲" : "占用") + "]";
    }
}
//println输出引用时会自动调用引用的toString方法

二、酒店类:

package 酒店管理系统;

/*
 酒店对象,酒店中有二维数组,二维数组模拟大厦
*/

public class Hotel {

    //二维数组模拟所有房间
    private Room[][] rooms;

    //通过构造方法来盖楼
    public Hotel() {
        //一共有几层
        //每层房间类型是什么,编号是什么
        //动态初始化
        rooms = new Room[3][10];

        //创建30个room对象放到数组中
        for (int i = 0; i < rooms.length; i++) {
            for (int j = 0; j < rooms[i].length; j++) {
                if (i == 0) {
                    //一楼
                    rooms[i][j] = new Room((i + 1) * 100 + j + 1, "单人间", true);
                } else if (i == 1) {
                    //二楼
                    rooms[i][j] = new Room((i + 1) * 100 + j + 1, "标准间", true);
                } else if (i == 2) {
                    //三楼
                    rooms[i][j] = new Room((i + 1) * 100 + j + 1, "总统套房", true);
                }
            }
        }
    }

    //在酒店对象上提供一个打印房间列表的方法
    public void print() {
        for (int i = 0; i < rooms.length; i++) {//层
            for (int j = 0; j < rooms[i].length; j++) {//每层房间
                Room room = rooms[i][j];
                System.out.print(room);
            }
            System.out.println();
        }
    }

    /*
      订房方法
      调用此方法需要传递一个房间编号,房间编号由前台服务人员进行输入
    */
    public void order(int roomNo) {
        //将room对象的status修改为false
        //通过房间编号演算出下标,获取房间对象
        Room room = rooms[roomNo / 100 - 1][roomNo % 100 - 1];
        if (room.getStatus() == true) {
            room.setStatus(false);
            System.out.println(roomNo + "已订房成功!");
        } else {
            System.out.println(roomNo + "房间已被占用,不可预订!");
        }
    }

    /*
      退房方法
    */
    public void exit(int roomNo) {
        //把房间对象修改为占用
        Room room = rooms[roomNo / 100 - 1][roomNo % 100 - 1];
        if (room.getStatus() == false) {
            room.setStatus(true);
            System.out.println(roomNo + "已退房成功!");
        } else {
            System.out.println((roomNo + "该房间为空闲,不可退房!"));
        }
    }
}

三、主方法测试类:

package 酒店管理系统;

import java.util.Scanner;

public class HotelManagementSystem {
    public static void main(String[] args) {
        Hotel hotel = new Hotel();
        /*
          首先输出欢迎页面
        */

        System.out.println("欢迎使用酒店管理系统,请认真阅读以下说明");
        System.out.println("请输入对应的功能编号:[1]表示查看房价列表。[2]表示订房。[3]表示退房。[0]表示退出系统。");
        Scanner s = new Scanner(System.in);

        //死循环,一直可以执行使用
        while (true) {
            System.out.println("请出输入功能编号:");
            int i = s.nextInt();
            if (i == 1) {
                hotel.print();
            } else if (i == 2) {
                System.out.println("请输入房间编号:");
                int roomNo = s.nextInt();
                hotel.order(roomNo);
            } else if (i == 3) {
                System.out.println("请输入房间编号:");
                int roomNo = s.nextInt();
                hotel.exit(roomNo);
            } else if (i == 0) {
                System.out.println("再见,欢迎下次光临!");
                return;
            } else {
                System.out.println("输入功能编号无法查询,请重新输入!");
            }
        }
    }
}

四、运行情况:

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值