资源描述
package Test;
import java.util.Scanner;
public class Hotel {
//设置一个宾馆 是10层楼 每层楼十个房间
static String[][] hotel = new String[10][10];
//创建一个scanner工具,用来和用户交互
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("欢迎光临,香格里拉饭店");
System.out.println("请问您需要什么样的服务");
//这个for循环 将宾馆的房间号码和房间的状态创建出来
for (int i = 1; i <= hotel.length; i++) {
for (int j = 1; j <= hotel[i - 1].length; j++) {
hotel[i - 1][j - 1] = i * 100 + j + "空";
}
}
//因为客户 需要服务的次数是不确定的所以要进行死循环
for (;;) {
//command变量:得到用户从控制台输入的命令
//根据command的命令去执行相应的操作
String command = scanner.nextLine();
//如果说输入的命令是查询的话
if (command.equals("select")) {
select();
//如果说输入的是clear清屏命令的话
} else if (command.equals("clear")) {
clear();
//如果说command命令的是入住的话
} else if (command.equals("in")) {
in();
//如果说输入的command命令是 退房的话
} else if (command.equals("out")) {
out();
//如果command命令是“换房”的话
}else if(command.equals("change")){
change();
}
}
}
private static void change() {
System.out.println("请问您之前的房间号码是?");
String beforeNum=scanner.nextLine();
String beforeNumStatus="";
System.out.println("请问您要换到那个房间去?");
String afterNum=scanner.nextLine();
String afterNumStatus="";
for(int i=0;i<hotel.length;i++){
for(int j=0;j<hotel[i].length;j++){
String str = hotel[i][j];
String newNum=str.substring(0, str.length()-1);
if(newNum.equals(beforeNum)){
beforeNumStatus=str.substring(str.length()-1,str.length());
}
}
}
for(int i=0;i<hotel.length;i++){
for(int j=0;j<hotel[i].length;j++){
String str= hotel[i][j];
String newNum=str.substring(0, str.length()-1);
if(newNum.equals(afterNum)){
afterNumStatus=str.substring(str.length()-1, str.length());
}
}
}
if(beforeNumStatus.equals("满")&&afterNumStatus.equals("空")){
for(int i=0;i<hotel.length;i++){
for(int j=0;j<hotel[i].length;j++){
String str = hotel[i][j];
String newNum=str.substring(0, str.length()-1);
if(newNum.equals(beforeNum)){
hotel[i][j]=str.replaceAll("满", "空");
}
}
}
for(int i=0;i<hotel.length;i++){
for(int j=0;j<hotel[i].length;j++){
String str = hotel[i][j];
String newNum=str.substring(0, str.length()-1);
if(newNum.equals(afterNum)){
hotel[i][j]=str.replaceAll("空", "满");
}
}
}
System.out.println("恭喜您!换房成功!!!");
}else if(beforeNumStatus.equals("空")){
System.out.println("请顾客确认您的房间号码");
}else if(beforeNumStatus.equals("满")&&afterNumStatus.equals("满")){
System.out.println("对不起顾客!您要换的那个房间已经有人住了");
System.out.println("请您选择其他的房间");
}
}
private static void out() {
//询问顾客要退的房间号码
System.out.println("请问您要退的房间号码是?");
//接收顾客要退的房间号码
String outNumber = scanner.nextLine();
for (int i = 0; i < hotel.length; i++) {
for (int j = 0; j < hotel[i].length; j++) {
//运用循环 查找每一层楼的每一个房间的号码
//是否和 顾客要退的房间号码匹配
String hotleNumber = hotel[i][j];
//截取房间号码,去掉房间的状态信息
String newHotelNum = hotleNumber.substring(0,
hotleNumber.length() - 1);
//如果说房间号码和顾客要退的房间号码匹配
if (newHotelNum.equals(outNumber)) {
//截取顾客的房间状态
String outStaus = hotleNumber.substring(hotleNumber
.length() - 1, hotleNumber.length());
//如果房间状态是空的话
if (outStaus.equals("空")) {
//提示相应的错误信息
System.out.println("不好意思顾客,请您确认你的房间号码");
} else {
//否则 更改房间的状态信息
hotel[i][j] = hotleNumber.replace("满", "空");
//提示 退房成功
System.out.println("感谢您的光临,欢迎您下次惠顾 baybay");
}
}
}
}
}
private static void in() {
//询问顾客的名字
System.out.println("请问您的名字?");
//接收顾客的名字
String name = scanner.nextLine();
//询问顾客的入住的天数
System.out.println("请问你要入住多少天?");
//接收顾客的入住的天数
String days = scanner.nextLine();
//询问顾客的入住的房间号码
System.out.println("请问你要入住的房间号码");
////接收顾客的入住的房间号码
String number = scanner.nextLine();
for (int i = 0; i < hotel.length; i++) {
for (int j = 0; j < hotel[i].length; j++) {
//运用循环 查找每一层楼的每一个房间的号码
//是否和 顾客入住的房间号码 匹配
String numbers = hotel[i][j];
//将房间的状态去掉,只留下房间的号码
String newNumber = numbers.substring(0, numbers
.length() - 1);
//如果说 房间号码匹配的话
if (newNumber.equals(number)) {
//截取房间的状态
String staus = numbers.substring(
numbers.length() - 1, numbers.length());
//如果说当前的那个房间的状态是“满”的话
if (staus.equals("满")) {
//输出相应的错误信息,提示给客户
System.out.println("不好意思顾客,此房间已经有顾客入住");
System.out.println("请选择其他房间");
} else {
//否则将房间的状态 从“空”变成“满”
hotel[i][j] = hotel[i][j].replace("空", "满");
//输出入住成功信息
System.out.println("恭喜您 你可以入住啦");
}
}
}
}
}
private static void clear() {
//在控制台上打印50个回车!!!!
for (int i = 0; i < 50; i++) {
System.out.println();
}
}
public static void select() {
//将宾馆的每一层楼的每一个房间的号码和状态显示出来
for (int i = 0; i < hotel.length; i++) {
for (int j = 0; j < hotel[i].length; j++) {
//这个判断的作用就是为了 第十层楼和其他九层楼的格式对齐
if (i == 9) {
System.out.print(hotel[i][j] + " ");
} else {
System.out.print(" " + hotel[i][j] + " ");
}
}
//每一层楼 加一个回车换行
System.out.println();
}
}
}
展开阅读全文