用java实现简单的酒店管理系统

系统概述:

1、酒店默认有3层,每层有10个房间;

2、房间类型有单人间、标准间和豪华间;

3、可以订房、退房、查询所有房间的状态(每个房间的类型和是否被占用);

源代码如下:

酒店类:

/**
 * 酒店类
 */

public class Hotel {
    //定义房间
    private Room[][] rooms;

    public Hotel() {
        //默认房间有三层每层有10个房间
        rooms = new Room[3][10];
        for(int i=0;i<rooms.length;i++){
            for (int j=0;j<rooms[i].length;j++){
                rooms[i][j]=new Room(((i+1)*100+j+1),((i+1)==1?"单人间":((i+1)==2?"标准间":"豪华间")),true);
            }
        }
    }
    /*输出所有房间状态*/
    public void print(){
        for (int i = 0; i < this.rooms.length; i++) {
            for (int j=0;j<this.rooms[i].length;j++){
                Room room=this.rooms[i][j];
                System.out.println(room);
            }
        }

    }
    /*订房方法*/
    public void order(int roomId){
        int i=roomId/100-1;
        int j=roomId%100-1;
        if (this.rooms[i][j].isSta()==true) {
            this.rooms[i][j].setSta(false);
            System.out.println(roomId + "预定成功!");
        }else{
            System.out.println("订房失败!房间已被占用!");
        }

    }
    /*退房的方法*/
    public void exitRoom(int roomId){
        int i=roomId/100-1;
        int j=roomId%100-1;
        if(this.rooms[i][j].isSta()==false){
            this.rooms[i][j].setSta(true);
            System.out.println(roomId+"退房成功!");
        }else{
            System.out.println("退房失败!房间没有被预定!");
        }
    }

}

房间类:

/**
 * 房间类
 */

import java.util.Objects;

public class Room {
    //房间编号
    private int id;
    //房间类型
    private String type;
    //房间状态
    private boolean sta;
    //有参构造
    public Room(int id, String type, boolean sta) {
        this.id = id;
        this.type = type;
        this.sta = sta;
    }
    //无参构造方法
    public Room() {
    }

     /*get、set方法*/
    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 isSta() {
        return sta;
    }

    public void setSta(boolean sta) {
        this.sta = sta;
    }

    /*重写toString方法*/
    public String toString() {
        return "房间号:" + id +
                ", 房间类型:" + type +
                ", 状态:" + (sta==true?"空闲":"占用") ;
    }

    /*重写equals方法*/
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Room room = (Room) o;
        return id == room.id && sta == room.sta && type.equals(room.type);
    }

}

主类:

/**
 * 酒店管理系统
 */
import java.util.Scanner;
public class HotelSystem {
    public static void main(String[] args) {
        //创建酒店对象
        Hotel h=new Hotel();
        //创建扫描器对象
        Scanner sc=new Scanner(System.in);
        while(true) {
            /*提示信息*/
            System.out.println("********欢迎使用张同学酒店订房系统********");
            System.out.println("======================================");
            System.out.println("订房输入: 1\t\t退房输入: 2");
            System.out.println("房间查询输入: 3\t退出系统输入: 4");
            System.out.println("======================================");
            System.out.print("请输入:");
            int num=sc.nextInt(); //输入操作状态
            if(num==1){
                System.out.print("请输入要预定的房间编号:");
                int roomId=sc.nextInt();
                h.order(roomId);//调用订房方法
                //该步骤意思是上面输出后暂停在这个位置,输入一个字符才能继续。以下同理
                System.out.println("按回车键继续!");
                String n=sc.nextLine();
                String n1=sc.nextLine();
            }else if(num==2){
                System.out.print("请输入要退的房间编号:");
                int roomId=sc.nextInt();
                h.exitRoom(roomId); //调用退房方法
                System.out.println("按回车键继续!");
                String n=sc.nextLine();
                String n1=sc.nextLine();
            }else if (num==3){
                h.print(); //调用输出方法
                System.out.println("按回车键继续!");
                String n=sc.nextLine();
                String n1=sc.nextLine();
            }else if(num==4){
                return;  //系统结束
            }else{
                System.out.println("序号输入有误请重新输入!");
                System.out.println("按回车键继续!");
                String n=sc.nextLine();
                String n1=sc.nextLine();
            }

        }

    }
}

主界面:

 

  • 11
    点赞
  • 92
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张同学%

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值