酒店管理系统小程序(包含源码C++实现)
本文实现一个酒店管理系统小程序,涉及多个方面,包括用户接口、房间管理、预订系统、用户管理等。为了保持示例的简洁性,下面的实现将包括一个简单的控制台程序,演示基本的酒店管理功能。这将涵盖以下功能:
- 查看房间状态
- 预订房间
- 取消预订
- 查看预订信息
以下是一个基础的实现示例:
1. 主要文件和目录结构
hotel_management/
├── main.cpp
├── Hotel.h
├── Hotel.cpp
├── Room.h
├── Room.cpp
└── CMakeLists.txt
2. Room.h
#ifndef ROOM_H
#define ROOM_H
#include <string>
class Room {
public:
Room(int number, bool isAvailable = true);
int getNumber() const;
bool isAvailable() const;
void book();
void cancel();
private:
int number;
bool available;
};
#endif // ROOM_H
3. Room.cpp
#include "Room.h"
Room::Room(int number, bool isAvailable) : number(number), available(isAvailable) {}
int Room::getNumber() const {
return number;
}
bool Room::isAvailable() const {
return available;
}
void Room::book() {
available = false;
}
void Room::cancel() {
available = true;
}
4. Hotel.h
#ifndef HOTEL_H
#define HOTEL_H
#include "Room.h"
#include <vector>
class Hotel {
public:
Hotel(int numRooms);
void showAvailableRooms() const;
bool bookRoom(int roomNumber);
bool cancelBooking(int roomNumber);
void showBookings() const;
private:
std::vector<Room> rooms;
};
#endif // HOTEL_H
5. Hotel.cpp
#include "Hotel.h"
#include <iostream>
Hotel::Hotel(int numRooms) {
for (int i = 1; i <= numRooms; ++i) {
rooms.emplace_back(i);
}
}
void Hotel::showAvailableRooms() const {
std::cout << "Available Rooms:\n";
for (const auto &room : rooms) {
if (room.isAvailable()) {
std::cout << "Room " << room.getNumber() << "\n";
}
}
}
bool Hotel::bookRoom(int roomNumber) {
for (auto &room : rooms) {
if (room.getNumber() == roomNumber && room.isAvailable()) {
room.book();
return true;
}
}
return false;
}
bool Hotel::cancelBooking(int roomNumber) {
for (auto &room : rooms) {
if (room.getNumber() == roomNumber && !room.isAvailable()) {
room.cancel();
return true;
}
}
return false;
}
void Hotel::showBookings() const {
std::cout << "Booked Rooms:\n";
for (const auto &room : rooms) {
if (!room.isAvailable()) {
std::cout << "Room " << room.getNumber() << "\n";
}
}
}
6. main.cpp
#include <iostream>
#include "Hotel.h"
void showMenu() {
std::cout << "Hotel Management System\n";
std::cout << "1. View Available Rooms\n";
std::cout << "2. Book a Room\n";
std::cout << "3. Cancel Booking\n";
std::cout << "4. View Bookings\n";
std::cout << "5. Exit\n";
std::cout << "Choose an option: ";
}
int main() {
Hotel hotel(10); // Initialize hotel with 10 rooms
int choice;
int roomNumber;
while (true) {
showMenu();
std::cin >> choice;
switch (choice) {
case 1:
hotel.showAvailableRooms();
break;
case 2:
std::cout << "Enter room number to book: ";
std::cin >> roomNumber;
if (hotel.bookRoom(roomNumber)) {
std::cout << "Room " << roomNumber << " booked successfully.\n";
} else {
std::cout << "Failed to book room " << roomNumber << ".\n";
}
break;
case 3:
std::cout << "Enter room number to cancel: ";
std::cin >> roomNumber;
if (hotel.cancelBooking(roomNumber)) {
std::cout << "Booking for room " << roomNumber << " canceled successfully.\n";
} else {
std::cout << "Failed to cancel booking for room " << roomNumber << ".\n";
}
break;
case 4:
hotel.showBookings();
break;
case 5:
return 0;
default:
std::cout << "Invalid option. Please try again.\n";
}
}
return 0;
}
7. CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(HotelManagement)
set(CMAKE_CXX_STANDARD 11)
add_executable(HotelManagement main.cpp Hotel.cpp Room.cpp)
编译和运行
-
创建构建目录:
mkdir build cd build
-
生成构建文件:
cmake ..
-
编译项目:
make
-
运行程序:
./HotelManagement
说明
- Room 类: 表示酒店房间,包含房间号和是否可用的状态。
- Hotel 类: 管理一组房间,提供查看可用房间、预订房间、取消预订和查看预订信息的功能。
- main.cpp: 提供了一个简单的用户界面,允许用户选择不同的操作并与
Hotel
类交互。
这个示例是一个简化的酒店管理系统,适合用作学习和基础参考。如果需要更复杂的功能,如用户身份管理、详细的预订信息管理等,可以在此基础上扩展。