Test:
import java.util.Scanner;
public class HotelSys {
public static void main(String[] args) {
Hotel hotel = new Hotel();
System.out.println("======欢迎使用酒店管理系统======");
System.out.println("请输入操作指令:[1]查看房间状态,[2]订房,[3]退房,[0]退出系统");
while(true){
int shuru = new Scanner(System.in).nextInt();
if(shuru == 1){
hotel.print();
}else if(shuru == 2){
System.out.print("输入订房编号:");
int roomNo = new Scanner(System.in).nextInt();
hotel.dingf(roomNo);
}else if(shuru == 3){
System.out.println("输入退房编号:");
int roomNo =new Scanner(System.in).nextInt();
hotel.tuif(roomNo);
}else if(shuru == 0){
System.out.println("======退出成功======");
return;
}else {
System.out.println("输入有误,请重新输入!");
}
}
}
}
Room:
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;
}
public String toString(){
return "[" + no +","+ type +","+ (true ? "空闲" : "占用") +"]";
}
public boolean equals(Object obj){
if(obj == null || !(obj instanceof Room)) return false;
if(this == obj) return true;
Room room = (Room)obj;
return this.getNo() == room.getNo() && this.getType().equals(room.getType()) && this.getStatus() == room.getStatus();
}
}
Hotel:
public class Hotel {
private Room[][] rooms;
public Hotel(){
rooms = new Room[3][5];//三层楼5个房间
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 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 dingf(int roomNo){
Room room = rooms[roomNo / 100 -1][roomNo % 100 -1];
room.setStatus(false);
System.out.println(roomNo + "已订房!");
}
//退房
public void tuif(int roomNo){
Room room = rooms[roomNo/ 100 -1][roomNo % 100 -1];
room.setStatus(true);
System.out.println(roomNo + "已退房!");
}
}



