java 小项目Demo

1.酒店管理demo:

//Room.java
package hotel;

public class Room {
    private int num;
    private boolean isOut;

    public Room(int n) {
        this.num = n;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public boolean isOut() {
        return isOut;
    }

    public void setOut(boolean isOut) {
        this.isOut = isOut;
    }

    public String toString() {
        return "[" + this.getNum() + "," + (this.isOut() ? "是" : "否") + "] ";
    }

}
//Hotel.java
package hotel;

public class Hotel {
    private int colum;
    private int row;
    Room[][] rooms;

    public Hotel(int row, int colum) {
        this.colum = colum;
        this.row = row;
        this.rooms = new Room[row][colum];
        // 初始化房间号
        this.initRooms();
    }

    private void initRooms() {
        for (int i = 0; i < this.rooms.length; i++) {
            for (int j = 0; j < this.rooms[i].length; j++) {
                this.rooms[i][j] = new Room((i + 1) * 100 + (j + 1));
            }
        }
    }

    /**
     * 入住酒店
     */
    public void setRooms(int roomNum) {
        // 楼层
        int row = roomNum / 100 - 1;
        // 房间号
        int colum = roomNum % 100 - 1;

        if (!this.rooms[row][colum].isOut()) {
            this.rooms[row][colum].setOut(true);
            System.out.println("房间订阅完成");
        } else {
            System.out.println("房间已被订");
            return;
        }
    }

    /**
     * 退订酒店
     */
    public void setRoomsOut(int roomNum) {
        // 楼层
        int row = roomNum / 100 - 1;
        // 房间号
        int colum = roomNum % 100 - 1;

        this.rooms[row][colum].setOut(false);
        System.out.println("房间退订完成");

    }

    /**
     * 查询房间状态
     */
    public void seeRooms() {
        if (this.rooms.length == 0) {
            System.out.println("酒店未初始化");
        }
        for (int i = 0; i < this.rooms.length; i++) {
            for (int j = 0; j < this.rooms[i].length; j++) {
                System.out.print(this.rooms[i][j]);
            }
            System.out.println("");
        }
    }
}
//App.java
import java.util.Scanner;

import hotel.Hotel;
import hotel.Room;

public class App {
    public static void main(String[] args) throws Exception {

        if (args.length == 2) {
            if ("admin".equals(args[0]) && "123".equals(args[1])) {
                Hotel h = new Hotel(10, 11);
                System.out.println("欢迎登录管理系统");
                while (true) {
                    System.out.println("输入以下指令操作系统:[1]查看所有房间;[2]订房;[3]退房;[4]退出系统;");
                    Scanner s = new Scanner(System.in);
                    switch (s.nextInt()) {
                        case 1:
                            h.seeRooms();
                            break;
                        case 2:
                            System.out.println("订房:请输入房间号");
                            Scanner s2 = new Scanner(System.in);
                            h.setRooms(s2.nextInt());
                            break;
                        case 3:
                            System.out.println("退房:请输入房间号");
                            Scanner s3 = new Scanner(System.in);
                            h.setRoomsOut(s3.nextInt());
                            break;
                        case 4:
                            System.out.println("欢迎下次再来");
                            return;
                    }
                }
            } else {
                System.out.println("账号密码错误");
                return;
            }
        } else {
            System.out.println("非法进入");
            return;
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

y_w_x_k

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

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

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

打赏作者

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

抵扣说明:

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

余额充值