- 房间类
- 酒店类
- 测试类
房间类
class Room{
private String number;
private String type;
private boolean isUse;
public Room(String number, String type, boolean isUse) {
super();
this.number = number;
this.type = type;
this.isUse = isUse;
}
public Room() {
super();
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public boolean isUse() {
return isUse;
}
public void setUse(boolean isUse) {
this.isUse = isUse;
}
public String toString(){
return "The room's number is "+number+", the room's type is "+type+", the room's mode is "+ (isUse? "占用":"空闲");
}
}
酒店类
class Hotel{
Room[][] rooms;
public Hotel() {
rooms=new Room[5][10];
for(int i=0;i<rooms.length;i++)
for(int j=0;j<rooms[i].length;j++)
{
if(i==1||i==0)
rooms[i][j]=new Room(String.valueOf(100*(i+1)+j+1),"标准间",false);
else if(i==2||i==3)
rooms[i][j]=new Room(String.valueOf(100*(i+1)+j+1),"双人间",false);
else if(i==4)
rooms[i][j]=new Room(String.valueOf(100*(i+1)+j+1),"豪华间",false);
}
}
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(String num){
for(int i=0;i<rooms.length;i++)
{
for(int j=0;j<rooms[i].length;j++)
{
if(rooms[i][j].getNumber().equals(num)){
if(rooms[i][j].isUse()==false)
{
rooms[i][j].setUse(true);
return;
}
else
{
System.out.println("很抱歉该房间已被预订或使用");
return;
}
}
}
}
System.out.println("未找到您想要预定的房间号,请检查所输入房间号是否正确");
return;
}
public void deorder(String num)
{
for(int i=0;i<rooms.length;i++)
{
for(int j=0;j<rooms[i].length;j++)
{
if(rooms[i][j].getNumber().equals(num)){
if(rooms[i][j].isUse()==true)
{
rooms[i][j].setUse(false);
return;
}
else{
System.out.println("该房间未被预订,不需退订,请检查您所输入房间号是否正确");
return;
}
}
}
}
System.out.println("未找到您想要退定的房间号,请检查所输入房间号是否正确");
return;
}
}
测试类
public class Test {
public static void main(String[] args){
System.out.println("欢迎使用酒店管理系统,酒店房间列表如下所示:");
Hotel hotel =new Hotel();
hotel.print();
System.out.println("请输入要预定的房间编号为:");
Scanner scanner =new Scanner(System.in);
String num=scanner.next();
hotel.order(num);
hotel.print();
System.out.println("请输入要退定的房间编号为:");
String num2=scanner.next();
hotel.deorder(num2);
hotel.print();
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e8f8269afc09fb1af1133f0d4bc724b5.png#pic_center)