第四章 4.1 二维数组练习题

简易酒店管理系统

酒店房间

/***
 * 酒店的房间
 */
public class Room {
    /**
     * 房间编号:101...110;601...610
     * */
    private int roomNo;
    /*
    ·单人间,标准间,豪华间
     */
    private String type;
    /**
     * true表示占用
     * */
    private boolean state;

    public Room(int roomNo, String type, boolean state) {
        this.roomNo = roomNo;
        this.type = type;
        this.state = state;
    }

    public Room() {
    }

    public boolean isState() {
        return state;
    }

    public void setState(boolean state) {
        this.state = state;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getRoomNo() {
        return roomNo;
    }

    public void setRoomNo(int roomNo) {
        this.roomNo = roomNo;
    }

    @Override
    public String toString() {
         return "["+this.roomNo + "," + this.type + "," + (this.state ? "占用" : "空闲") + "]";
    }
}

酒店

public class Hotel {
    //6为楼层 10为每层有10个房间
    private Room[][] rooms = new Room[6][10];//60个null

    /*
        构造方法:通过构造方法可以盖一个酒店
     */

    public Hotel(){
        for (int i = 0; i<rooms.length; i++){
            for (int j = 0; j<rooms[i].length; j++){
                if (i == 0 || i == 1){
                    //1 2 楼
                    rooms[i][j] = new Room((i + 1)*100 + j + 1 ,"单人间",false);
                }else if (i == 2 || i == 3){
                    rooms[i][j] = new Room((i + 1)*100 + j + 1 ,"标准间",false);
                }else if (i == 4 || i == 5){
                    rooms[i][j] = new Room((i + 1)*100 + j + 1 ,"豪华间",false);
                }

            }
        }
    }
    public void  order(int roomNo){
        //101 i = 0; j = 0 ;
        rooms[roomNo / 100 - 1][roomNo % 100 - 1 ].setState(true);
    }
    public void exit(int roomNo){
        rooms[roomNo / 100 - 1][roomNo % 100 - 1 ].setState(false);
    }
    public void display(){
        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();
        }
    }
}

酒店前台

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Hotel hotel = new Hotel();
        System.out.println("欢迎使用酒店管理系统,请按以下操作对应输入:");
        System.out.println("[1]查看所有房间状态");
        System.out.println("[2]预定房间");
        System.out.println("[3]退房");
        System.out.println("[0]退出系统");
        Scanner s =  new Scanner(System.in);
        while (true){
            System.out.println("请输入功能编号:");
            //获取功能编号
            int  no = s.nextInt();
            switch (no){
                case 1:
                    hotel.display();
                    break;
                case 2:
                    System.out.println("请输入预定的房间号");
                    int roomNo = s.nextInt();
                    hotel.order(roomNo);
                    System.out.println("预定房间"+roomNo+"成功");
                    break;
                case 3:
                    System.out.println("请输入退房的房间号");
                    int roomNo1 = s.nextInt();
                    hotel.exit(roomNo1);
                    System.out.println("退房"+roomNo1+"成功");
                    break;
               case 0:
                   System.out.println("退出系统");
                   System.exit(0);
            }
        }
    }
}

运行结果示意图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值