[java]代码库
import java.util.Scanner;
/**
* 旅店管理系统(控制台版)
*
*/
public class HotelManager {
private static String[][] rooms;// 表示房间
public static void main(String[] args) {
rooms = new String[10][12];
String comm;// 表示用户输入的命令
for (int i = 0; i < rooms.length; i++) {
for (int j = 0; j < rooms[0].length; j++) {
rooms[i][j] = "EMPTY";
}
}
while (true) {
System.out.println("欢迎光临【程序猿】旅店,请输入命令:");
Scanner sca = new Scanner(System.in);
System.gc();
comm = sca.next();
if ("search".equalsIgnoreCase(comm)) {
search();
} else if ("in".equalsIgnoreCase(comm)) {
int roomNo = sca.nextInt();
String name = sca.next();
in(roomNo, name);
} else if ("out".equalsIgnoreCase(comm)) {
int roomNo = sca.nextInt();
out(roomNo);
} else if ("exit".equalsIgnoreCase(comm)) {
System.out.println("程序退出...");
break;
} else if ("help".equalsIgnoreCase(comm)) {
System.out
.println(" in命令格式:in 房号 姓名\n out命令格式:out 房号 姓名\n search命令:查看所有房间状态 \n exit命令:退出");
} else {
System.out.println("命令输入错误,请重新输入:");
}
}
}
private static void out(int roomNo) {
if ("EMPTY".equals(rooms[(roomNo / 100) - 1][(roomNo % 100) - 1])) {
System.out.println("该房间没有客人入住,退房失败!");
return;
}
rooms[(roomNo / 100) - 1][(roomNo % 100) - 1] = "EMPTY";
System.out.println(roomNo + "退房成功!");
}
private static void in(int roomNo, String name) {
if (!"EMPTY".equals(rooms[(roomNo / 100) - 1][(roomNo % 100) - 1])) {
System.out.println("该房间已经有客人入住!");
return;
}
rooms[(roomNo / 100) - 1][(roomNo % 100) - 1] = name;
System.out.println(name + "成功入住" + roomNo + "房间!");
}
private static void search() {
for (int i = 0; i < rooms.length; i++) {
// 打印房间号
for (int j = 0; j < rooms[0].length; j++) {
if (j + 1 < 10) {
System.out.print(i + 1 + "0" + (j + 1) + " ");
} else {
System.out.print(i + 1 + "" + (j + 1) + " ");
}
}
// 打印房间状态
System.out.println();
for (int j = 0; j < rooms[0].length; j++) {
System.out.print(rooms[i][j] + " ");
}
System.out.println();
}
}
}
[代码运行效果截图]