java数组练习项目(简单的酒店管理系统)

房间类(Room):

属性:房间编号(int),房间类型(String),是否为空(boolean)

方法:构造方法,set/get方法,toString方法(重写)

class Room{
    private int id;
    private String roomType;
    private boolean isEmpty;
    //constructor

    public Room() {
    }

    public Room(int id, String roomType, boolean isEmpty) {
        this.id = id;
        this.roomType = roomType;
        this.isEmpty = isEmpty;
    }
    //setter and getter

    public int getId() {
        return id;
    }

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

    public String getRoomType() {
        return roomType;
    }

    public void setRoomType(String roomType) {
        this.roomType = roomType;
    }

    public boolean isEmpty() {
        return isEmpty;
    }

    public void setEmpty(boolean empty) {
        isEmpty = empty;
    }

    @Override
    public String toString() {
        return "(" + this.roomType + ")" + this.id + " is " + (this.isEmpty?"empty":"subcribed");
    }
}

Room类并没有赋予很多方法,只重写了toString方法,便于直接打印房间信息

前台类(FrontDesk):

属性:房间列表(Room[][])

方法:构造方法,set/get方法,预定房间方法(reservation),退订房间(checkOut)方法,便利所有房间(ergodic)方法,初始化(initialization)方法,主方法

package com.array;


import java.util.Scanner;

public class FrontDesk {
    private Room[][] roomsList;
    //constructor
    public FrontDesk() {

    }

    public FrontDesk(Room[][] roomsList) {
        this.roomsList = roomsList;
    }

    public Room[][] getRoomsList() {
        return roomsList;
    }

    public void setRoomsList(Room[][] roomsList) {
        this.roomsList = roomsList;
    }
    public void reservation(int reserveNumber){
        if (reserveNumber/100 >5 || reserveNumber%100 > 20){
            System.out.println("Room number error, please re-enter");
            return;
        }
        for (int i = 0;i < roomsList.length;i++){
            for (int j = 0;j < roomsList[i].length;j++){
                if (roomsList[i][j].getId() == reserveNumber){
                    if (roomsList[i][j].isEmpty()){
                        roomsList[i][j].setEmpty(false);
                        System.out.println("Reservation successful");
                        return;
                    }else {
                        System.out.println("Reservation failed, the room has been occupied");
                        return;
                    }
                }
            }
        }
    }
    public void checkOut(int outNumber){
        if (outNumber/100 >5 || outNumber%100 > 20){
            System.out.println("Room number error, please re-enter");
            return;
        }
        for (int i = 0;i < roomsList.length;i++){
            for (int j = 0;j < roomsList[i].length;j++){
                if (roomsList[i][j].getId() == outNumber){
                    System.out.println((!roomsList[i][j].isEmpty())?"Unsubscribe successfully":"This room is not subscribed. Please re verify the room number");
                   return;
                }
            }
        }
    }
    public void ergodic(){
        for (int i = 0;i < roomsList.length;i++){
            for (int j = 0;j < roomsList[i].length;j++){
                System.out.print(roomsList[i][j] + " ");
            }
            System.out.println();
        }
    }
    public void initialization(){
        this.setRoomsList(new Room[5][20]);
        for (int i = 0;i < roomsList.length;i++){
            for (int j = 0;j < roomsList[i].length;j++){
                this.roomsList[i][j] = new Room();
                this.roomsList[i][j].setId((i+1) * 100 + (j+1));
                this.roomsList[i][j].setEmpty(true);
                if (i <= 2){
                    this.roomsList[i][j].setRoomType("single room");
                }else {
                    this.roomsList[i][j].setRoomType("Double room");
                }
            }
        }
    }

    public static void main(String[] args) {
        FrontDesk huang = new FrontDesk();
        huang.initialization();
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("Welcome to our hotel. What do you need to do?");
            System.out.println("1. Book a room 2. Unsubscribe a room 3. View all rooms 4. Exit the system");
            int input = scanner.nextInt();
            switch (input){
                case 1:
                    System.out.println("Please enter the reservation number");
                    int reserveNumber = scanner.nextInt();
                    huang.reservation(reserveNumber);
                    break;
                case 2:
                    System.out.println("Please enter the unsubscribe room number");
                    int outNumber = scanner.nextInt();
                    huang.checkOut(outNumber);
                    break;
                case 3:
                    huang.ergodic();
                    break;
                case 4:
                    System.exit(0);
                    break;
                default:
                    System.out.println("input error, please re-enter");
            }
        }
    }
}

