酒店订房系统模拟,二维数组、while循环、双重for循环、if语句的组合运算

package hotel;

import java.util.Scanner;

/**
* 酒店订房系统
* 房间号码:1001、1002、1003、1004、1005、1006、1007、1008、1009
* 入住状态:
* 楼层:9层
* 分析需求:房间号对应入住状态,需要遍历for循环,由于是矩形9*9楼层房间,则需要双重数组进行遍历
* @author 74599
*
*/
public class CalssHotel {
  //定义酒店的楼层
  static int row = 9;
  //定义每层楼房间数
  static int col = 9;
  //定义二维数组用来存放房间的入住状态,没人入住就显示empty,因为empty是字符串所以需要创建一个字符串的数组出来
  static String[][] rooms = new String[row][col];
  //存放每个房间入住人的名字,界面看到的名字已经赋值为****后台需要一个数组变量来对名字进行单独存放
  static String[][] names = new String[row][col];
  //键盘输入指令
  static Scanner key = new Scanner(System.in);


  public static void main(String[] args) {
    //初始化酒店的房间,数组类型定义的是String字符串,字符串数组元素输出默认都为null
    for(int i = 0;i < rooms.length;i++) {
      for(int j = 0;j < rooms[i].length;j++) {
        //将所有null的房间都变成empty进行展示
        rooms[i][j] = "empty";
      }
    }
    while(true) {
      //调用引导方法
      help();
      //接收输入指令
      String con = key.next();
      //判断用户输入的字符串,使用if或者switch都可以,由于是比对字符串所以需要使用equals方法
      if(con.equals("search")) {
        //查询房间的入住状态
        searchRoom();
      }else if(con.equals("in")) {
        //预定房间
        inTheRoom();
      }
    }
  }

  /**
  * 查询房间的入住状态,判断是否为空
  * 查询所有房间,即需要用到遍历双重for循环
  */
  public static void searchRoom() {
    for(int i = 0;i<rooms.length;i++) {
      //输入所有房间号码,房间号码可以使用循环数组角标进行生成
      for(int j = 0;j<rooms[i].length;j++) {
        //角标是从0开始计算,所以不管是行还是列都需要+1从1开始计算,要求每层房间号(列)要从1001、2001、3001开始,所以i需要+1之后要再乘以1000
        System.out.print((i+1)*1000+(j+1)+"\t");
      }
      //换行分割所有房间号码和状态
      System.out.println();
      //输出所有房间入住的状态
      for(int j = 0;j<rooms[i].length;j++) {
        System.out.print(rooms[i][j]+"\t");
      }
      //换行输出房间状态
      System.out.println();
      //每层房间分隔符
      System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    }
  }

  /**
  * 预定房间的方法
  */
  public static void inTheRoom() {
    System.out.println("输入你需要预定的房间:");
    //接收输入的房间号码,号码是数字所以用nextInt();
    int num = key.nextInt();
    //假设输入1001号房间
    //判断该房间是否有人入住,状态是否为empty
    //假设用户输入的房间号为1001,那么就需要对1001进行拆分获取在数组中的位置,即1001除以1000取整-1就得到楼层数,1001除以1000取余-1就得到房间数
    int row = num/1000-1;
    int col = num%1000-1;
    //如果房间号无人入住
    if(rooms[row][col].equals("empty")) {
      //提示用户输入姓名
      System.out.println("输入你的姓名:");
      //接收输入的订房姓名,姓名是字符串所以用next();
      String name = key.next();
      //将房间注入状态empty更改为输入的姓名,姓名不允许直接查看,所以将输入的值变更为****进行显示
      rooms[row][col] = "****";
      //将输入的名字放到names数组中进行储存
      names[row][col] = name;
      System.out.println(name+"成功预定"+num+"号房间!");
    }else {
    System.out.println("已有人!");
    }
  }

  /**
  * 系统引导帮助界面
  */
  public static void help() {
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    System.out.println("~~~~~~~wellcome~~~~~~~~~~~~~");
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    System.out.println("~~~~~~~~请选择操作!!!~~~~~~~~");
    System.out.println("~~~~~输入search查询入住状态~~~~~");
    System.out.println("~~~~~输入in预定房间~~~~~~~~~~~~");
    System.out.println("~~~~~输入out退房~~~~~~~~~~~~~~");
    System.out.println("~~~~~输入exit退出系统~~~~~~~~~~");
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    System.out.println("~~~~~~请输入指令~~~~~~~~~~~~~~~");
  }
}

猜你喜欢

转载自www.cnblogs.com/yomai/p/12416338.html