Home  >  Article  >  Java  >  How to implement a hotel management system using Java code

How to implement a hotel management system using Java code

WBOY
WBOYforward
2023-05-01 11:19:061594browse

1. Requirements Analysis

If we want to implement a hotel management system, we need to first analyze the business requirements of the hotel management system:

1. What functions does the hotel management system need to implement?

(1) Enter a command to query all rooms in the hotel;
(2) Enter the number of a certain room to make a reservation;
(3) Enter the number of a certain room to check out;
(4) Enter a certain command to exit the hotel management system;

2. What data structure does the hotel management system use to represent rooms?

(1) The hotel management system uses arrays to store rooms. Here we use a two-dimensional array to represent them;

3. What attributes do we have for hotel rooms?

(1) Room number;
(2) Room type;
(3) Whether the room is free;

4. What do we use to control the room class with the above attributes? ?

(1) We can use custom annotations to achieve this;

2. Drawing analysis

How to implement a hotel management system using Java code

3. Code structure

How to implement a hotel management system using Java code

4. Code implementation

/**
    自定义注解类:该类作用在房间类上。
*/
@Retention(RUNTIME)        // 用该注解表示此自定义注解可以被外部映射到
@Target(TYPE)            // 用该注解表示此自定义注解只可以作用在类上
public @interface ProperyAnnotation {}
/**
    当自定义注解类作用在房间实体类上时,该类是用来判断房间实体类上是否有:房间编号、房间类型、房间是否空闲这些属性的。
*/
public class ProperyAnnotationToJudge {

