使用簡單的Java程式碼實現酒店管理系統

小泥鰍戲水發表於2021-01-04

我們通過學習Java基礎知識,讓自己正式踏入學習Java語言的行列,這篇部落格是用來讓我們真正的瞭解並應用物件導向的思想來實現的。

使用簡單的Java程式碼實現酒店管理系統

1.需求分析

我們如果要實現酒店管理系統,就需要先對酒店管理系統的業務要求進行分析:

  1. 酒店管理系統需要實現哪些功能?
    (1)輸入某個命令查詢酒店中的所有房間;
    (2)輸入某個房間的編號訂房;
    (3)輸入某個房間的編號退房;
    (4)輸入某個命令可以退出酒店管理系統;
  2. 酒店管理系統使用什麼資料結構來表示房間?
    (1)酒店管理系統使用陣列的形式來儲存房間,這裡我們用二維陣列來表示;
  3. 酒店的房間我們都有什麼屬性?
    (1)房間編號;
    (2)房間型別;
    (3)房間是否空閒;
  4. 我們用什麼來控制房間類有上面這些屬性?
    (1)我們可以使用自定義註解來實現;

2.畫圖分析

酒店管理系統分析圖片

3.程式碼實現

/**
	自定義註解類:該類作用在房間類上。
*/
@Retention(RUNTIME)		// 用該註解表示此自定義註解可以被外部對映到
@Target(TYPE)			// 用該註解表示此自定義註解只可以作用在類上
public @interface ProperyAnnotation {}
/**
	當自定義註解類作用在房間實體類上時,該類是用來判斷房間實體類上是否有:房間編號、房間型別、房間是否空閒這些屬性的。
*/
public class ProperyAnnotationToJudge {

