首先对对象进行抽象化处理,一共有三大块(房间、酒店、操作系统)
房间类中需要包含房间号、房间类型、房间状态等实例变量
酒店类中需要创建二维矩阵储存房间实例,能够对房间的实例变量进行赋值,也能够对房间的状态进行修改。
操作系统中可以接收用户输入的命令,通过酒店类中的方法实现订房、退房等操作。
1、首先建立房间类,对房间类进行分析
有参和无参的构造方法,对方法进行封装,equals()方法和toString()方法的重写
import java.util.Objects;
public class Rooms {
// 房间号
private int no ;
// 类型
private String type ;
// 状态
private boolean status ;
// 构造方法
public Rooms() {
}
public Rooms(int no, String type, boolean status) {
this.no = no;
this.type = type;
this.status = status;
}
// getter and setter
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
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Rooms rooms = (Rooms) o;
return no == rooms.no &&
status == rooms.status &&
Objects.equals(type, rooms.type);
}
// toString
public String toString() {
return "Rooms{" + "no=" + no + ", type=" + type + ", status=" + (status ? "空闲" : "占用") + '}';
}
// 实验程序
/*public static void main(String[] args) {
Rooms room = new Rooms(101,"单人间",true);
System.out.println(room.toString());
}*/
}
2、其次建立酒店类
酒店中需要建立房间类型的二维矩阵去储存房间的实例变量,注意这布操作一共有两布,首先是建立房间类型的二维矩阵,然后是在构造方法中对矩阵中的每一个元素进行房间实例的生成。然后就是对订房、退房和遍历房间三个方法进行编写。
public class Hotel {
// 定义酒店的房间数组
private Rooms[][] rooms;
// 构造方法
public Hotel() {
rooms = new Rooms[3][4];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
rooms[i][j] = new Rooms();
rooms[i][j].setNo((i + 1) * 100 + j + 1);
rooms[i][j].setStatus(true);
switch (i) {
case 0:
rooms[i][j].setType("标准间");
continue;
case 1:
rooms[i][j].setType("豪华间");
continue;
case 2:
rooms[i][j].setType("总统套房");
continue;
}
}
}
System.out.println("房间初始化成功");
}
public Hotel(Rooms[][] rooms) {
this.rooms = rooms;
}
// 订房
public void roomOrder(int no){
int floor = no/100 ;
int room = no%10 ;
if (floor-1 < rooms.length && room < rooms[floor-1].length) {
if (rooms[floor - 1][room - 1].getStatus()) {
rooms[floor - 1][room - 1].setStatus(false);
System.out.println("订房成功");
} else
System.out.println("房间已经被订,请选择另外的房间");
}else{
System.out.println("房间号不存在,请重新输入");
}
}
// 退房
public void roomOut(int no){
int floor = no/100 ;
int room = no%10 ;
if (floor-1 < rooms.length && room < rooms[floor-1].length) {
if (rooms[floor - 1][room - 1].getStatus()) {
System.out.println("房间是空的,请重新选择需要退的房间");
} else {
rooms[floor - 1][room - 1].setStatus(true);
System.out.println("退房成功");
}
}
else{
System.out.println("房间号不存在,请重新输入");
}
}
// 查询所有房间的状态
public void roomReserch(){
for (int i = 0; i < rooms.length; i++) {
for (int j = 0; j < rooms[i].length; j++) {
System.out.print(rooms[i][j].getNo() + " " + rooms[i][j].getType() + " ");
if(rooms[i][j].getStatus()){
System.out.print("空闲 ");
}
else{
System.out.print("使用 ");
}
}
System.out.println("");
}
}
}
3、最后是建立操作系统类
考虑到操作的连续性,通过while(true)实现,加上swith方法就可以实现对不同输入的分类处理。
import java.util.Scanner;
public class HotelSystem {
public static void main(String[] args) {
Hotel hotel = new Hotel();
while(true){
System.out.println("请选择要执行的操作:");
System.out.println("0:退出 1:订房 2:退房 3:查询房间状态");
Scanner s = new Scanner(System.in);
int i = s.nextInt();
switch (i){
case 0:
return;
case 1:
Scanner s2 = new Scanner(System.in);
int num = s2.nextInt();
hotel.roomOrder(num);
continue;
case 2:
Scanner s3 = new Scanner(System.in);
int num2 = s3.nextInt();
hotel.roomOut(num2);
continue;
case 3:
hotel.roomReserch();
continue;
default:
System.out.println("您输入的数据不符合标准,请重新输入");
continue;
}
}
}
}