Java语言实现简单的酒店前台管理小功能

笔者是一名刚上路的小萌新,有什么问题希望大家可以指正!

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

 

以下为该功能的源码:

Room类(酒店房间类)

package com.kukudeyu.hotelsystem;

public class Room {
    private int id;             //房间编号
    private String type;            //房间类型
    private boolean status;             //房间状态:true表示空闲,false表示占用

    public Room() {
    }

    public Room(int id, String type, boolean status) {
        this.id = id;
        this.type = type;
        this.status = status;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

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

    public boolean getStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

    /*
     *   重写toString方法
     *   打印出房间详情信息,其中包括房间编号,类型,状态
     * */
    @Override
    public String toString() {
        return "[" + this.id + "," + this.type + "," + (this.status ? "空闲":"占用" ) + "]";
    }

    //  按照惯例,重写equals方法,作用为判断两个房间是否为一个房间
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || !(o instanceof Room)) return false;
        Room room = (Room)o;
        if(this.id == room.id){
            return true;
        }
        return false;
    }
}

Hotel类(酒店类)

package com.kukudeyu.hotelsystem;

public class Hotel {
    private Room[][] rooms;      //利用二维数组创建酒店房间数组

    /*
        利用构造方法来进行酒店房间布置操作
        利用数组遍历,创建酒店房间对象放进酒店房间数组里
        其中,
        一层为单人间,二层为双人间,三层为总统套房
    */
    public Hotel() {
        rooms = new Room[3][10];

        for (int i = 0; i < rooms.length; i++) {
            for (int j = 0; j < rooms[i].length; j++) {
                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);
                }
            }
        }
    }

    /*
        print方法提供查看房间列表功能,可以查询所有房间的当前状态
        利用循环将所有房间对象均调用Room类的toString方法进行房间状态查询
     */
    public void print(){
        for(int i = 0 ; i< rooms.length ; i++){
            for(int j = 0 ; j<rooms[i].length ; j++){
                System.out.print(rooms[i][j].toString());        //调用Room类重写的toString方法,查看单个房间的状态
            }
            System.out.println();
        }
    }

    /*
       提供booking方法,用于修改房间状态
       即订房
       调用getStatus方法查询房间状态
       如果为true为空闲,提示订房成功
       如果为false为占用,提示房间占用
    */
    public void booking(int id){
        if(rooms[id / 100 -1][id % 100 -1].getStatus()){
            rooms[id / 100 - 1][id % 100 -1].setStatus(false);          //调用setStatus方法对房间状态进行修改
            System.out.println("订房成功!");
        }else{
            System.out.println("房间已占用,请换另外一间房!");
        }
    }

    /*
       提供cancelBooking方法,用于修改房间状态
       即退房
       对getStatus方法的返回值使用逻辑非,查询房间状态
       如果为false为占用,
    */
    public void cancelBooking(int id){
        if( rooms[id / 100 -1][id % 100 -1].getStatus() ){
            System.out.println("房间空闲,无需退房!");
        }else{
            rooms[id / 100 - 1][id % 100 -1].setStatus(true);
            System.out.println("退房成功!");
        }
    }
}

HotelSystem类(酒店系统类)

package com.kukudeyu.hotelsystem;

import java.util.Scanner;

public class HotelSystem {
    public static void main(String[] args) {
        Hotel hotel = new Hotel();               //创建一个酒店对象

        System.out.println("----------------------------------------------------------------------------");
        System.out.println("欢迎使用酒店管理系统,请认真阅读以下使用说明!");
        System.out.println("功能编号:【1】查看房间列表。【2】订房。【3】退房。【4】退出酒店管理系统。");
        System.out.println("----------------------------------------------------------------------------");
        Scanner s = new Scanner(System.in);

        while(true){
            System.out.print("请输入功能编号:");
            int i = s.nextInt();
            if(i == 1){
                hotel.print();
            }else if(i == 2 ){
                System.out.print("请输入要订房的房间编号:");
                int roomid = s.nextInt();
                hotel.booking(roomid);      //调用booking方法进行订房
            }else if(i == 3){
                System.out.print("请输入要退订的房间编号:");
                int roomid = s.nextInt();
                hotel.cancelBooking(roomid);        //调用cancelBooking方法进行退房
            }else if(i == 4){
                return;
            }
        }
    }
}

 

posted @ 2021-03-01 20:52  酷酷的宇  阅读(284)  评论(0)    收藏  举报