使用二维数组模拟简易酒店订房退房系统

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()方法重写@Overridepublic 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()方法重写@Overridepublic 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!!!但不影响体会理解

使用二维数组模拟简易酒店订房退房系统

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()方法重写@Overridepublic 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()方法重写@Overridepublic 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!!!但不影响体会理解