编写一个Java项目:
为某个酒店编写程序:酒店管理系统,模拟订房、退房、打印所有房间状态等功能。
问题描述:
为某个酒店编写程序:酒店管理系统,模拟订房、退房、打印所有房间状态等功能。
1、该系统的用户是:酒店前台。
2、酒店使用一个二维数组来模拟。“Room[][] rooms;”
3、酒店中的每一个房间应该是一个java对象:Room
4、每一个房间Room应该有:房间编号、房间类型、房间是否空闲.
5、系统应该对外提供的功能:
可以预定房间:用户输入房间编号,订房。
可以退房:用户输入房间编号,退房。
可以查看所有房间的状态:用户输入某个指令应该可以查看所有房间状态。
酒店管理系统:模拟订房、退房、打印所有房间状态的功能
功能需求:
1,订房:用户可以输入房间编号可以订房
2,退房:用户可以输入房间编号进行退房
3,查看所有房间的状态:用户输入某些指令查看所有房间的状态
问题分析:
定义Room和Hotel类,Hotel里面包含一个Room类的二维数组用来模拟每一个不同楼层的房间号。依次实现相应要求。
代码实现:
Room类:
public class Room {
//房间编号
private int no;
//房间类型
private String type;
//房间状态,true表示可以被预定,false表示不可被预定
private boolean status;
public int getNo() {
return no;
}
//构造方法
public Room() {
}
public Room(int no, String type, boolean status) {
this.no = no;
this.type = type;
this.status = status;
}
//封装
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;
}
//toString方法重写
@Override
public String toString() {
return "[" + no + "," + type + "," + (status ? "可预订":"不可预定") + "]";
}
//equals方法重写
@Override
public boolean equals(Object obj) {
if(obj == null || !(obj instanceof Room)) return false;//传来的为空或不是Room类直接返回false
if (this == obj) return true;//如果内存地址一样,直接返回true
Room r = (Room)obj;//强制类型转换
return this.getNo() == r.getNo();
}
}
Hotel类:
public class Hotel {
private Room[][] rooms;
//构造方法
public Hotel() {
//一共三层,一层单人间,二层双人间,三层豪华间
//动态初始化
rooms = new Room[4][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);
else if (i == 3) 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++) {
System.out.print(rooms[i][j]);
}
System.out.println();
}
}
//订房间方法
public void order(int roomNo){
//根据房间编号计算在数组中的坐标
Room room = rooms[roomNo / 100 - 1][roomNo % 10 - 1];
room.setStatus(false);
System.out.println(roomNo + "房间,已成功预订!");
}
//退房
public void exit(int roomNo){
Room room = rooms[roomNo / 100 -1][roomNo % 10 - 1];
room.setStatus(true);
System.out.println(roomNo + "房间,已成功退房!");
}
}
HotelMgtSystem类:
import java.util.Scanner;
public class HotelMgtSystem {
public static void main(String[] args) {
Hotel h = new Hotel();
System.out.println("欢迎使用酒店管理系统,请认真阅读以下使用说明:");
System.out.println("请输入对应的功能编号:[1]表示查看房间状况;[2]表示订房;[3]表示退房;[0]表示退出");
System.out.println("请输入功能编号:");
while (true) {
Scanner s = new Scanner(System.in);
int i = s.nextInt();
if (i == 1) {
h.print();
} else if (i == 2) {
System.out.println("请输入要预定的房间编号:");
Scanner n = new Scanner(System.in);
int roomNo = n.nextInt();
h.order(roomNo);
} else if (i == 3) {
System.out.println("请输入要退房的房间编号:");
Scanner n = new Scanner(System.in);
int roomNo = n.nextInt();
h.exit(roomNo);
} else if (i == 0) {
System.out.println("欢迎下次光临");
return;
} else System.out.println("输入的功能编号有误,请重新输入:");
}
}
}
运行结果为:
更多知识关注微信公众号“51学代码”