酒店管理系统
为某个酒店编写程序:酒店管理系统,模拟订房、退房、打印所有房间状态等功能
1.该系统的用户是:酒店前台
2.酒店使用一个二维数组来模拟“Room[][] rooms”
3.九点钟使用的每一个房间应该是一个java对象:Room
4.每个房间Room应该有:房间编号,房间类型,房间是否空闲
5.系统应该对外提供的功能:
可以预订房间:用户输入房间编号,订房
可以退房:用户输入房间编号,退房
可以查看所有房间的状态:用户输入某个指令应该可以查看所有房间状态
Room房间类:
package array.homework;
public class Room {
//编号:一楼:101,102.。。
private int no;
//房间类型:标准间,单人间,总统套房
private String type;
//房间状态:true空闲,false占用
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;
}
//IDEA工具对于boolean类型的变量,生成get方法的方法名是:isXxx()
//不习惯可以修改为getXxx()
public boolean getStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
//重写equals方法
public boolean equals(Object obj){
if(obj == null || !(obj instanceof Room)) return false;
if (this == obj) return true;
Room room = (Room)obj;
return this.no == room.getNo();
}
//重写toString方法
public String toString(){
return "[" + no + "," + type + "," + (status ? "空闲" : "占用") + "]";
}
//编写一个临时程序测试
public static void main(String[] args){
Room room = new Room(101,"单人间",true);
System.out.println(room);
}
}
Hotel酒店对象:
package array.homework;
/*
酒店对象:酒店中有二维数组,二维数组模拟大厦
*/
public class Hotel {
//二维数组:模拟大厦所有房间
private Room[][] rooms;//null
//盖楼通过构造方法
public Hotel() {
//共有几层,每层的房间类型和编号各是什么
rooms = new Room[3][10];//3层楼,每层10个房间
//创建30个Room对象,放到数组中
//二维数组遍历
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 Hotel(Room[][] rooms) {
this.rooms = rooms;
}
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++){
Room room = rooms[i][j];
System.out.print(room);
}
System.out.println();
}
}
//订房:传递一个房间编号
public void order(int roomNo){
//主要是将Room对象的status修改为false
Room room = rooms[roomNo / 100 -1][roomNo %100 -1];
room.setStatus(false);
System.out.println(roomNo + "已预订成功!");
}
//退房
public void exit(int roomNo){
Room room = rooms[roomNo / 100 -1][roomNo %100 -1];
room.setStatus(true);
System.out.println(roomNo +"已退房!");
}
}
main主方法:
package array.homework;
import java.util.Scanner;
public class HotelMgtSystem {
public static void main(String[] args) {
Hotel hotel = new Hotel();
//打印房间状态
//hotel.print();
//欢迎页面
System.out.println("欢迎使用酒店管理系统");
System.out.println("请输入您所需要的的服务:【1】查看房间列表;【2】订房;【3】退房;【0】退出系统");
Scanner s = new Scanner(System.in);
while (true) {
System.out.print("请输入服务编号:");
int i = s.nextInt();
if (i == 1) {
//查看房间
hotel.print();
} else if (i == 2) {
System.out.print("请输入预定房间编号:");
int roomNo = s.nextInt();
hotel.order(roomNo);
} else if (i == 3) {
System.out.print("请输入退房编号");
int roomNo = s.nextInt();
hotel.exit(roomNo);
} else if (i == 0) {
System.out.println("谢谢使用!");
return;
} else {
System.out.println("输入服务编号有误,请重新输入!");
}
}
}
}
运行结果展示: