【日常练习题】二维数组模拟简单酒店管理系统

【日常练习题】二维数组模拟简单酒店管理系统—java


酒店管理系统:

为某酒店编写程序:酒店管理系统,模拟订房、退房、打印所有房间状态等功能。
1、该系统的用户是:酒店前台
2、酒店使用一个二维数组来模拟。“Room[][] rooms;”
3、酒店中得每个房间应该是一个java对象:Room
4、每个房间Room应该有:房间号、房间类型、房间是否空闲
5、系统应该对外提供得功能
预定房间:用户输入房间号,订房
退房:用户输入房间号,退房
查看所以房间状态:用户输入某个指令可以查看所以房间状态。

以下实现代码功能缺陷:已入住房间仍能被订房

Hotel类

public class Hotel {
    private Room[][] rooms;
        //用二维数组模拟一个三层楼,每层10个房间的酒店。
     public Hotel() {
         rooms = new Room[3][10];
         //用for循环遍历二维数组,创建Room对象。
         for (int i = 0;i<rooms.length;i++){        //i+1代表酒店楼层数
             for (int j=0;j<rooms[i].length;j++){   //j+1代表房间号
                 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 show(){
        for (int i = 0;i<rooms.length;i++){
            for (int j =0;j<rooms[i].length;j++){
                System.out.print(rooms[i][j]);
            }
            System.out.println();
        }
     }
     //办理订房
     public void liveIn(int rNum){
         Room room = rooms[rNum/100-1][rNum%100-1];
         room.setrCon(false);
     }
     //办理退房
     public void out(int rNum){
         Room room = rooms[rNum/100-1][rNum%100-1];
         room.setrCon(true);
     }
 }

Room类

public class Room{
    private int rNum;       //房间号
    private String rSyt;    //房间类型
    private boolean rCon;   //房间状态(空闲or占用)

    public Room(int rNum, String rSyt, boolean rCon) {
        this.rNum = rNum;
        this.rSyt = rSyt;
        this.rCon = rCon;
    }
    //setter getter方法
    public int getrNum() {
        return rNum;
    }

    public void setrNum(int rNum) {
        this.rNum = rNum;
    }

    public String getrSyt() {
        return rSyt;
    }

    public void setrSyt(String rSyt) {
        this.rSyt = rSyt;
    }

    public boolean getrCon() {
        return rCon;
    }

    public void setrCon(boolean rCon) {
        this.rCon = rCon;
    }

    @Override
    public String toString() {
        return "["+rNum+","+rSyt+","+(rCon ? "空闲":"占用"+"]");
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Room room = (Room) o;
        return rNum == room.rNum &&
                rCon == room.rCon &&
                Objects.equals(rSyt, room.rSyt);
    }
}

酒店管理系统测试类

public class HotelSystem{
    public static void main(String[] args) {
        Scanner src = new Scanner(System.in);
        Hotel adim = new Hotel();
        System.out.println("欢迎使用酒店管理系统");
        while (true){
            System.out.println("功能选项为:[1]办理入住 [2]办理退房 [3]查看当前所有房间状态 [4]退出本系统");
            System.out.print("请输入功能选项:");
            int a = src.nextInt();
            if (a==1){
                System.out.print("请输入房间号:");
                int a1 = src.nextInt();
                adim.liveIn(a1);
            }else if (a==2){
                System.out.print("请输入房间号:");
                int a2 = src.nextInt();
                adim.out(a2);
            }else if (a==3){
                adim.show();
            }else {
                return; 	//跳出循环,退出系统
            }
        }
    }
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值