需求分析:显示房间状态、订房、退房。
需要三个类:测试类、酒店类、房间类
房间类有三个attribute:房间编号、房间类型、房间状态
酒店类三个operation:显示房间状态、订房、退房
public class Room
{
private int no;
private String type;
private boolean occupancy;
public int getNo(){
return no;
}
public void setNo(){
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;
}
public Room(int no,String type,boolean occupancy){
this.no = no;
this.type = type;
this.occupancy = occupancy;
}
public String toString(){
String str = occupancy ? "占用" : "空闲";
return "[no="+no+",房型="+type+",状态"+str+"]";
}
}
public class Hotel
{
Room[][] rooms;
public Hotel(){
rooms = new Room[3][9];//3层,每层9个房间
//给房间编号
for(int i = 0;i < rooms.length ;i++){
for(int j = 0;j < rooms[i].length;j++){
//一楼单人间,二楼标准间,三楼豪华间
if(i==0){
rooms[i][j] = new Room((i+1)*100+j+1,"单人间",false);
}
if(i==1){
rooms[i][j] = new Room((i+1)*100+j+1,"标准间",false);
}
if(i==2){
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){//每行显示3个房间信息
System.out.println();
}
System.out.print(rooms[i][j]+"\t");
}
}
}
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;
}
}
}
}
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();
}
}
}
System.out.println("您输入的房间不存在!");
return false;
}
}
import java.util.*;
public class Test
{
public static void main(String[] agrs){
Scanner input = new Scanner(System.in);
Hotel hotel = new Hotel();
int no;
System.out.println("-------------------欢迎来到XX酒店-------------------");
while(true){
System.out.println("请选择服务(输入数字):1、查询房间情况 2、预定房间 3、退房");
switch(input.nextInt()){
case 1:{
System.out.println("------------------- 酒店房间情况--------------------");
hotel.display();
break;
}
case 2:{
while(true){
try{
System.out.println("请输入您想要入住的房间编号");
no = input.nextInt();
if(hotel.getRoomOccupancy(no)){
System.out.println("对不起,该房间已有人入住,请重新选择!");
}else{
hotel.reserve(no);
break;
}
}catch(Exception e){
System.out.println("您输入的信息有误,请重新进入系统!");
}
}
break;
}
case 3:{
while(true){
try{
System.out.println("请输入您想退的房间号");
no = input.nextInt();
if(hotel.getRoomOccupancy(no)){
hotel.checkout(no);
break;
}else{
System.out.println("该房间无人入住,请重新输入!");
}
}catch(Exception e){
System.out.println("您输入的信息有误,请重新进入系统!");
}
}
break;
}
}
System.out.println("\n按任意键继续使用,按q退出");
if(input.next().charAt(0)=='q'){
break;
}
}
}
}