Java基础练习之数组

最近在学习Java的基础,也是听了某网课的视频;
其中有一道题目是这样的:

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

                          刚开始做的我

在这里插入图片描述

第一遍做的是数组静态的初始化,就不上传啦,后来又参考了老师的代码,写出了第二个版本,对用户传入参数错误问题进行了处理。

下面就是上代码啦:

import java.util.Scanner;

public class HotelMS {
	Room1[][]rooms = new Room1 [3][10];

    public static void main(String[] args) {
        HotelMS h = new HotelMS();
        while (true){
            System.out.println();
            System.out.println("欢迎使用酒店管理系统!");
            System.out.println("1:房间预约 | 2:退房 | 3:查看房间列表 | 0:退出系统" );
            System.out.println("请输入对应功能编号:");
            Scanner scanner = new Scanner(System.in);
            int q = scanner.nextInt();
            if (q == 1){
                System.out.println("请输入您要预约的房间号码:");
                int w = scanner.nextInt();
                if (w>=101 && w<=110 || w>=201 && w<= 210 || w>=301 && w<= 310 ){
                    if (h.check(w)){
                        System.out.println("您预约的房间已满");
                    }
                    else {
                        h.order(w);
                    }
                }
                else {
                    System.out.println("您的输入有误!");
                }

            }
            else if (q == 2){
                System.out.println("请您输入要退订的房间号码:");
                int w = scanner.nextInt();
                if (w>=101 && w<=110 || w>=201 && w<= 210 || w>=301 && w<= 310 ){
                    if (h.check(w)==false){
                        System.out.println("此房空闲,请检查房间号码是否正确!");
                    }
                    else {
                        System.out.println("请稍后......");
                        h.Unsubscribe(w);
                    }

                }
                else {
                    System.out.println("您的输入有误!");
                }
            }
            else if (q == 3){
                h.check();
            }
            else if (q == 0){
                System.out.println("感谢使用!");
                break;
            }
            else {
                System.out.println("您的输入有误!");
            }
        }


    }

    public boolean order(int room_no){
            Room1 r = rooms[room_no / 100 - 1][room_no % 100 - 1];
            r.setRoom_state(true);
            System.out.println(r.getRoom_no()+"订房成功");
            return r.isRoom_state();
    }

    public boolean Unsubscribe(int room_no){
        Room1 r = rooms[room_no / 100-1][room_no % 100-1];
        r.setRoom_state(false);
        System.out.println(room_no+"退房成功");
        return r.isRoom_state();
    }


    public boolean check(int room_no){
        Room1 r = rooms[room_no / 100-1][room_no % 100-1];
        return r.isRoom_state();
    }

    public void check(){
        for (int i = 0; i < rooms.length; i++) {
            for (int j = 0; j < rooms[i].length; j++) {
                Room1 r = rooms[i][j];
                if (r.isRoom_state()==true){
                    System.out.print("("+r.getRoom_no()+" "+r.getRoom_type()+" "+"已预约"+")");
                }
                else if (r.isRoom_state()==false){
                    System.out.print("("+r.getRoom_no()+" "+r.getRoom_type()+" "+"空闲"+")");
                }

            }
            System.out.println();

        }
    }

    public HotelMS() {
        for (int i = 0; i < rooms.length; i++) {
            for (int j = 0; j < rooms[i].length; j++) {
                if (i ==0) {
                    rooms[i][j] = new  Room1(100*(i+1)+j+1,"标准间",false);
                }else if(i==1){
                    rooms[i][j]= new  Room1(100*(i+1)+j+1,"豪华间",false);
                }else if (i==2){
                    rooms[i][j]= new  Room1(100*(i+1)+j+1,"套房",false);
                }
            }
        }
    }
}

class Room1 {
private int room_no;
private String room_type;
private boolean room_state;

    public Room1() {
    }

    public Room1(int room_no, String room_type, boolean room_state) {
        this.room_no = room_no;
        this.room_type = room_type;
        this.room_state = room_state;
    }

    public int getRoom_no() {
        return room_no;
    }

    public void setRoom_no(int room_no) {
        this.room_no = room_no;
    }

    public String getRoom_type() {
        return room_type;
    }

    public void setRoom_type(String room_type) {
        this.room_type = room_type;
    }

    public boolean isRoom_state() {
        return room_state;
    }

    public void setRoom_state(boolean room_state) {
        this.room_state = room_state;
    }
}

class Reception1{
private String re_name;
private int re_no;

    public Reception1() {
    }

    public Reception1(String re_name, int re_no) {
        this.re_name = re_name;
        this.re_no = re_no;
    }

    public String getRe_name() {
        return re_name;
    }

    public void setRe_name(String re_name) {
        this.re_name = re_name;
    }

    public int getRe_no() {
        return re_no;
    }

    public void setRe_no(int re_no) {
        this.re_no = re_no;
    }
}

#代码直接复制粘贴过来缩进似乎有点问题,不过我已经改好了。

在这里插入图片描述

欢迎大家一起交流!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值