功能要求:
?
相关图片:
拆分版
make编译
?
./hotel运行
?
输入2,进入开房模块
?
相关源码:
class.cpp
1 #include <fstream> 2 #include "tools.h" 3 #include "class.h" 4 5 using namespace std; 6 7 Customer* cust[30]; 8 Room* room[30]; 9 10 11 int live; // 被订房间数 12 13 // 获取room_num 14 short Room::get_room_num(void) 15 { 16 return room_num; 17 } 18 19 // 返回房间的下标 20 int Manage::room_index(short room_num) 21 { 22 short num = room_num; 23 for(int i=0; i<30; i++) 24 { 25 if(num == room[i]->get_room_num()) 26 { 27 return i; 28 } 29 } 30 return -1; 31 } 32 33 // 返回顾客的下标 34 int Manage::cust_index(short room_num) 35 { 36 short num = room_num; 37 for(int i=0; i<30; i++) 38 { 39 if(num == cust[i]->room_num) 40 { 41 return i; 42 } 43 } 44 return -1; 45 } 46 47 // 查询剩余房间 48 void Manage::find_room(void) 49 { 50 for(int i=0; i<30; i++) 51 { 52 if(i == 10 || i == 20) 53 { 54 cout << endl; 55 } 56 if(room[i]->use != 0) 57 { 58 continue; 59 } 60 cout << room[i]->room_num << " "; 61 } 62 cout << endl; 63 } 64 65 // 开房 66 void Manage::get_room(void) 67 { 68 cout << "现有房间如下" << endl; 69 find_room(); 70 71 // cout << "已经入住的人员" << endl; 72 // show_cust(); 73 74 cout << "请输入您选择的房间:"; 75 short room_num; 76 cin >> room_num; 77 int flag = used(room_num); 78 if(flag == 1) 79 { 80 cout << "此房间不能使用,请重新选择" << endl; 81 getch(); 82 return; 83 } 84 else if(flag == -1) 85 { 86 cout << "此房间不存在,请重新选择" << endl; 87 getch(); 88 return; 89 } 90 else 91 { 92 cout << "您选择的房间是:" << room_num << endl; 93 } 94 95 int index = room_index(room_num); 96 short type = room[index]->room_type; 97 98 cout << "请输入您的姓名:"; 99 string name; 100 cin >> name; 101 cout << "请输入您的身份证:"; 102 string id; 103 cin >> id; 104 cout << "请输入您的2位家属(0,表示没有)" << endl; 105 string family1,family2; 106 cin >> family1 >> family2; 107 108 if(type == 1) 109 { 110 if(family1 != "0" || family2 != "0") 111 { 112 cout << "人数过多,开房失败" << endl; 113 getch(); 114 return; 115 } 116 } 117 else if(type == 2) 118 { 119 if(family1 != "0" && family2 != "0") 120 { 121 cout << "人数过多,开房失败" << endl; 122 getch(); 123 return; 124 } 125 } 126 else 127 { 128 } 129 130 cout << "请输入要订的天数:"; 131 short day; 132 cin >> day; 133 short pay = day*room[index]->price; 134 cout << "请支付" << pay << "元" << endl; 135 short money = 0,change = 0; 136 cout << "收您:"; 137 cin >> money; 138 cout << "元" << endl; 139 change = money-pay; 140 if(change < 0) 141 { 142 cout << "余额不足,请充值" << endl; 143 getch(); 144 return; 145 } 146 cout << "找您" << change << "元" << endl; 147 148 short floor = room_num/100; 149 150 cust[live++] = new Customer(name,id,family1,family2,floor,room_num,day); 151 152 cout << "已订房间:" << live << endl; 153 154 for(int i=0; i<30; i++) 155 { 156 if(room[i]->room_num == room_num) 157 { 158 room[i]->use = 1; 159 } 160 } 161 162 cout << "开房成功,欢迎您的入住,祝你生活愉快!" << endl; 163 getch(); 164 } 165 166 // 使用情况 167 int Manage::used(short room_num) 168 { 169 short num = room_num; 170 for(int i=0; i<30; i++) 171 { 172 if(num == room[i]->room_num) 173 { 174 if(room[i]->use == 1) 175 { 176 return 1; 177 } 178 else 179 { 180 return 0; 181 } 182 } 183 } 184 return -1; 185 } 186 187 // 显示顾客 188 void Manage::show_cust(void) 189 { 190 for(int i=0; i<30; i++) 191 { 192 if(cust[i]->name == "0") 193 { 194 break; 195 } 196 else 197 { 198 cout << "姓名:" << cust[i]->name << "," << "房间:" << cust[i]->room_num << ","; 199 200 string f1,f2; 201 f1 = cust[i]->family1; 202 f2 = cust[i]->family2; 203 cout << "家属1:"; 204 if(f1 == "0") cout << " ,"; 205 else cout << f1 << ","; 206 cout << "家属2:"; 207 if(f2 == "0") cout << " "; 208 else cout << f2; 209 } 210 } 211 } 212 213 // 房间价格 214 int Manage::room_price(short room_num) 215 { 216 short num = room_num; 217 for(int i=0; i<30; i++) 218 { 219 if(room[i]->room_num == num) 220 { 221 return room[i]->price; 222 } 223 } 224 return 0; 225 } 226 227 // 续费 228 void Manage::renew_room(void) 229 { 230 cout << "请输入你的房间号:"; 231 short room_num; 232 cin >> room_num; 233 int flag = used(room_num); 234 if(flag == -1 || flag == 0) 235 { 236 cout << "您输入的房间号有误" << endl; 237 getch(); 238 return; 239 } 240 241 int index = cust_index(room_num); 242 243 cout << "您的房间剩余:" << cust[index]->day << "天" << endl; 244 245 cout << "请输入你要续的天数:"; 246 short day; 247 cin >> day; 248 short pay = day*room_price(room_num); 249 cout << "请支付" << pay << "元" << endl; 250 short price = 0,change = 0; 251 cin >> price; 252 change = price-pay; 253 if(change < 0) 254 { 255 cout << "余额不足,请充值" << endl; 256 getch(); 257 return; 258 } 259 cout << "收您" << price <<"元,找您" << change << "元" << endl; 260 261 string rename = cust[index]->name,reid = cust[index]->id; 262 string refamily1=cust[index]->family1,refamily2=cust[index]->family2; 263 short refloor = cust[index]->floor,reday = cust[index]->day+day; 264 cust[index] = new Customer(rename,reid,refamily1,refamily2,refloor,room_num,reday); 265 266 cout << "续费成功,房间的使用时间为:" << reday <<"天" << endl; 267 getch(); 268 269 } 270 271 // 退房 272 void Manage::cancel_room(void) 273 { 274 cout << "请输入您的房间号:"; 275 short room_num; 276 string name; 277 cin >> room_num; 278 cout << "请输入您的姓名:"; 279 cin >> name; 280 int flag = used(room_num); 281 if(flag == 0 || flag == -1) 282 { 283 cout << "您输入的房间有误。" << endl; 284 getch(); 285 return; 286 } 287 else 288 { 289 short refloor = 0,retype = 0,reprice = 0; 290 291 int i = cust_index(room_num); 292 if(i != -1) 293 { 294 if(cust[i]->name == name) 295 { 296 short price = room[room_index(room_num)]->price; 297 short change = cust[i]->day*price; 298 cout << "退还您" << change << "元" << endl; 299 300 cust[i] = new Customer("0","0","0","0",0,0,0); 301 int j = room_index(room_num); 302 refloor = room[j]->floor; 303 retype = room[j]->room_type; 304 reprice = room[j]->price; 305 room[j] = new Room(refloor,room_num,retype,reprice,0); 306 307 cout << "退房成功,感谢您的光顾,欢迎下次光临!"<< endl; 308 live--; 309 getch(); 310 return; 311 } 312 else 313 { 314 //cout << cust[i]->name << endl; 315 cout << "您输入的相关信息有误" << endl; 316 getch(); 317 return; 318 } 319 } 320 else 321 { 322 cout << "您输入的信息有误" << endl; 323 getch(); 324 return; 325 } 326 } 327 } 328 329 // 顾客初始化 330 void Manage::c_init(void) 331 { 332 fstream ci("data/cust.txt",ios::in); 333 if(!ci.good()) 334 { 335 cout << "cust.txt数据加载异常" << endl; 336 } 337 338 for(int i=0; i<30; i++) 339 { 340 string name,id,family1,family2; 341 short floor,room_num,day; 342 ci >> name >> id >> family1 >> family2; 343 ci >> floor >> room_num >> day; 344 cust[i] = new Customer(name,id,family1,family2,floor,room_num,day); 345 if(name != "0") 346 { 347 live++; 348 } 349 } 350 } 351 352 // 房间初始化 353 void Manage::r_init(void) 354 { 355 live = 0; 356 fstream ri("data/room.txt",ios::in); 357 if(!ri.good()) 358 { 359 cout << "room.txt数据加载异常" << endl; 360 } 361 362 for(int i=0; i<30; i++) 363 { 364 short floor,room_num,room_type,price,use; 365 ri >> floor >> room_num; 366 ri >> room_type >> price >> use; 367 room[i] = new Room(floor,room_num,room_type,price,use); 368 } 369 } 370 371 // 数据保存 372 void Manage::save_data(void) 373 { 374 fstream co("data/cust.txt",ios::out); 375 fstream ro("data/room.txt",ios::out); 376 377 for(int i=0; i<30; i++) 378 { 379 co << cust[i]->name << " " << cust[i]->id << " "; 380 co << cust[i]->family1 << " " << cust[i]->family2 << " "; 381 co << cust[i]->floor << " " << cust[i]->room_num << " "; 382 co << cust[i]->day << "\n"; 383 384 ro << room[i]->floor << " " << room[i]->room_num << " "; 385 ro << room[i]->room_type << " " << room[i]->price << " "; 386 ro << room[i]->use << "\n"; 387 } 388 } 389 390 // 菜单 391 void Manage::menu(void) 392 { 393 cout << "***欢迎使用酒店管理系统***" << endl; 394 cout << " 1、查询房间" << endl; 395 cout << " 2、开房" << endl; 396 cout << " 3、续费" << endl; 397 cout << " 4、退房" << endl; 398 cout << " 0、退出系统" << endl; 399 cout << "-----------------------" << endl; 400 }
main.cpp
1 #include <iostream> 2 #include <fstream> 3 #include <stdlib.h> 4 #include <termio.h> 5 #include "class.h" 6 #include "tools.h" 7 8 using namespace std; 9 10 Manage admin; 11 12 // 主函数 13 int main() 14 { 15 admin.c_init(); 16 admin.r_init(); 17 while(1) 18 { 19 system("clear"); 20 admin.menu(); 21 switch(get_cmd(‘0‘,‘4‘)) 22 { 23 case ‘1‘: admin.find_room(); break; 24 case ‘2‘: admin.get_room(); break; 25 case ‘3‘: admin.renew_room(); break; 26 case ‘4‘: admin.cancel_room(); break; 27 case ‘0‘: admin.save_data(); return 0; 28 } 29 getch(); 30 } 31 32 }
tools.cpp
1 #include "tools.h" 2 #include <string.h> 3 #include <getch.h> 4 #include <stdbool.h> 5 6 void clear_stdin(void) 7 { 8 stdin->_IO_read_ptr = stdin->_IO_read_end;//清理输入缓冲区 9 } 10 11 char get_cmd(char start,char end) 12 { 13 clear_stdin(); 14 15 printf("请输入指令:"); 16 while(true) 17 { 18 char val = getch(); 19 if(val >= start && val <= end) 20 { 21 printf("%c\n",val); 22 return val; 23 } 24 } 25 }
class.h
1 #ifndef CLASS_H 2 #define CLASS_H 3 4 #include <iostream> 5 #include <string.h> 6 7 using namespace std; 8 9 class Customer 10 { 11 public: 12 string name; //姓名 13 string id; //身份证 14 string family1; //家属1 15 string family2; //家属2 16 short floor; //楼层 17 short room_num; //房间号 18 short day; //时间 19 Customer(string name="0",string id="0",string family1="0",string family2="0",short floor=0,short room_num=0,short day=0) 20 { 21 this->name = name; 22 this->id = id; 23 this->family1 = family1; 24 this->family2 = family2; 25 this->floor = floor; 26 this->room_num = room_num; 27 this->day = day; 28 } 29 }; 30 31 class Room 32 { 33 public: 34 short floor; //楼层 35 short room_num; //房间号 36 short room_type; //房间类型 37 short price; //价格 38 short use; //是否使用 39 Room(short floor=0,short room_num=0,short room_type=0,short price=0,short use=0) 40 { 41 this->floor = floor; 42 this->room_num = room_num; 43 this->room_type = room_type; 44 this->price = price; 45 this->use = use; 46 } 47 short get_room_num(void); 48 49 }; 50 51 class Manage 52 { 53 public: 54 void menu(void); // 菜单 55 void find_room(void); // 剩余房间 56 void get_room(void); // 开房 57 void renew_room(void); // 续费 58 void cancel_room(void); // 退房 59 int room_index(short room_num); // 房间下标 60 int cust_index(short room_num); // 顾客下标 61 int used(short room_num); // 房间使用情况 62 void show_cust(void); // 显示入住顾客 63 int room_price(short room_num); // 房间价格 64 void c_init(void); // 顾客初始化 65 void r_init(void); // 房间初始化 66 void save_data(void); // 保存数据 67 }; 68 69 #endif//CLASS_H
tools.h
1 #ifndef TOOL_H 2 #define TOOL_H 3 4 #include <stdio.h> 5 6 #include "tools.h" 7 #include <string.h> 8 #include <getch.h> 9 #include <stdbool.h> 10 11 void clear_stdin(void); 12 13 char get_cmd(char start,char end); 14 15 #endif//TOOL_h
原文地址:https://www.cnblogs.com/ikaros-521/p/11402668.html
时间: 2024-10-11 10:56:01