    // 判断ProperyAnnotation注解的某个类中是否有某些属性的方法
    public static void Judge () {
        try {
            // 使用反射机制来反射Room类
            // 我这里的房间Room类放到了test包下,您可以自定义它的路径。
            Class<?> c = Class.forName("test.Room");
            // 判断Room类上是否存在@ProperyAnnotation注解
            if (c.isAnnotationPresent(ProperyAnnotation.class)) {
                // 如果Room类上存在@ProperyAnnotation注解就获取这个Room类中的全部属性
                Field[] fields = c.getFields();
                boolean isExist = false;
                for (Field field : fields) {
                    // 如果Room类中存在房间编号、房间类型、房间是否空闲这些属性,就让isExist=true,否则抛出异常
                    if (item.getName().equals("rId") && item.getType().getSimpleName().equals("Integer")) {
                        for (Field item1 : fields) {
                            if (item1.getName().equals("rType") && item1.getType().getSimpleName().equals("String")) {
                                for (Field item2 : fields) {
                                    if (item2.getName().equals("rFree") && item2.getType().getSimpleName().equals("Boolean")) {
                                        isExist = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                if (!isExist) {
                    throw new ProperyException("Room类中不存在房间编号、房间类型、房间是否空闲这些属性");
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
/**
    当ProperyAnnotation注解作用的房间实体类中没有房间编号、房间类型、房间是否空闲这些属性时,此类为抛出的异常类。
*/
public class ProperyException extends RuntimeException {
    private static final long serialVersionUID = -8343113740914228496L;
    public ProperyException () {}
    public ProperyException (String msg) {super(msg);}
}
/**
    Room房间实体类
*/
@ProperyAnnotation
public class Room {
    private Integer rId;    // 房间编号
    private String rType;    // 房间类型
    private Boolean rFree;    // 房间是否空闲
    public Room () {}
    public Room (Integer rId, String rType, Boolean rFree) {
        this.rId = rId;
        this.rType = rType;
        this.rFree = rFree;
    }
    protected Integer getrId() {
        return rId;
    }
    protected void setrId(Integer rId) {
        this.rId = rId;
    }
    protected String getrType() {
        return rType;
    }
    protected void setrType(String rType) {
        this.rType = rType;
    }
    protected Boolean getrFree() {
        return rFree;
    }
    protected void setrFree(Boolean rFree) {
        this.rFree = rFree;
    }
    @Override
    public String toString() {
        return "Room [" + rId + ", " + rType + ", "+ (rFree ? "有人入住" : "无人入住")+"]";
    }
}
/**
    酒店管理实体类:用来管理房间的,其中包括查看所有房间状态、订房、退房功能。
*/
public class Hotel {

    // 这里需要定义一个二维数组来表示房间,因为我们设想的酒店有很多层,且每层有很多你发件。
    private static Room[][] rooms;
    
    public Hotel () {
        // 这里定义酒店为5层,且每层有9个房间
        rooms = new Room[5][9];
        // 这里我们来设置酒店的房间,由于酒店的房间很多,所以我们使用for循环来分别设置每个楼层。
        for (int m = 0 ; m < rooms.length ; m ++) {
            for (int n = 0 ; n < rooms[m].length ; n ++) {
                // 第一层
                if (m == 0) {
                    /*
                        这里我们的房间编号这样设置:
                        如果是是酒店的第一层楼的第一个房间,我们将房间编号设置成:101
                        我规定我们的酒店的楼层为1~5层;
                        我规定我们的酒店的第一个房间为1
                        所以如果我们用二维数组来表示酒店的楼层和第几个房间时,因为我们的二维数组的横纵坐标都是从0开始的,所以我们需要分别加上1,此时房间编号的表达式就为:
                        (m + 1) * 100 + n + 1
                        当m = 0时:
                            n = 0:房间编号为101;
                            n = 1:房间编号为102;
                            n = 2;房间编号为103;
                            ...
                        当m = 1时:
                            n = 0:房间编号为201;
                            n = 1:房间编号为202;
                            ...
                        ...
                    */
                    rooms[m][n] = new Room((m + 1) * 100 + n + 1, "单人豪华房", false);
                }
                
                // 第二层
                if (m == 1) {
                    rooms[m][n] = new Room((m + 1) * 100 + n + 1, "双人豪华房", false);
                }
                
                // 第三层
                if (m == 2) {
                    rooms[m][n] = new Room((m + 1) * 100 + n + 1, "三人豪华房", false);
                }
                
                // 第四层
                if (m == 3) {
                    rooms[m][n] = new Room((m + 1) * 100 + n + 1, "三人豪华房", false);
                }
                
                // 第五层
                if (m == 4) {
                    rooms[m][n] = new Room((m + 1) * 100 + n + 1, "三人豪华房", false);
                }
            }
        }
    }
    
    // 查看所有房间状态
    public void queryAllRooms () {
        for (int m = 0 ; m < rooms.length ; m ++) {
            for (int n = 0 ; n < rooms[m].length ; n ++) {
                System.out.println(rooms[m][n].toString());
            }
        }
    }
    
    // 使用房间编号订房
    public void makeRoom (int rId) {
        Room room = rooms[rId / 100 - 1][rId % 100 - 1];
        // 如果该编号的房间已经有人订了
        if (room.getrFree() == true) {
            System.out.println("抱歉,请您订购其他房间,此房间已经有人居住!");
        } else {
            room.setrFree(true);
            System.out.println("订房完成");
        }
    }
    
    // 使用房间编号退房
    public void existRoom (int rId) {
        Room room = rooms[rId / 100 - 1][rId % 100 - 1];
        // 如果该编号的房间本来就没有人居住
        if (room.getrFree() == false) {
            System.out.println("抱歉,请您退订其他房间,该房间没有人居住不需要退订!");
        } else {
            room.setrFree(false);
            System.out.println("退房完成");
        }
    }
}
/**
    酒店的操作测试类:
*/
public class Test {
    public static void main (String[] args) {
        ProperyAnnotationToJudge.Judge();
        Hotel hotel = new Hotel();
        System.out.println("欢迎使用酒店管理系统,请认真阅读以下使用说明:");
        System.out.println("请输入对应的功能编号:[1]查看房间列表; [2]订房; [3]退房; [0]退出系统");
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("请输入功能编号:");
            Integer i = scanner.nextInt();
            if (i == 1) {
                hotel.queryAllRooms();
                System.out.println("酒店所有的房间已经加载完毕!");
            }
            else if (i == 2) {
                System.out.print("请输入房间编号,房间编号为101~110、201~210、301~310、401~410、501~510:");
                Integer rId = scanner.nextInt();
                if (rId >= 101 && rId <= 110 || rId >= 201 && rId <= 210 || rId >= 301 && rId <= 310 || rId >= 401 && rId <= 410 || rId >= 501 && rId <= 510) {
                    hotel.makeRoom(rId);
                } else {
                    System.out.println("请输入正确的房间编号!");
                }
            }
            else if (i == 3) {
                System.out.print("请输入房间编号,房间编号为101~110、201~210、301~310、401~410、501~510:");
                Integer rId = scanner.nextInt();
                if (rId >= 101 && rId <= 110 || rId >= 201 && rId <= 210 || rId >= 301 && rId <= 310 || rId >= 401 && rId <= 410 || rId >= 501 && rId <= 510) {
                    hotel.existRoom(rId);
                } else {
                    System.out.println("请输入正确的房间编号!");
                }
                
            }
            else if (i == 0) {
                System.out.println("成功退出酒店管理系统!");
                scanner.close();
                return;
            }
            else {
                System.out.println("请仔细阅读使用说明,输入正确的功能编号");
            }
        }
    }
}

Output result:

How to implement a hotel management system using Java code

The above is the detailed content of How to implement a hotel management system using Java code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete