/***
* 酒店的房间
*/
public class Room {
/**
* 房间编号:101...110;601...610
* */
private int roomNo;
/*
·单人间,标准间,豪华间
*/
private String type;
/**
* true表示占用
* */
private boolean state;
public Room(int roomNo, String type, boolean state) {
this.roomNo = roomNo;
this.type = type;
this.state = state;
}
public Room() {
}
public boolean isState() {
return state;
}
public void setState(boolean state) {
this.state = state;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public int getRoomNo() {
return roomNo;
}
public void setRoomNo(int roomNo) {
this.roomNo = roomNo;
}
@Override
public String toString() {
return "["+this.roomNo + "," + this.type + "," + (this.state ? "占用" : "空闲") + "]";
}
}
酒店
public class Hotel {
//6为楼层 10为每层有10个房间
private Room[][] rooms = new Room[6][10];//60个null
/*
构造方法:通过构造方法可以盖一个酒店
*/
public Hotel(){
for (int i = 0; i<rooms.length; i++){
for (int j = 0; j<rooms[i].length; j++){
if (i == 0 || i == 1){
//1 2 楼
rooms[i][j] = new Room((i + 1)*100 + j + 1 ,"单人间",false);
}else if (i == 2 || i == 3){
rooms[i][j] = new Room((i + 1)*100 + j + 1 ,"标准间",false);
}else if (i == 4 || i == 5){
rooms[i][j] = new Room((i + 1)*100 + j + 1 ,"豪华间",false);
}
}
}
}
public void order(int roomNo){
//101 i = 0; j = 0 ;
rooms[roomNo / 100 - 1][roomNo % 100 - 1 ].setState(true);
}
public void exit(int roomNo){
rooms[roomNo / 100 - 1][roomNo % 100 - 1 ].setState(false);
}
public void display(){
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();
}
}
}
酒店前台
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Hotel hotel = new Hotel();
System.out.println("欢迎使用酒店管理系统,请按以下操作对应输入:");
System.out.println("[1]查看所有房间状态");
System.out.println("[2]预定房间");
System.out.println("[3]退房");
System.out.println("[0]退出系统");
Scanner s = new Scanner(System.in);
while (true){
System.out.println("请输入功能编号:");
//获取功能编号
int no = s.nextInt();
switch (no){
case 1:
hotel.display();
break;
case 2:
System.out.println("请输入预定的房间号");
int roomNo = s.nextInt();
hotel.order(roomNo);
System.out.println("预定房间"+roomNo+"成功");
break;
case 3:
System.out.println("请输入退房的房间号");
int roomNo1 = s.nextInt();
hotel.exit(roomNo1);
System.out.println("退房"+roomNo1+"成功");
break;
case 0:
System.out.println("退出系统");
System.exit(0);
}
}
}
}