二维数组实现酒店管理系统
public class Room {
int id;
String type;
boolean isEmp;
public Room(int id, String type, boolean isEmp) {
this.id = id;
this.type = type;
this.isEmp=isEmp;
}
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 isEmp() {
return isEmp;
}
public void setEmp(boolean emp) {
isEmp = emp;
}
@Override
public String toString() {
return "["+id+type+(isEmp?"空闲":"已用")+"]";
}
}
public class Hotel {
Room[][] rooms = new Room[3][3];
public Hotel() {
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(100 * (i + 1) + j + 1, "标准间", true);
} else if (i == 1) {
rooms[i][j] = new Room(100 * (i + 1) + j + 1, "单人间", true);
} else if (i == 2) {
rooms[i][j] = new Room(100 * (i + 1) + j + 1, "总统套房", true);
}
}
}
}
public void print() {
for (int i = 0; i < rooms.length; i++) {
for (int j = 0; j < rooms[i].length; j++) {
System.out.print(rooms[i][j]);
}
System.out.println();
}
}
public void getRoom(int RoomNo){
Room room = rooms[RoomNo / 100 - 1][RoomNo % 100 - 1];
room.isEmp=false;
System.out.println("订房成功");
}
public void exitRoom(int RoomNo){
Room room = rooms[RoomNo / 100 - 1][RoomNo % 100 - 1];
room.isEmp=true;
System.out.println("退房成功");
}
}
public class HotelText {
public static void main(String[] args) {
Hotel hotel=new Hotel();
System.out.println("欢迎来到***酒店");
System.out.println("1.[查看房间状态] 2.[订房] 3.[退房] 4.[退出]");
while (true) {
System.out.println("请输入操作");
Scanner scanner = new Scanner(System.in);
int i = scanner.nextInt();
if (i == 1) {
hotel.print();
} else if (i == 2) {
System.out.println("请输入订房编号");
int x = scanner.nextInt();
hotel.getRoom(x);
System.out.println("订房成功");
} else if (i == 3) {
System.out.println("请输入退房编号");
int x = scanner.nextInt();
hotel.exitRoom(x);
System.out.println("退房成功");
} else if (i == 4) {
System.exit(0);
} else {
System.out.println("输入错误");
}
}
}
}
欢迎来到***酒店
1.[查看房间状态] 2.[订房] 3.[退房] 4.[退出]
请输入操作
1
[101标准间空闲][102标准间空闲][103标准间空闲]
[201单人间空闲][202单人间空闲][203单人间空闲]
[301总统套房空闲][302总统套房空闲][303总统套房空闲]
请输入操作
2
请输入订房编号
302
订房成功
订房成功
请输入操作
1
[101标准间空闲][102标准间空闲][103标准间空闲]
[201单人间空闲][202单人间空闲][203单人间空闲]
[301总统套房空闲][302总统套房已用][303总统套房空闲]
请输入操作
0
输入错误
请输入操作
4
Process finished with exit code 0