本帖最后由 蒋博1026 于 2019-12-23 09:03 编辑
/*
酒店客房管理系统
*/
import java.util.Scanner;//通过键盘来输入命令需要的引入
class HotelManangerSystem
{
public static void main(String[] args)
{
//酒店有12个楼层,每层有10个房间
//字符串数组如果没有被初始化,每个元素都是null
String[][] rooms = new String[12][10];
System.out.println("欢迎来到蒋氏集团大酒店");//换行打印
System.out.println("请输入要操作的命令:search查询,in办理入住,out退房"
+ "init初始化所有房间");
Scanner s = new Scanner(System.in);//这个在主函数定义的,其他函数用不了
while(true)
{
String command = s.next();//提示输入命令
//比较字符串相同--"输入的字符串".equals(command)
if("init".equals(command))
{
init(rooms);
System.out.println("房间初始化完毕");
}
else if("search".equals(command))
{
search(rooms);
}
else if("in".equals(command))
{
in(rooms);
}
else if("out".equals(command))
{
out(rooms);
}
}
}
//初始化所有的房间号
public static void init(String[][] rooms)
{
for(int i = 0; i < 12; i++)
{
for(int j = 0; j < 10;j++)
{
rooms[i][j] = "EMPLY";
}
}
}
public static void search(String[][] rooms)
{
for(int i = 0; i < rooms.length; i++)
{
for(int j = 0; j < rooms[i].length;j++)
{
if(i < 9)
{
System.out.print("0");
}
int roomNum = (i + 1) * 100 + j + 1;
System.out.print(roomNum + "\t");
}
System.out.println();
for(int k = 0; k < rooms[i].length; k++)
{
System.out.print(rooms[i][k] + "\t");
}
System.out.println();
}
}
public static void in(String[][] rooms)
{
System.out.println("请输入房间号:");
Scanner s = new Scanner(System.in);
int roomNum = s.nextInt();
int i = roomNum / 100;
int j = roomNum % 100;
if(i < 1 || i > 10 || j < 1|| j > 12)
{
System.out.println("输入错误,请重新输入in:");
return;
}
if(rooms[i-1][j-1] != "EMPLY")
{
System.out.println("此房间已经有人入住,请重新输入in:");
return;
}
System.out.println("请输入您的姓名:");
String name = s.next();
rooms[i-1][j-1] = name;
System.out.println("入住成功");
}
public static void out(String[][] rooms)
{
System.out.println("请输入房间号:");
Scanner s = new Scanner(System.in);
int roomNum = s.nextInt();
int i = roomNum / 100;
int j = roomNum % 100;
if(i < 1 || i > 10 || j < 1|| j > 12)
{
System.out.println("输入错误,请重新输入out:");
return;
}
if(rooms[i-1][j-1] == "EMPLY")
{
System.out.println("输入错误,请重新输入out:");
return;
}
if(rooms[i-1][j-1] != "EMPLY")
{
rooms[i-1][j-1] = "EMPLY";
System.out.println("退房成功");
}
}
}
最近初次接触JAVA,感觉之前学的C语言很有用,跟着视频做了一个小项目-酒店客房管理系统
|