好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java代码实现酒店管理系统

我们通过学习Java基础知识,让自己正式踏入学习Java语言的行列,这篇博客是用来让我们真正的了解并应用面向对象的思想来实现的。

使用简单的Java代码实现酒店管理系统,供大家参考,具体内容如下

一. 需求分析

我们如果要实现酒店管理系统,就需要先对酒店管理系统的业务要求进行分析:

1.酒店管理系统需要实现哪些功能?

(1)输入某个命令查询酒店中的所有房间;
(2)输入某个房间的编号订房;
(3)输入某个房间的编号退房;
(4)输入某个命令可以退出酒店管理系统;

2.酒店管理系统使用什么数据结构来表示房间?

(1)酒店管理系统使用数组的形式来存储房间,这里我们用二维数组来表示;

3.酒店的房间我们都有什么属性?

(1)房间编号;
(2)房间类型;
(3)房间是否空闲;

4.我们用什么来控制房间类有上面这些属性?

(1)我们可以使用自定义注解来实现;

二. 画图分析

三. 代码结构

四. 代码实现

?

1

2

3

4

5

6

/**

     自定义注解类:该类作用在房间类上。

*/

@Retention (RUNTIME)         // 用该注解表示此自定义注解可以被外部映射到

@Target (TYPE)             // 用该注解表示此自定义注解只可以作用在类上

public @interface ProperyAnnotation {}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

/**

     当自定义注解类作用在房间实体类上时,该类是用来判断房间实体类上是否有:房间编号、房间类型、房间是否空闲这些属性的。

*/

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 (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();

         }

     }

}

?

1

2

3

4

5

6

7

8

/**

     当ProperyAnnotation注解作用的房间实体类中没有房间编号、房间类型、房间是否空闲这些属性时,此类为抛出的异常类。

*/

public class ProperyException extends RuntimeException {

     private static final long serialVersionUID = -8343113740914228496L;

     public ProperyException () {}

     public ProperyException (String msg) { super (msg);}

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

/**

     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 ? "有人入住" : "无人入住" )+ "]" ;

     }

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

/**

     酒店管理实体类:用来管理房间的,其中包括查看所有房间状态、订房、退房功能。

*/

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( "退房完成" );

         }

     }

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

/**

     酒店的操作测试类:

*/

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( "请仔细阅读使用说明,输入正确的功能编号" );

             }

         }

     }

}

输出结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/weixin_45645403/article/details/112196199

查看更多关于Java代码实现酒店管理系统的详细内容...

  阅读:23次