Java面向对象前奏:酒店客房管理系统

需求: 
实现一个简单的酒店客房管理系统,房间信息包含,类型、楼层,房间号,价格,入住状态,它具备5个功能,
分别为【1:查看所有房间功能;2:订房功能;3:退房功能;4:修改价格功能;5:退出本系统功能】

提示:用Java基础知识完成,不能使用面向对象

分析:

如果不用面向对象的话,房间信息的每个属性可以用等长的数组来存储,在主函数中定义5个数组并动态初始化,假设一共有20个房间

将20个房间等分成4份,每5个房间在同一楼层,且房型相同。写入房间信息时可根据数组下标在不同数组中写入信息

将除了退出系统以外所有功能定义成方法,将需要更改或查看的属性作为方法的参数,需要输入信息的还可以将Scanner对象作为参数

系统中要求重新输入指令的地方和实现功能的地方都要用到while语句,保证功能可以重复使用,在输入有误时程序依然有较强的健壮性

package array;

import java.util.Scanner;

public class E {
    /**
     * @description TODO
     * @author Reno
     * @date 2022/7/13 15:35
     * @version 1.0.0
     */
    //主菜单显示方法
    public static void mainMenu() {
        Scanner sc = new Scanner(System.in);
        String[] menu = {"1. 查看全部房间", "2. 订房", "3. 退房", "4. 修改房间价格", "5. 退出系统"};
        for (int i = 0; i < menu.length; i++) {
            System.out.println(menu[i]);
        }
    }

    //显示所有房间信息方法
    public static void displayRooms(int[] roomID, String[] type, short[] floor, double[] price, boolean[] isUsed) {
        System.out.println("房间号|房间类型|楼层|价格|入住状态");
        for (int i = 0; i < roomID.length; i++) {
            System.out.println(roomID[i] + " | " + type[i] + " | " + floor[i] + " | " + price[i] + " | " + isUsed[i]);
        }
    }

    //订房功能方法
    public static void bookRoom(int[] roomID, short[] floor, String[] type, boolean[] isUsed, Scanner sc) {
        int room;
        boolean aviliable = false;
        for (int i = 0; i < roomID.length; i++) {
            if (!isUsed[i]) {
                aviliable = true;
            }
        }
        if (!aviliable) {
            System.out.println("房间已全部订满, 即将返回主菜单");
        } else {
            System.out.println("可选房间如下: ");
            System.out.println("楼层 | 房间号 | 房间类型");
            for (int i = 0; i < roomID.length; i++) {
                if (!isUsed[i]) {
                    System.out.println(floor[i] + " | " + roomID[i] + " | " + type[i]);
                }
            }
            boolean isRightID = false;
            while (!isRightID) {
                System.out.println("请输入房间号选择要预定的房间");
                room = sc.nextInt();


                for (int i = 0; i < roomID.length; i++) {
                    if (room == roomID[i] && !isUsed[i]) {
                        isRightID = true;
                        System.out.println("要预订这个房间吗?\n 1. 确定\n2. 取消并返回主菜单");
                        int n = sc.nextInt();
                        if (n == 1) {
                            isUsed[i] = true;
                            System.out.println("您已成功预订" + roomID[i] + "房间, 祝旅行者入住愉快");
                        } else return;
                        break;
                    }
                }
                if (!isRightID)
                    System.out.println("房间号输入有误, 请重新输入");
            }
        }
    }

    //退房功能方法
    public static void returnRoom(int[] roomID, boolean[] isUsed, Scanner sc) {
        //定义一个变量用于存放房间索引,没找到对应房间按-1处理
        int index = -1;
        while (index == -1) {
            System.out.println("请输入要退的房间号");
            int room = sc.nextInt();
            System.out.println("确定退房吗?\n1. 确定\n2. 取消\n任意键 返回主菜单");
            int ok = sc.nextInt();
            if (ok == 1) {
                for (int i = 0; i < roomID.length; i++) {
                    if (roomID[i] == room && isUsed[i]) {
                        System.out.println("退房中");
                        index = i;
                        isUsed[i] = false;
                        System.out.println(roomID[i] + "房间已退");
                        break;
                    }
                }
                if (index == -1) {
                    System.out.println("房间无人入住, 或者您的房间号输入有误, 请重新输入");
                }
            } else if (ok == 2) {
                System.out.println("退房已取消");
            } else return;
        }
    }

    //改房间价格方法
    public static void alterPrice(short[] floor, double[] price, Scanner sc) {
        System.out.println("请输入指令选择要更改价格的房间类型\n2. 特价房\n3. 家庭房\n4. 双床房\n5. 大床房\n任意键 返回主菜单");
        int select = sc.nextInt();
        double newPrice = 0;
        if (select >= 2 && select <= 5) {
            while (newPrice <= 0) {
                System.out.println("请输入新的价格");
                newPrice = sc.nextDouble();
                if (newPrice <= 0) {
                    System.out.println("您的输入有误, 请重新输入");
                } else break;
            }
            for (int i = 0; i < floor.length; i++) {
                if (floor[i] == select) {
                    price[i] = newPrice;
                }
            }
            System.out.println("房价已更改, 即将返回主菜单");
        }
        for (int i = 0; i < floor.length; i++) {
            if (floor[i] == select) {
                price[i] = newPrice;
            }
        }
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        String[] type = new String[20];
        short[] floor = new short[20];
        int[] roomID = new int[20];
        double[] price = new double[20];
        boolean[] isUsed = new boolean[20];
        System.out.println("欢迎来到甜心酒店, 请根据文字提示进行操作或输入相应数字选择操作");
        for (int i = 0; i < roomID.length; i++) {
            if (i < 5) {
                type[i] = "大床房";
                floor[i] = 5;
                price[i] = 124;
            } else if (i < 10) {
                type[i] = "双床房";
                floor[i] = 4;
                price[i] = 154;
            } else if (i < 15) {
                type[i] = "家庭房";
                floor[i] = 3;
                price[i] = 186;
            } else {
                type[i] = "特价房";
                floor[i] = 2;
                price[i] = 70;
            }
            isUsed[i] = false;
            //房间号是由楼层及每层房间数决定的,如201表示2楼第1个房间
            roomID[i] = floor[i] * 100 + i % 5 + 1;
        }

        int cmd = 0;
        //循环结束条件: 主菜单中输入指令5结束循环
        while (cmd != 5) {
            mainMenu();
            cmd = sc.nextInt();
            switch (cmd) {
                case 1:
                    displayRooms(roomID, type, floor, price, isUsed);
                    break;
                case 2:
                    bookRoom(roomID, floor, type, isUsed, sc);
                    break;
                case 3:
                    returnRoom(roomID, isUsed, sc);
                    break;
                case 4:
                    alterPrice(floor, price, sc);
                    break;
                case 5:
                    System.out.println("Bye");
                    break;
                default:
                    System.out.println("您的输入有误, 请输入正确指令");
            }
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值