可以退房:用户输入房间编号,退房。
可以查看所有房间的状态:用户输入某个指令应该可以查看所有房间状态。
Room类:
import java.util.Objects;
//酒店房间类。
public class Room {
/*
房间编号:
一楼:101 102 103 104 105 106 ...
二楼:201 202 203 204 205 206 ...
三楼:301 302 303 304 305 306 ...
.......
*/
private int no;
/*
房间类型:标准间,单人间,豪华间
*/
private String type;
;
/*
true表示房间空闲,可以预定。
false表示房间占用,不能预定。
*/
private boolean status;
public Room() {
}
public Room(int no, String type, boolean status) {
this.no = no;
this.type = type;
this.status = status;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
/*
equals方法:比较两个房间编号是不是同一个房间类型。
*/
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Room room = (Room) o;
return no == room.no && Objects.equals(type, room.type);
}
public String toString() {
return "Room{" + no + "," + type + '\'' + ", " + (status ? "空闲" : "占用") + " }";
}
}
Hotel类:
//酒店对象,二维数组模拟酒店大厦
public class Hotel {
private Room[][] rooms;
/*
房间编号:
一楼:101 102 103 104 105 106 ...
二楼:201 202 203 204 205 206 ...
三楼:301 302 303 304 305 306 ...
.......
*/
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:打印房间状态列表的方法。
*/
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();
}
}
/*
order:订房方法,订完房后,放被占用。
将room对象的status属性修改为false。
*/
public void order(int roomNo) {
Room room = rooms[roomNo / 100 - 1][roomNo % 100 - 1];
room.setStatus(false);
System.out.println(roomNo+"订房成功");
}
/*
exit:退房方法,退完房,房间空闲
将room的statues属性修改为true
*/
public void exit(int roomNo){
Room room = rooms[roomNo / 100 - 1][roomNo % 100 - 1];
room.setStatus(true);
System.out.println(roomNo+"退房成功");
}
}
酒店管理系统测试类:
public class HotelMgsSystem {
public static void main(String[] args) {
Hotel hotel=new Hotel();
System.out.println("欢迎使用酒店管理系统,请在使用前阅读以下说明");
System.out.println("功能如下:[1]查看房间状态,[2]订房,[3]退房,[0]退出系统");
Scanner s=new Scanner(System.in);
while(true) {
System.out.print("请输入功能编号:");
int i = s.nextInt();