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

由题目我们可分析出,房间信息有五种,因此我们可以定义五个一维数组来储存这些房间信息,

String leixing[] = {"单人间", "双人间", "三人间"};
int louceng[] = {1, 2, 3, 4};
int fangjianhao[] = {101, 102, 201, 202, 301, 302, 401, 402};
int[] jiage= {100, 200, 300};
boolean[] ruzhuzhuangtai = {false, false, false, false, false, false, true, true};


其中房间号和楼层刚好是同类型的int数组,而且也是关联比较密切的元素,我们不妨把他们储存在一个二维数组中。

int arr[][] = {fjh, jg};


再看功能:

1.查看所有房间,将储存了房间信息的数组中的元素遍历出来即可。

2.订房,我的思路是用boolean类型的数组来储存入住状态,当选中房间时,数组里对应的元素变为true,意为已入住,订房成功。

3.原理同订房,只是将true变为false。

4.修改价格,我的思路是价格应该是跟房间类型绑定在一起的,这两个数组的长度也相同,我们输入想要修改的房间类型和价格,并把这两个数组都遍历一遍,找到价格数组里下标相对应的元素,将新的价格赋值给数组里的元素。

5.退出,即

System.exit(0);


接下来写代码

import java.util.*;
public class jiudian {
    public static void main(String[] args) {
        String leix[] = {"单人间", "双人间", "三人间"};
        int lc[] = {1, 2, 3, 4};
        int fjh[] = {101, 102, 201, 202, 301, 302, 401, 402};
        int[] jg = {100, 200, 300};
        boolean[] rzzt = {false, false, false, false, false, false, true, true};
        int arr[][] = {fjh, jg};
        while (true) {//在选择功能部分加入一个死循环,这样每次从某一功能返回时,可以继续选择其他功能
            System.out.println("请输入数字选择功能: 1.查看所有房间  2.订房  3.退房 4.修改价格 5.退出");
            Scanner sc = new Scanner(System.in);
            int a = sc.nextInt();
            switch (a) {//通过switch语句来实现选择菜单的功能
                case 1:
                    cksyfj(arr, rzzt);
                    break;
                case 2:
                    df(fjh, rzzt);
                    break;
                case 3:
                    tf(rzzt, fjh);
                    break;
                case 4:
                    xgjg(leix, jg);
                    break;
                case 5:
                    tc();
                    break;
                default:
                    break;
            }
        }
    }
    public static void cksyfj(int arr[][], boolean rzzt[]) {
        while (true) {
            int count = 0;
            System.out.println("输入数字选择想要查看的楼层: 1   2   3   4");
            Scanner sc = new Scanner(System.in);
            int a = sc.nextInt();
            for (int i = 0; i < arr.length - 1; i++) {//将二维数组遍历
                int arr1[] = arr[i];
                for (int j = 2 * a - 2; j < (2 * a); j++) {//因为每层楼只有两间房而且数组的下标是从0开始,所以我们可以总结出这样的规律
                    System.out.println(arr1[j]);
                    if (!rzzt[j]) {
                        System.out.println("可入住");
                    } else {
                        System.out.println("不可入住");
                    }
                }
            }
            System.out.println("是否查看其他楼层?  按2继续查看 按1停止查看");
            Scanner sc1 = new Scanner(System.in);
            int b = sc.nextInt();
            if (b == 1) {//通过if循环来实现继续或者停止该功能进行,切记该语句要写在for循环之外,才能正确停止整个查看房间的循环
                break;
            } else if (b == 2) {
                continue;
            } else {
                System.out.println("输入错误,请输入1(停止)或者2(继续)");
            }
        }
    }

    public static boolean[] df(int fjh[], boolean rzzt[]) {

        while (true) {
            System.out.println("请输入数字选择房间号:");
            Scanner sc = new Scanner(System.in);
            int f = sc.nextInt();
            int count = 0;
            for (int i = 0; i < fjh.length; i++) {
                count++;
                if (f == fjh[i]) {//房间号的数组与入住状态的数组长度一直,所以通过找到房间号的下标来确定入住状态数组里对应元素的位置
                    System.out.println("找到了");
                    break;
                }
                else{
                    System.out.println("请输入正确的房间号");//要判断输入的房间号是否存在
                }
            }
            if (rzzt[count - 1]) {//判断房间是否已经入住
                System.out.println("已入住,请选择其他房间");
                continue;
            } else {
                for (int j = 0; j < rzzt.length; j++) {
                    if (count == j) {
                        rzzt[j] = true;
                        System.out.println("订房成功");
                        break;
                    }
                }
            }
            System.out.println("是否继续订房,按2继续订房,按1停止");
            Scanner sc1 = new Scanner(System.in);
            int a = sc.nextInt();
            if (a == 1) {
                break;
            } else if (a == 2) {
                continue;
            } else {
                System.out.println("输入错误,请输入1(停止)或者2(继续)");
            }
        }
        return rzzt;
    }


    public static boolean[] tf(boolean rzzt[], int fjh[]) {
        while (true) {
            System.out.println("请输入数字选择房间号:");
            Scanner sc = new Scanner(System.in);
            int f = sc.nextInt();
            int count = 0;
            for (int i = 0; i < fjh.length; i++) {
                count++;
                if (f == fjh[i]) {
                    System.out.println("找到了");
                    break;
                }
                else{
                    System.out.println("请输入正确的房间号:");
                }
            }
            if (!rzzt[count - 1]) {
                System.out.println("已搬出,请选择其他房间");
                continue;
            } else {
                for (int j = 0; j < rzzt.length; j++) {
                    if (count == j) {
                        rzzt[j] = false;
                        System.out.println("退房成功");
                        break;
                    }
                }
            }
            System.out.println("是否继续退房,按2继续退房,按1停止");
            Scanner sc1 = new Scanner(System.in);
            int a = sc.nextInt();
            if (a == 1) {
                break;
            } else if (a == 2) {
                continue;
            } else {
                System.out.println("输入错误,请输入1(停止)或者2(继续)");
            }
        }
        return rzzt;
    }

    public static int[] xgjg(String[] leix, int jg[]) {
        while (true) {
            System.out.println("各类型房间价格为:");
            for (int i = 0; i < leix.length; i++) {
                System.out.println(leix[i] + "  " + jg[i]);//打印出现有的房间类型和价格

            }
            System.out.println("输入想要修改的房间类型和价格");
            Scanner sc = new Scanner(System.in);
            String lx = sc.next();
            int jg1 = sc.nextInt();
            int count = 0;
            for (int i = 0; i < leix.length; i++) {
                count++;
                if (lx.equals(leix[i])) {//输入的当房间类型匹配时,进入下一步
                    break;
                }
            }
            for (int j = 0; j < leix.length; j++) {
                if (count - 1 == j) {
                    jg[j] = jg1;
                }
            }
            System.out.println("是否继续修改价格,按2继续,按1停止");
            Scanner sc1 = new Scanner(System.in);
            int a = sc.nextInt();
            if (a == 1) {
                break;
            } else if (a == 2) {
                continue;
            } else {
                System.out.println("输入错误,请输入1(停止)或者2(继续)");
            }

        }
        return jg;
    }
    public static void tc() {
        System.exit(0);
    }
}