Java学习日记-数组作业-酒店管理系统

1.自己编写一个程序,完成数组的拷贝

2.使用一维数组模拟栈这种数据结构

3.编写一个程序模拟酒店管理系统:显示酒店所有房间列表,预订房间,退房…

class Room{

String no;
String type; //"标准间" "双人间" “豪华间”
boolean isUse; //true表示占用,false表示空闲

}

class Hotel{

//规定酒店:5层,每层10个房间
//1,2层是标准间
//3,4层是双人间
//5 是豪华间

Room[][] rooms;

//无参数
Hotel(){
	this(5,10);
}

//有参数
Hotel(int rows,int cols){
	rooms = new Room[rows][cols];
	/*
	rooms[0][0]....
	rooms[4][9]....
	*/
}


//对外提供预订方法

//对外提供退房的方法

}

class Test{
main
}

以上为老师所提供的题干需求等

个人作业如下:

Hotel.java

package HotelMangement_System;

public class Hotel {
	
	//房间
	Room [][] rooms;
	
	//constructor
	
	Hotel(){
		
		//五层   每层十个房间
		rooms = new Room[5][10];
		
		//赋值
		//1,2层标准间
		//3 4层大床房
		//5层豪华间
		for(int i=0;i<rooms.length;i++){
			for(int u=0;u<rooms[i].length;u++){
				
				if(i==0||i==1){
					rooms[i][u] = new Room(((i+1)*100)+u+1+"","标准间",false);
				}
				if(i==2||i==3){
					rooms[i][u] = new Room(((i+1)*100)+u+1+"","大床房",false);
				}
				if(i==4){
					rooms[i][u] = new Room(((i+1)*100)+u+1+"","豪华间",false);
				}
			}
		}
	}
		//对外提供一个打印酒店列表的方法
		public void print(){
			
			for(int i=0;i<rooms.length;i++){
				
				for(int u=0;u<rooms[i].length;u++){
					System.out.print(rooms[i][u]+" ");
				}
				System.out.println();
			}
		}
		
		//对外提供一个预定酒店的方法
		public void order(String no){
			for(int i=0;i<rooms.length;i++){
				
				for(int u=0;u<rooms[i].length;u++){
					
					if(rooms[i][u].getNo().equals(no)){
						//将房间状态改写为占用
						rooms[i][u].setIsUse(true);
						return;
					}
				}
			}
		}
		//对外提供一个退房的方法
		public void out(String no){
			for(int i=0;i<rooms.length;i++){
				
				for(int u=0;u<rooms[i].length;u++){
					if(rooms[i][u].getNo().equals(no)){
						//将房间状态改写为空闲
						rooms[i][u].setIsUse(false);
						return;
					}
				}
			}
		}
	}

Room.java

package HotelMangement_System;
//酒店房间
public class Room {
	
	private String no;
	private String type;//标准间 大床房 豪华间
	private Boolean isUse;//false表示空闲,true则表示占用
	
	public Room(){
		super();
	}
	public Room(String no,String type,Boolean isUse){
		super();
		this.no = no;
		this.type = type;
		this.isUse = isUse;
	}
	

	public String getNo() {
		return no;
	}
	public void setNo(String no) {
		this.no = no;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public Boolean getIsUse() {
		return isUse;
	}
	public void setIsUse(Boolean isUse) {
		this.isUse = isUse;
	}
	public String toString(){
		return"["+no+","+type+","+(isUse?"占用":"空闲")+"]";
	}
}

Test.java

package HotelMangement_System;
import java.util.Scanner;
public class Test {
	
	class A{
		void fA(){
			
			Scanner s = new Scanner(System.in);
			
			Hotel h = new Hotel();
			
			System.out.print("请输入预订房间的编号:");
			
			String no = s.next();
			
			
			h.order(no);
			
			System.out.println("您所提交的房间"+no+"号房已预订成功,请携带身份证入住");
			
			h.print();
		}
	}
	
	class B{
		void fB(){
			
			Scanner s = new Scanner(System.in);
			
			Hotel h = new Hotel();
			
			System.out.print("请输入所需要退房房间的编号:");
			
			String no = s.next();
			h.out(no);
			
			System.out.println("您所提交的房间"+no+"号房已退房成功,欢迎下次光临!");
			
			h.print();
			
		}
	}
	
	public static void main(String[] args){
		
		Scanner s = new Scanner(System.in);
		
		System.out.println("欢迎使用酒店管理系统,酒店房间列表如下:");
		
		//初始化酒店
		Hotel h = new Hotel();
		
		//输出房间列表
		h.print();
		
		System.out.println("如需预订房间请输入1,退房请输入2。");
		
		String v = s.next();
		
		while(v.equals("1")){
			
			A a = new Test().new A();
			
			a.fA();
			
           
		}
		while(v.equals("2")){
			
			B b = new Test().new B();
			
			b.fB();
			
		}
		

}
}	

以上。

此作业仍有许多不完善之处,如:1.进行订房操作之后无法进行退房处理;
2.输入2进行退房操作时并不会审核房间在此之前有被占用;
3.程序缺乏灵活性;
后续仍会对此进行合理的改进。

发布了20 篇原创文章 · 获赞 0 · 访问量 273

猜你喜欢

转载自blog.csdn.net/weixin_43150344/article/details/103982044
今日推荐