To use Java to realize some functions of the hotel management system: room inspection, reservation, check-out and other functions, first analyze the hotel object and room object, write the hotel class, room class, and then have a hotel system test class. The hotel can be imagined as a container, and the room exists like a two-dimensional array (as shown in the figure below).
- Room class
Write the room class, determine the attributes: room number, room type, room status, and rewrite the toString() and equals() methods.
import java.util.Objects;
public class Room {
private int num;
private String type;
private boolean statue;
public Room(int num, String type, boolean statue) {
this.num = num;
this.type = type;
this.statue = statue;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isStatue() {
return statue;
}
public void setStatue(boolean statue) {
this.statue = statue;
}
@Override
public String toString() {
return "[房间号:"+num+" 房间类型:"+type+" 房间状态:"+statue+"]";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Room room = (Room) o;
return num == room.num && statue == room.statue && Objects.equals(type, room.type);
}
}
- Hotel class
Two-dimensional array simulates hotel rooms, for loop traverses to create room objects, and then writes room inspection methods, room reservation methods and check-out methods.
public class Hotel {
//酒店对象 二维数组模拟酒店房间
private Room[][] rooms;
//构造方法造房间
public Hotel() {
rooms = new Room[3][5]; //三层楼每层十个房间
//for循环遍历,创建30个Room对象
for (int i = 0; i < rooms.length; i++) {
for (int j = 0; j < rooms[i].length; j++) {
// new Room((i+1)*100+(j+1),"房间类型",true);
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);
}
}
}
}
//在酒店对象上提供打印房间列表的功能
public void print(){
for (int i = 0; i < rooms.length; i++) {
for (int j = 0; j < rooms[i].length; j++) {
Room room = rooms[i][j];
System.out.print(room);
}
System.out.println();
}
}
//订房方法 调用方法穿房间编号过来
public void order(int roomNum){
//订房最主要的是将房间 对象的状态由true修改为false,代表房间占用
//通过房间编号获取数组下标。获取房间对象
rooms[roomNum / 100 - 1][roomNum % 100 - 1].setStatue(false);
System.out.println(roomNum+"已订房");
}
//退房;将房间状态改为true,代表房间空闲
public void exit(int roomNum){
//订房最主要的是将房间 对象的状态由true修改为false
//通过房间编号获取数组下标。获取房间对象
rooms[roomNum / 100 - 1][roomNum % 100 - 1].setStatue(true);
System.out.println(roomNum+"已退房");
}
}
- Hotel system test class
Write service menu.
import java.util.Scanner;
public class HotelSystem {
public static void main(String[] args) {
Hotel hotel = new Hotel();
System.out.println("欢迎使用酒店管理系统,请认真阅读使用说明");
System.out.println("请输入对应功能编号:[1]表示查看房间列表 [2]表示订房 [3]表示退房 [0]表示退出");
Scanner scanner = new Scanner(System.in);
//循环使用
while (true) {
System.out.println("请输入功能编号:");
int i = scanner.nextInt();
if (i == 1) {
//查看房间列表
hotel.print();
} else if (i == 2) {
//订房
System.out.println("请输入订房编号:");
int roomNum = scanner.nextInt();//输入房间编号
hotel.order(roomNum);
} else if (i == 3) {
//退房
System.out.println("请输入退房编号:");
int roomNum = scanner.nextInt();//输入房间编号
hotel.exit(roomNum);
} else if (i == 0) {
//退出系统
System.out.println("已退出系统...");
}
}
}
}
This is an immature system with big loopholes, and the information cannot be saved, and it will be improved after establishing a connection with the database.
result: