B站老杜的一道小Java作业
为某个酒店编写程序:酒店管理系统,模拟订房、退房、打印所有房间状态等功能。
1、该系统的用户是:酒店前台。
2、酒店使用一个二维数组来模拟。“Room[][] rooms;”
3、酒店中的每一个房间应该是一个java对象:Room
4、每一个房间Room应该有:房间编号、房间类型、房间是否空闲.
下面是实现
package Array.Array;
import java.util.Scanner;
public class ArrayHomeWorkHotel {
public static void main(String[] args) {
//用二维数组来存储房间号
int floorCount; //记录楼高
int roomCount; //记录每层楼的房间数目
int type; //1表示大床房,2表示标间,3表示豪华标间
boolean quit = true;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入酒店楼高");
floorCount = scanner.nextInt();
//创建房间二维数组
Room[][] rooms = new Room[floorCount][];
//为每层楼分配房间数
for (int i = 0; i < floorCount; i++) {
System.out.println("请输入第" + (i + 1) + "层的房间数");
roomCount = scanner.nextInt();
rooms[i] = new Room[roomCount];
}
//对房间录入信息
for (int i = 0; i < floorCount; i++) {
System.out.println("请输入第" + (i + 1) + "层楼的房间类型");
System.out.println("1表示大床房,2表示标间,3表示豪华标间");
type = scanner.nextInt();
for (int j = 0; j < rooms[i].length; j++) {
int number = getRoomNumber(i + 1, j + 1);
if (type == 1) {
rooms[i][j] = new Room(number, "大床房");
}else if(type == 2){
rooms[i][j] = new Room(number, "标间");
}else{
rooms[i][j] = new Room(number, "豪华套房");
}
}
}
int choice;
while(quit){
System.out.println("请选择你的操作");
System.out.println("1:打印房间信息");
System.out.println("2:订房");
System.out.println("3:退房");
System.out.println("4:退出程序");
choice = scanner.nextInt();
switch(choice){
case 1:
Print(rooms);
break;
case 2:
System.out.println("请输入一个空闲的房间号");
int number1;
number1 = scanner.nextInt();
reservation(rooms,number1);
break;
case 3:
System.out.println("请输入一个空闲的房间号");
int number2;
number2 = scanner.nextInt();
depart(rooms,number2);
break;
case 4:
quit = false;
break;
default:
System.out.println("请重新输入你的选择");
}
}
System.out.println("Bye Bye");
}
//订房
private static void reservation(Room[][] rooms,int roomNumber){
//确定房间的位置
int floor = roomNumber / 100 - 1;
int room = roomNumber % 10 - 1;
if(rooms[floor][room].Available() == false){
System.out.println("请重新输入一个空闲的房间号");
return;
}
rooms[floor][room].setAvailable(false);
System.out.println(rooms[floor][room].toString());
}
private static void depart(Room[][] rooms,int roomNumber){
int floor = roomNumber / 100 - 1;
int room = roomNumber % 10 - 1;
if(rooms[floor][room].Available() == true){
System.out.println("请输入一个占有的房间号");
}
rooms[floor][room].setAvailable(true);
System.out.println(rooms[floor][room].toString());
}
private static void Print(Room[][] rooms){
for(int i = 0; i < rooms.length; i++){
for(int j = 0; j < rooms[i].length; j++){
System.out.println(rooms[i][j].toString());
}
}
}
//房间号生成方法
private static int getRoomNumber(int floor, int room){
return floor*100 + room;
}
}
class Room{
private int roomNumber;
private String roomType;
/* private static final String BIG_BED = "大床房"; // 大床房
private static final String TWIN_ROOM = "标间"; // 标间
private static final String PRESIDENTIAL_SUITE = "总统套房"; // 总统套房*/
private boolean available; //true表示空闲,false表示占用
//无参构造函数
public Room() {
}
//带参构造函数
public Room(int roomNumber, String roomType) {
this.roomNumber = roomNumber;
this.roomType = roomType;
this.available = true;
}
@Override
public String toString() {
if(available == true){
return "房号:" + roomNumber + " 房间类型:" + roomType + "房间状态:空闲" ;
}else{
return "房号:" + roomNumber + " 房间类型:" + roomType + "房间状态:有客" ;
}
}
//获取房间号
public int getRoomNumber() {
return roomNumber;
}
//获取房间类型
public String getRoomType() {
return roomType;
}
//写入房间是否空闲
public void setAvailable(boolean available) {
this.available = available;
}
//判断房间是否空闲
public boolean Available(){
return this.available;
}
}