二维数组模拟酒店订房系统

需求分析:显示房间状态、订房、退房。

需要三个类:测试类、酒店类、房间类

房间类有三个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;
			}
		}
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值