Room.java文件
import java.util.Objects;
public class Room {
// 房间编号
private int no;
// 房间类型
private String type;
// 房间状态
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 getStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
// equals()方法重写
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Room room = (Room) obj;
return no == room.no &&
status == room.status &&
Objects.equals(type, room.type);
}
// toString()方法重写
@Override
public String toString() {
return "["+no+type+ (status? "空闲]":"占用]");
}
}
Hotel.java文件
public class Hotel {
private Room[][] rooms;
public Hotel() {
// 酒店共三层,每层10个房间
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);
}
}
}
}
public Room[][] getRooms() {
return rooms;
}
public void setRooms(Room[][] rooms) {
this.rooms = rooms;
}
// 输出各楼层房间的状态
public void print(){
for (int i = 0; i < rooms.length; i++) {
for (int j = 0; j < rooms[i].length; j++) {
if (rooms[i][j].getStatus() == true) {
System.out.print(rooms[i][j]);
}else if (rooms[i][j].getStatus() == false) {
System.err.print(rooms[i][j]);
}
}
// 换行
System.out.println();
}
}
// 预定房间
public void order(int room){
rooms[room/100-1][room%100-1].setStatus(false);
}
// 退订房间
public void exit(int room){
rooms[room/100-1][room%100-1].setStatus(true);
}
}
MyHotel.java文件
import java.util.Scanner;
public class MyHotel {
public static void main(String[] args) {
Hotel hotel = new Hotel();
Scanner input = new Scanner(System.in);
boolean exitSta = true;
while (exitSta){
System.out.print(
"欢迎光临本酒店\n"+
"1.查看房间状态\n"+
"2.我要订房\n"+
"3.我要退房\n"+
"4.退出系统\n"+
"请输入功能编号:"
);
int select = input.nextInt();
switch (select) {
case 1:
hotel.print();
break;
case 2:
System.out.println("请输入您要订的房间号:");
int orderNum = input.nextInt();
hotel.order(orderNum);
System.out.println("预定"+orderNum+"房间成功!");
break;
case 3:
System.out.println("请输入您要退订的房间号:");
int exitNum = input.nextInt();
hotel.exit(exitNum);
System.out.println("退房"+exitNum+"房间成功!");
break;
case 4:
exitSta = false;
System.out.println("退出系统成功!欢迎下次入住本酒店!");
break;
default:
System.err.println("请输入正确的功能编号!");
break;
}
}
}
}
有些小BUG!!!但不影响体会理解