首先创建一个房间类Room
package com.longya.HOTEL;
public class Room
{
private int no;
private String type;
private boolean occupancy;
public Room(int no, String type, boolean occupancy)
{
this.no = no;
this.type = type;
this.occupancy = occupancy;
}
//重写toString()——按照规定格式输出房间信息
public String toString()
{
String str = occupancy ? "占用" : "空闲";
return "[no=" + no + ",房型=" + type + ",状态=" + str + "]";
}
//setter/getter
public int getNo(){ return no;}
public void setNo(int no){ this.no = no;}
public String getType(){ return type;}
public void setType(String type){ this.type = type;}
public boolean getOccupancy(){ return occupancy;}
public void setOccupancy(boolean occupancy){ this.occupancy = occupancy;}
}
然后,创建一个旅馆类Hotel
package com.longya.HOTEL;
public class Hotel
{
Room[][] rooms;
public Hotel(){
rooms = new Room[5][9]; //默认创建5层楼,每层9个房间
//给所有房间编号
for (int i = 0; i < rooms.length; i++)
{
for (int j = 0; j < rooms[i].length; j++)
{
/*
房间分布设置:1楼、2楼为标准间,3楼为单人间,4楼为情侣间,5楼为豪华间
*/
if (i == 0 || i == 1)
{
rooms[i][j] = new Room((i + 1) * 100 + j + 1, "标准间", false);
}else if (i == 2)
{
rooms[i][j] = new Room((i + 1) * 100 + j + 1, "单人间", false);
}else if (i == 3)
{
rooms[i][j] = new Room((i + 1) * 100 + j + 1, "情侣间", false);
}else if (i == 4)
{
rooms[i][j] = new Room((i + 1) * 100 + j + 1, "豪华间", false);
}
}
}
}
//显示所有房间信息
public void display()
{
for (int i = 0; i < rooms.length; i++)
{
for (int j = 0; j < rooms[i].length; j++)
{
if (j % 3 == 0)
{
System.out.println(); //每行显示三个房间的信息
}
System.out.print(rooms[i][j] + "\t");
}
System.out.println();
}
}
//订房
public void reserve(int no)
{
for (int i = 0; i < rooms.length; i++)
{
for (int j = 0; j < rooms[i].length; j++)
{
if (rooms[i][j].getNo() == no)
{
rooms[i][j].setOccupancy(true);
System.out.println("订房成功!您预定的房间编号为:" + no);
return;
}
}
}
}
//退房
public void checkout(int no)
{
for (int i = 0; i < rooms.length; i++)
{
for (int j = 0; j < rooms[i].length; j++)
{
if (rooms[i][j].getNo() == no)
{
rooms[i][j].setOccupancy(false);
System.out.println("您已成功退掉[" + no + "]房间!");
return;
}
}
}
}
//根据房间编号,获取房间的占用情况occupancy
public boolean getRoomOccupancy(int no)
{
for (int i = 0; i < rooms.length; i++)
{
for (int j = 0; j < rooms[i].length; j++)
{
if (rooms[i][j].getNo() == no)
{
return rooms[i][j].getOccupancy();
}
}
}
return false;
}
}
最后,创建一个测试类HotelTest
package com.longya.HOTEL;
import java.util.*;
public class HotelTest
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("-------------------------------------------------- 酒店管理系统 -------------------------------------------------");
Hotel hotel = new Hotel();
hotel.display(); //显示所有房间信息
while(true)
{
try
{
//订房
System.out.println();
System.out.print("请输入您想要入住的房间编号:");
int no = sc.nextInt();
if (hotel.getRoomOccupancy(no))
{
System.out.println("该房间已经有人入住!请重新预定!");
}else{
hotel.reserve(no);
hotel.display();
}
//退房
System.out.println();
System.out.print("请输入您想要退房的房间编号:");
no = sc.nextInt();
if (hotel.getRoomOccupancy(no))
{
hotel.checkout(no);
hotel.display();
}else{
System.out.println("该房间原本就无人入住!请重新输入!");
}
}
catch (Exception e)
{
System.out.println("输入数据有误!请重新进入系统!"); return;
}
}
}
}
运行结果如下:
至此程序结束!