Java实现酒店管理系统部分功能

要用Java实现酒店管理系统的部分功能:查房,订房,退房等功能,先来分析有酒店对象、房间对象,编写酒店类,房间类,再有一个酒店系统测试类。可以把酒店想象为容器,而房间是二维数组一样的存在(如下图)。在这里插入图片描述

  • 房间类
    编写房间类,确定属性:房间编号、房间类型、房间状态,重写toString()、equals()方法。
import java.util.Objects;

public class Room {
    
    
    private int num;
    private String type;
    private boolean statue;


    public Room(int num, String type, boolean statue) {
    
    
        this.num = num;
        this.type = type;
        this.statue = statue;

    }

    public int getNum() {
    
    
        return num;
    }

    public void setNum(int num) {
    
    
        this.num = num;
    }

    public String getType() {
    
    
        return type;
    }

    public void setType(String type) {
    
    
        this.type = type;
    }
    public boolean isStatue() {
    
    
        return statue;
    }

    public void setStatue(boolean statue) {
    
    
        this.statue = statue;
    }


    @Override
    public String toString() {
    
    
        return "[房间号:"+num+"  房间类型:"+type+" 房间状态:"+statue+"]";
    }

    @Override
    public boolean equals(Object o) {
    
    
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Room room = (Room) o;
        return num == room.num && statue == room.statue && Objects.equals(type, room.type);
    }
}
  • 酒店类
    二维数组模拟酒店房间,for循环遍历创建房间对象,接着写查房方法、订房方法和退房方法。
public class Hotel {
    
    
    //酒店对象 二维数组模拟酒店房间
    private Room[][] rooms;
    //构造方法造房间
    public Hotel() {
    
    
        rooms = new Room[3][5];  //三层楼每层十个房间
        //for循环遍历,创建30个Room对象
        for (int i = 0; i < rooms.length; i++) {
    
    
            for (int j = 0; j < rooms[i].length; j++) {
    
    
              //  new Room((i+1)*100+(j+1),"房间类型",true);
                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 roomNum){
    
    
        //订房最主要的是将房间 对象的状态由true修改为false,代表房间占用
        //通过房间编号获取数组下标。获取房间对象
        rooms[roomNum / 100 - 1][roomNum % 100 - 1].setStatue(false);
        System.out.println(roomNum+"已订房");
    }

    //退房;将房间状态改为true,代表房间空闲
    public void exit(int roomNum){
    
    
        //订房最主要的是将房间 对象的状态由true修改为false
        //通过房间编号获取数组下标。获取房间对象
        rooms[roomNum / 100 - 1][roomNum % 100 - 1].setStatue(true);
        System.out.println(roomNum+"已退房");
    }
}
  • 酒店系统测试类
    编写服务菜单。
import java.util.Scanner;

public class HotelSystem {
    
    
    public static void main(String[] args) {
    
    
        Hotel hotel = new Hotel();
        System.out.println("欢迎使用酒店管理系统,请认真阅读使用说明");
        System.out.println("请输入对应功能编号:[1]表示查看房间列表 [2]表示订房 [3]表示退房 [0]表示退出");
        Scanner scanner = new Scanner(System.in);
        //循环使用
        while (true) {
    
    
            System.out.println("请输入功能编号:");
            int i = scanner.nextInt();
            if (i == 1) {
    
    
                //查看房间列表
                hotel.print();
            } else if (i == 2) {
    
    
                //订房
                System.out.println("请输入订房编号:");
                int roomNum = scanner.nextInt();//输入房间编号
                hotel.order(roomNum);
            } else if (i == 3) {
    
    
                //退房
                System.out.println("请输入退房编号:");
                int roomNum = scanner.nextInt();//输入房间编号
                hotel.exit(roomNum);
            } else if (i == 0) {
    
    
                //退出系统
                System.out.println("已退出系统...");
            }
        }
    }
}

这是不成熟的一个系统,有很大的漏洞,信息无法保存,在后续与数据库建立连接后再完善。
结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_52822058/article/details/126473768