	// 判斷ProperyAnnotation註解的某個類中是否有某些屬性的方法
	public static void Judge () {
		try {
			// 使用反射機制來反射Room類
			// 我這裡的房間Room類放到了test包下,您可以自定義它的路徑。
			Class<?> c = Class.forName("test.Room");
			// 判斷Room類上是否存在@ProperyAnnotation註解
			if (c.isAnnotationPresent(ProperyAnnotation.class)) {
				// 如果Room類上存在@ProperyAnnotation註解就獲取這個Room類中的全部註解
				Field[] fields = c.getFields();
				boolean isExist = false;
				for (Field field : fields) {
					// 如果Room類中存在房間編號、房間型別、房間是否空閒這些屬性,就讓isExist=true,否則丟擲異常
					if ("rId".equals(field.getName()) && "Integer".equals(field.getType().getSimpleName())) {
						isExist = true;
						break;
					}
				}
				if (!isExist) {
					throw new ProperyException("Room類中不存在房間編號、房間型別、房間是否空閒這些屬性");
				}
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}
/**
	當ProperyAnnotation註解作用的房間實體類中沒有房間編號、房間型別、房間是否空閒這些屬性時,此類為丟擲的異常類。
*/
public class ProperyException extends RuntimeException {
	private static final long serialVersionUID = -8343113740914228496L;
	public ProperyException () {}
	public ProperyException (String msg) {super(msg);}
}
/**
	Room房間實體類
*/
@ProperyAnnotation
public class Room {
	private Integer rId;	// 房間編號
	private String rType;	// 房間型別
	private Boolean rFree;	// 房間是否空閒
	public Room () {}
	public Room (Integer rId, String rType, Boolean rFree) {
		this.rId = rId;
		this.rType = rType;
		this.rFree = rFree;
	}
	protected Integer getrId() {
		return rId;
	}
	protected void setrId(Integer rId) {
		this.rId = rId;
	}
	protected String getrType() {
		return rType;
	}
	protected void setrType(String rType) {
		this.rType = rType;
	}
	protected Boolean getrFree() {
		return rFree;
	}
	protected void setrFree(Boolean rFree) {
		this.rFree = rFree;
	}
	@Override
	public String toString() {
		return "Room [" + rId + ", " + rType + ", "+ (rFree ? "有人入住" : "無人入住")+"]";
	}
}
/**
	酒店管理實體類:用來管理房間的,其中包括檢視所有房間狀態、訂房、退房功能。
*/
public class Hotel {

	// 這裡需要定義一個二維陣列來表示房間,因為我們設想的酒店有很多層,且每層有很多你發件。
	private static Room[][] rooms;
	
	public Hotel () {
		// 這裡定義酒店為5層,且每層有9個房間
		rooms = new Room[5][9];
		// 這裡我們來設定酒店的房間,由於酒店的房間很多,所以我們使用for迴圈來分別設定每個樓層。
		for (int m = 0 ; m < rooms.length ; m ++) {
			for (int n = 0 ; n < rooms[m].length ; n ++) {
				// 第一層
				if (m == 0) {
					/*
						這裡我們的房間編號這樣設定:
						如果是是酒店的第一層樓的第一個房間,我們將房間編號設定成:101
						我規定我們的酒店的樓層為1~5層;
						我規定我們的酒店的第一個房間為1
						所以如果我們用二維陣列來表示酒店的樓層和第幾個房間時,因為我們的二維陣列的橫縱座標都是從0開始的,所以我們需要分別加上1,此時房間編號的表示式就為:
						(m + 1) * 100 + n + 1
						當m = 0時:
							n = 0:房間編號為101;
							n = 1:房間編號為102;
							n = 2;房間編號為103;
							...
						當m = 1時:
							n = 0:房間編號為201;
							n = 1:房間編號為202;
							...
						...
					*/
					rooms[m][n] = new Room((m + 1) * 100 + n + 1, "單人豪華房", false);
				}
				
				// 第二層
				if (m == 1) {
					rooms[m][n] = new Room((m + 1) * 100 + n + 1, "雙人豪華房", false);
				}
				
				// 第三層
				if (m == 2) {
					rooms[m][n] = new Room((m + 1) * 100 + n + 1, "三人豪華房", false);
				}
				
				// 第四層
				if (m == 3) {
					rooms[m][n] = new Room((m + 1) * 100 + n + 1, "三人豪華房", false);
				}
				
				// 第五層
				if (m == 4) {
					rooms[m][n] = new Room((m + 1) * 100 + n + 1, "三人豪華房", false);
				}
			}
		}
	}
	
	// 檢視所有房間狀態
	public void queryAllRooms () {
		for (int m = 0 ; m < rooms.length ; m ++) {
			for (int n = 0 ; n < rooms[m].length ; n ++) {
				System.out.println(rooms[m][n].toString());
			}
		}
	}
	
	// 使用房間編號訂房
	public void makeRoom (int rId) {
		Room room = rooms[rId / 100 - 1][rId % 100 - 1];
		// 如果該編號的房間已經有人訂了
		if (room.getrFree() == true) {
			System.out.println("抱歉,請您訂購其他房間,此房間已經有人居住!");
		} else {
			room.setrFree(true);
			System.out.println("訂房完成");
		}
	}
	
	// 使用房間編號退房
	public void existRoom (int rId) {
		Room room = rooms[rId / 100 - 1][rId % 100 - 1];
		// 如果該編號的房間本來就沒有人居住
		if (room.getrFree() == false) {
			System.out.println("抱歉,請您退訂其他房間,該房間沒有人居住不需要退訂!");
		} else {
			room.setrFree(false);
			System.out.println("退房完成");
		}
	}
}
/**
	酒店的操作測試類:
*/
public class Test {
	public static void main (String[] args) {
		ProperyAnnotationToJudge.Judge();
		Hotel hotel = new Hotel();
		System.out.println("歡迎使用酒店管理系統,請認真閱讀以下使用說明:");
		System.out.println("請輸入對應的功能編號:[1]檢視房間列表; [2]訂房; [3]退房; [0]退出系統");
		Scanner scanner = new Scanner(System.in);
		while (true) {
			System.out.print("請輸入功能編號:");
			Integer i = scanner.nextInt();
			if (i == 1) {
				hotel.queryAllRooms();
				System.out.println("酒店所有的房間已經載入完畢!");
			}
			else if (i == 2) {
				System.out.print("請輸入房間編號,房間編號為101~110、201~210、301~310、401~410、501~510:");
				Integer rId = scanner.nextInt();
				if (rId >= 101 && rId <= 110 || rId >= 201 && rId <= 210 || rId >= 301 && rId <= 310 || rId >= 401 && rId <= 410 || rId >= 501 && rId <= 510) {
					hotel.makeRoom(rId);
				} else {
					System.out.println("請輸入正確的房間編號!");
				}
			}
			else if (i == 3) {
				System.out.print("請輸入房間編號,房間編號為101~110、201~210、301~310、401~410、501~510:");
				Integer rId = scanner.nextInt();
				if (rId >= 101 && rId <= 110 || rId >= 201 && rId <= 210 || rId >= 301 && rId <= 310 || rId >= 401 && rId <= 410 || rId >= 501 && rId <= 510) {
					hotel.existRoom(rId);
				} else {
					System.out.println("請輸入正確的房間編號!");
				}
				
			}
			else if (i == 0) {
				System.out.println("成功退出酒店管理系統!");
				scanner.close();
				return;
			}
			else {
				System.out.println("請仔細閱讀使用說明,輸入正確的功能編號");
			}
		}
	}
}

輸出結果:在這裡插入圖片描述
本文有一個自定義註解的判斷有沒有房間編號、房間型別、房間是否空閒這些屬性的類具體沒有完成!(本文只判斷了一個房間編號,以後改!)

相關文章