一、题目
使用二维数组实现酒店管理系统。功能如下:
1.查看酒店所有房间的状态
2.预定房间
3.退房
4.退出系统
二、程序
1.Hotel.java
package Hotel;public class Hotel {private int id;private String standard;private String status;public Hotel() {}public Hotel(int id, String standard, String status) {this.id = id;this.standard = standard;this.status = status;}public Hotel(String standard, String status) {this.standard = standard;this.status = status;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getStandard() {return standard;}public void setStandard(String standard) {this.standard = standard;}public String getStatus() {return status;}public void setStatus(String status) {this.status = status;}@Overridepublic String toString() {return "[" + id + "," + standard + "," + status + "]" ;}@Overridepublic boolean equals(Object obj) {if(obj == null){return false;}if(this == obj){return true;}if(obj instanceof Hotel){Hotel h = (Hotel)obj;return h.getStatus().equals(this.getStatus());}return false;}
}
2.manager.java
package Hotel;public class manager {private Hotel[][] hotel = new Hotel[7][11];public manager(){for(int i = 0;i < 2; i ++){for(int j = 0;j < 10;j++){hotel[i+1][j+1] = new Hotel(100 * (i+1) +j+1,"单人间","空闲");}}for(int i = 2;i < 4;i++){for(int j = 0;j<10;j++){hotel[i+1][j+1] = new Hotel(100 * (i+1) +j+1,"标准间","空闲");}}for(int i = 4;i < 6;i++){for(int j = 0;j<10;j++){hotel[i+1][j+1] = new Hotel(100 * (i+1) +j+1,"豪华间","空闲");}}}public void display(){for(int i = 1;i<7;i++){for(int j = 1;j<11;j++){System.out.print(hotel[i][j]);}System.out.println();}}public void book(int num){for(int i = 1;i<7;i++) {for (int j = 1; j < 11; j++) {if (num == hotel[i][j].getId()){if(hotel[i][j].getStatus().equals("占用")){System.out.println("不好意思,您预定的房间已经被占用,请预定别的房间!");return;}hotel[i][j].setStatus("占用");System.out.println("预定房间成功!");}}}}public void unbook(int num){for(int i = 1;i<7;i++) {for (int j = 1; j < 11; j++) {if (num == hotel[i][j].getId()){hotel[i][j].setStatus("空闲");System.out.println("退房成功!");}}}}
}
3.test.java
package Hotel;
import java.util.Scanner;
public class test {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);manager manager = new manager();manager.display();while(true){System.out.println("欢迎来到酒店管理系统~");System.out.println("[1]查看所有房间状态");System.out.println("[2]预定房间");System.out.println("[3]退房");System.out.println("[0]退出系统");System.out.print("请输入不同的数字来选择对应的功能:");int menu = scanner.nextInt();switch (menu){case 0 -> System.exit(0);case 1 -> {manager.display();}case 2 ->{System.out.print("请输入预定的房间编号:");int booknum = scanner.nextInt();manager.book(booknum);}case 3 ->{System.out.print("请输入退房编号:");int unbooknum = scanner.nextInt();manager.unbook(unbooknum);}}}}
}
三、小结
本实例较简单,用的是二维数组的方法实现的简单的酒店管理系统。