完整代码如下

package com.array;


import java.util.Scanner;

public class FrontDesk {
    private Room[][] roomsList;
    //constructor
    public FrontDesk() {

    }

    public FrontDesk(Room[][] roomsList) {
        this.roomsList = roomsList;
    }

    public Room[][] getRoomsList() {
        return roomsList;
    }

    public void setRoomsList(Room[][] roomsList) {
        this.roomsList = roomsList;
    }
    public void reservation(int reserveNumber){
        if (reserveNumber/100 >5 || reserveNumber%100 > 20){
            System.out.println("Room number error, please re-enter");
            return;
        }
        for (int i = 0;i < roomsList.length;i++){
            for (int j = 0;j < roomsList[i].length;j++){
                if (roomsList[i][j].getId() == reserveNumber){
                    if (roomsList[i][j].isEmpty()){
                        roomsList[i][j].setEmpty(false);
                        System.out.println("Reservation successful");
                        return;
                    }else {
                        System.out.println("Reservation failed, the room has been occupied");
                        return;
                    }
                }
            }
        }
    }
    public void checkOut(int outNumber){
        if (outNumber/100 >5 || outNumber%100 > 20){
            System.out.println("Room number error, please re-enter");
            return;
        }
        for (int i = 0;i < roomsList.length;i++){
            for (int j = 0;j < roomsList[i].length;j++){
                if (roomsList[i][j].getId() == outNumber){
                    System.out.println((!roomsList[i][j].isEmpty())?"Unsubscribe successfully":"This room is not subscribed. Please re verify the room number");
                   return;
                }
            }
        }
    }
    public void ergodic(){
        for (int i = 0;i < roomsList.length;i++){
            for (int j = 0;j < roomsList[i].length;j++){
                System.out.print(roomsList[i][j] + " ");
            }
            System.out.println();
        }
    }
    public void initialization(){
        this.setRoomsList(new Room[5][20]);
        for (int i = 0;i < roomsList.length;i++){
            for (int j = 0;j < roomsList[i].length;j++){
                this.roomsList[i][j] = new Room();
                this.roomsList[i][j].setId((i+1) * 100 + (j+1));
                this.roomsList[i][j].setEmpty(true);
                if (i <= 2){
                    this.roomsList[i][j].setRoomType("single room");
                }else {
                    this.roomsList[i][j].setRoomType("Double room");
                }
            }
        }
    }

    public static void main(String[] args) {
        FrontDesk huang = new FrontDesk();
        huang.initialization();
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("Welcome to our hotel. What do you need to do?");
            System.out.println("1. Book a room 2. Unsubscribe a room 3. View all rooms 4. Exit the system");
            int input = scanner.nextInt();
            switch (input){
                case 1:
                    System.out.println("Please enter the reservation number");
                    int reserveNumber = scanner.nextInt();
                    huang.reservation(reserveNumber);
                    break;
                case 2:
                    System.out.println("Please enter the unsubscribe room number");
                    int outNumber = scanner.nextInt();
                    huang.checkOut(outNumber);
                    break;
                case 3:
                    huang.ergodic();
                    break;
                case 4:
                    System.exit(0);
                    break;
                default:
                    System.out.println("input error, please re-enter");
            }
        }
    }
}
class Room{
    private int id;
    private String roomType;
    private boolean isEmpty;
    //constructor

    public Room() {
    }

    public Room(int id, String roomType, boolean isEmpty) {
        this.id = id;
        this.roomType = roomType;
        this.isEmpty = isEmpty;
    }
    //setter and getter

    public int getId() {
        return id;
    }

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

    public String getRoomType() {
        return roomType;
    }

    public void setRoomType(String roomType) {
        this.roomType = roomType;
    }

    public boolean isEmpty() {
        return isEmpty;
    }

    public void setEmpty(boolean empty) {
        isEmpty = empty;
    }

    @Override
    public String toString() {
        return this.roomType + " " + this.id + " " + (this.isEmpty?"empty":"subcribed");
    }
}

本人实为java初学者,学艺不精,希望各位读者批评指正!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值