Java编程题-模拟酒店管理系统

需求:显示酒店所有房间列表,预订房间,退房…

已知:

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
class Room {
String no;
String type; // "标准间" "双人间" "豪华间"
boolean isUse; // true表示占用,false表示空闲
}

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

Room[][] rooms;

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

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

// 对外提供预定方法
// 对外提供退房方法
}

class Test(){
// main
}

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//酒店的房间
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 isUse() {
return isUse;
}

public void setUse(boolean isUse) {
this.isUse = isUse;
}

public String toString() {
return "[" + no + "," + type + "," + (isUse ? "占用" : "空闲") + "]";
}
}

public class Hotel {

// 房间
Room[][] rooms;

// Constructor
Hotel() {

// 5层 每层10个房间
rooms = new Room[5][10];

// 赋值
// 1,2 标准间
// 3 4 双人间
// 5 豪华间
for (int i = 0; i < rooms.length; i++) {

for (int j = 0; j < rooms[i].length; j++) {

if (i == 0 || i == 1) {
rooms[i][j] = new Room(((i + 1) * 100) + j + 1 + "", "标准间", false);
}

if (i == 2 || i == 3) {
rooms[i][j] = new Room(((i + 1) * 100) + j + 1 + "", "双人间", false);
}

if (i == 4) {
rooms[i][j] = new Room(((i + 1) * 100) + j + 1 + "", "豪华间", false);
}

}

}

}

// 对外提供一个打印酒店房间列表的方法
public void print() {

for (int i = 0; i < rooms.length; i++) {

for (int j = 0; j < rooms[i].length; j++) {
System.out.print(rooms[i][j] + " ");
}

System.out.println();

}

}

// 对外提供一个预订酒店的方法
public void order(String no) {

for (int i = 0; i < rooms.length; i++) {

for (int j = 0; j < rooms[i].length; j++) {
if (rooms[i][j].getNo().equals(no)) {
// 将该房间的状态改成占用
rooms[i][j].setUse(true);
return;
}
}

}
}
}

import java.util.Scanner;
public class Test {

public static void main(String[] rags) {

Scanner s = new Scanner(System.in);

System.out.println("欢迎使用酒店管理系统,酒店房间列表如下所示:");

// 初始化酒店
Hotel h = new Hotel();

// 输出房间列表
h.print();

while (true) {
System.out.print("请输入预订房间的编号:");

String no = s.next();

// 预订房间
h.order(no);

// 打印酒店列表
h.print();
}

}
}