Java代碼實(shí)現(xiàn)酒店管理系統(tǒng)
我們通過學(xué)習(xí)Java基礎(chǔ)知識,讓自己正式踏入學(xué)習(xí)Java語言的行列,這篇博客是用來讓我們真正的了解并應(yīng)用面向?qū)ο蟮乃枷雭韺?shí)現(xiàn)的。
使用簡單的Java代碼實(shí)現(xiàn)酒店管理系統(tǒng),供大家參考,具體內(nèi)容如下
一. 需求分析
我們?nèi)绻獙?shí)現(xiàn)酒店管理系統(tǒng),就需要先對酒店管理系統(tǒng)的業(yè)務(wù)要求進(jìn)行分析:
1.酒店管理系統(tǒng)需要實(shí)現(xiàn)哪些功能?
(1)輸入某個命令查詢酒店中的所有房間;
(2)輸入某個房間的編號訂房;
(3)輸入某個房間的編號退房;
(4)輸入某個命令可以退出酒店管理系統(tǒng);
2.酒店管理系統(tǒng)使用什么數(shù)據(jù)結(jié)構(gòu)來表示房間?
(1)酒店管理系統(tǒng)使用數(shù)組的形式來存儲房間,這里我們用二維數(shù)組來表示;
3.酒店的房間我們都有什么屬性?
(1)房間編號;
(2)房間類型;
(3)房間是否空閑;
4.我們用什么來控制房間類有上面這些屬性?
(1)我們可以使用自定義注解來實(shí)現(xiàn);
二. 畫圖分析
三. 代碼結(jié)構(gòu)
四. 代碼實(shí)現(xiàn)
/** ?? ?自定義注解類:該類作用在房間類上。 */ @Retention(RUNTIME)?? ??? ?// 用該注解表示此自定義注解可以被外部映射到 @Target(TYPE)?? ??? ??? ?// 用該注解表示此自定義注解只可以作用在類上 public @interface ProperyAnnotation {}
/** ?? ?當(dāng)自定義注解類作用在房間實(shí)體類上時,該類是用來判斷房間實(shí)體類上是否有:房間編號、房間類型、房間是否空閑這些屬性的。 */ public class ProperyAnnotationToJudge { ?? ?// 判斷ProperyAnnotation注解的某個類中是否有某些屬性的方法 ?? ?public static void Judge () { ?? ??? ?try { ?? ??? ??? ?// 使用反射機(jī)制來反射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 (item.getName().equals("rId") && item.getType().getSimpleName().equals("Integer")) { ?? ??? ??? ??? ??? ??? ?for (Field item1 : fields) { ?? ??? ??? ??? ??? ??? ??? ?if (item1.getName().equals("rType") && item1.getType().getSimpleName().equals("String")) { ?? ??? ??? ??? ??? ??? ??? ??? ?for (Field item2 : fields) { ?? ??? ??? ??? ??? ??? ??? ??? ??? ?if (item2.getName().equals("rFree") && item2.getType().getSimpleName().equals("Boolean")) { ?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?isExist = true; ?? ??? ??? ??? ??? ??? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ?} ?? ??? ??? ??? ?if (!isExist) { ?? ??? ??? ??? ??? ?throw new ProperyException("Room類中不存在房間編號、房間類型、房間是否空閑這些屬性"); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} catch (ClassNotFoundException e) { ?? ??? ??? ?e.printStackTrace(); ?? ??? ?} ?? ?} }
/** ?? ?當(dāng)ProperyAnnotation注解作用的房間實(shí)體類中沒有房間編號、房間類型、房間是否空閑這些屬性時,此類為拋出的異常類。 */ public class ProperyException extends RuntimeException { ?? ?private static final long serialVersionUID = -8343113740914228496L; ?? ?public ProperyException () {} ?? ?public ProperyException (String msg) {super(msg);} }
/** ?? ?Room房間實(shí)體類 */ @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 ? "有人入住" : "無人入住")+"]"; ?? ?} }
/** ?? ?酒店管理實(shí)體類:用來管理房間的,其中包括查看所有房間狀態(tài)、訂房、退房功能。 */ public class Hotel { ?? ?// 這里需要定義一個二維數(shù)組來表示房間,因?yàn)槲覀冊O(shè)想的酒店有很多層,且每層有很多你發(fā)件。 ?? ?private static Room[][] rooms; ?? ? ?? ?public Hotel () { ?? ??? ?// 這里定義酒店為5層,且每層有9個房間 ?? ??? ?rooms = new Room[5][9]; ?? ??? ?// 這里我們來設(shè)置酒店的房間,由于酒店的房間很多,所以我們使用for循環(huán)來分別設(shè)置每個樓層。 ?? ??? ?for (int m = 0 ; m < rooms.length ; m ++) { ?? ??? ??? ?for (int n = 0 ; n < rooms[m].length ; n ++) { ?? ??? ??? ??? ?// 第一層 ?? ??? ??? ??? ?if (m == 0) { ?? ??? ??? ??? ??? ?/* ?? ??? ??? ??? ??? ??? ?這里我們的房間編號這樣設(shè)置: ?? ??? ??? ??? ??? ??? ?如果是是酒店的第一層樓的第一個房間,我們將房間編號設(shè)置成:101 ?? ??? ??? ??? ??? ??? ?我規(guī)定我們的酒店的樓層為1~5層; ?? ??? ??? ??? ??? ??? ?我規(guī)定我們的酒店的第一個房間為1 ?? ??? ??? ??? ??? ??? ?所以如果我們用二維數(shù)組來表示酒店的樓層和第幾個房間時,因?yàn)槲覀兊亩S數(shù)組的橫縱坐標(biāo)都是從0開始的,所以我們需要分別加上1,此時房間編號的表達(dá)式就為: ?? ??? ??? ??? ??? ??? ?(m + 1) * 100 + n + 1 ?? ??? ??? ??? ??? ??? ?當(dāng)m = 0時: ?? ??? ??? ??? ??? ??? ??? ?n = 0:房間編號為101; ?? ??? ??? ??? ??? ??? ??? ?n = 1:房間編號為102; ?? ??? ??? ??? ??? ??? ??? ?n = 2;房間編號為103; ?? ??? ??? ??? ??? ??? ??? ?... ?? ??? ??? ??? ??? ??? ?當(dāng)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); ?? ??? ??? ??? ?} ?? ??? ??? ?} ?? ??? ?} ?? ?} ?? ? ?? ?// 查看所有房間狀態(tài) ?? ?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]; ?? ??? ?// 如果該編號的房間已經(jīng)有人訂了 ?? ??? ?if (room.getrFree() == true) { ?? ??? ??? ?System.out.println("抱歉,請您訂購其他房間,此房間已經(jīng)有人居??!"); ?? ??? ?} 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("歡迎使用酒店管理系統(tǒng),請認(rèn)真閱讀以下使用說明:"); ?? ??? ?System.out.println("請輸入對應(yīng)的功能編號:[1]查看房間列表; [2]訂房; [3]退房; [0]退出系統(tǒng)"); ?? ??? ?Scanner scanner = new Scanner(System.in); ?? ??? ?while (true) { ?? ??? ??? ?System.out.print("請輸入功能編號:"); ?? ??? ??? ?Integer i = scanner.nextInt(); ?? ??? ??? ?if (i == 1) { ?? ??? ??? ??? ?hotel.queryAllRooms(); ?? ??? ??? ??? ?System.out.println("酒店所有的房間已經(jīng)加載完畢!"); ?? ??? ??? ?} ?? ??? ??? ?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("成功退出酒店管理系統(tǒng)!"); ?? ??? ??? ??? ?scanner.close(); ?? ??? ??? ??? ?return; ?? ??? ??? ?} ?? ??? ??? ?else { ?? ??? ??? ??? ?System.out.println("請仔細(xì)閱讀使用說明,輸入正確的功能編號"); ?? ??? ??? ?} ?? ??? ?} ?? ?} }
輸出結(jié)果:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java實(shí)現(xiàn)簡單酒店管理系統(tǒng)
- Java實(shí)戰(zhàn)之酒店人事管理系統(tǒng)的實(shí)現(xiàn)
- Java 實(shí)戰(zhàn)練手項(xiàng)目之酒店管理系統(tǒng)的實(shí)現(xiàn)流程
- 基于JavaSwing設(shè)計和實(shí)現(xiàn)的酒店管理系統(tǒng)
- Java實(shí)現(xiàn)簡單的酒店管理系統(tǒng)
- java實(shí)現(xiàn)酒店管理系統(tǒng)
- Java swing實(shí)現(xiàn)酒店管理系統(tǒng)
- Java實(shí)現(xiàn)酒店客房管理系統(tǒng)
- Java代碼實(shí)現(xiàn)簡單酒店管理系統(tǒng)
相關(guān)文章
Java實(shí)現(xiàn)將文件或者文件夾壓縮成zip的詳細(xì)代碼
這篇文章主要介紹了Java實(shí)現(xiàn)將文件或者文件夾壓縮成zip的詳細(xì)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-11-11SpringCloud Config連接git與數(shù)據(jù)庫流程分析講解
springcloud config是一個解決分布式系統(tǒng)的配置管理方案。它包含了 client和server兩個部分,server端提供配置文件的存儲、以接口的形式將配置文件的內(nèi)容提供出去,client端通過接口獲取數(shù)據(jù)、并依據(jù)此數(shù)據(jù)初始化自己的應(yīng)用2022-12-12Hibernate實(shí)現(xiàn)many-to-many的映射關(guān)系
今天小編就為大家分享一篇關(guān)于Hibernate實(shí)現(xiàn)many-to-many的映射關(guān)系,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03java 對文件夾目錄進(jìn)行深度遍歷實(shí)例代碼
這篇文章主要介紹了java 對文件夾目錄進(jìn)行深度遍歷實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03intellij idea如何將web項(xiàng)目打成war包的實(shí)現(xiàn)
這篇文章主要介紹了intellij idea如何將web項(xiàng)目打成war包的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07SpringBoot中MybatisX插件的簡單使用教程(圖文)
MybatisX 是一款基于 IDEA 的快速開發(fā)插件,方便在使用mybatis以及mybatis-plus開始時簡化繁瑣的重復(fù)操作,本文主要介紹了SpringBoot中MybatisX插件的簡單使用教程,感興趣的可以了解一下2023-06-06