用c++设计一个飞机类和旅客类用来实现一个航班订票系统。飞机类数据成员有:载客量,头等舱座位数量,经济舱座位数量,座位状态(0表示空,1表示座位已订),航班号,起飞时间,到达时间,;旅客类数据成员有:姓名,座位号,是否会员(0表示非会员,1表示会员),会员号,会员购票折扣,会员旅行记录(用一个结构体(航班号,起飞时间)数组记录该会员的历史旅行记录)。程序显示一下菜单选项:1)Please input 1 for member customer and type 0 for non-member customer;用户确定为会员或非会员后,进行下一步操作:2)Please input Please type 1 for “first class” and type 3 for “Economy”。如果客人输入1,头等舱不满,则分配座位号;如头等舱满座,询问客人是否接受经济舱。如愿意,就为其分配座位。如果不愿意,打印“Please book next flight”。如座位分配成功,打印:座位号,票价,航班号。票价根据是否会员,计算(会员按照折扣计算最终价格,非会员全价)。 程序不能把订过的座位重新分配个别人。
时间: 2023-10-07 12:07:40 浏览: 81
以下是一个简单的飞机类和旅客类的实现,以及一个基本的航班订票系统的实现。注意,这只是一个基本的例子,可能需要根据实际需求进行修改和扩展。
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 旅客类
class Passenger {
private:
string name; // 姓名
int seatNumber; // 座位号
bool isMember; // 是否会员
int memberNumber; // 会员号
double discount; // 会员购票折扣
struct TravelRecord {
string flightNumber;
string departureTime;
} travelRecord[10]; // 最多记录10次旅行历史
public:
// 构造函数
Passenger(string name, bool isMember = false, int memberNumber = 0, double discount = 1.0)
: name(name), seatNumber(-1), isMember(isMember), memberNumber(memberNumber), discount(discount) {}
// 获取姓名
string getName() const {
return name;
}
// 获取座位号
int getSeatNumber() const {
return seatNumber;
}
// 设置座位号
void setSeatNumber(int seatNumber) {
this->seatNumber = seatNumber;
}
// 是否会员
bool getIsMember() const {
return isMember;
}
// 获取会员号
int getMemberNumber() const {
return memberNumber;
}
// 获取折扣
double getDiscount() const {
return discount;
}
// 添加旅行记录
void addTravelRecord(string flightNumber, string departureTime) {
for (int i = 0; i < 10; i++) {
if (travelRecord[i].flightNumber == "") {
travelRecord[i].flightNumber = flightNumber;
travelRecord[i].departureTime = departureTime;
break;
}
}
}
};
// 飞机类
class Airplane {
private:
int capacity; // 载客量
int firstClassSeats; // 头等舱座位数量
int economyClassSeats; // 经济舱座位数量
vector<int> seatStatus; // 座位状态,0表示空,1表示座位已订
string flightNumber; // 航班号
string departureTime; // 起飞时间
string arrivalTime; // 到达时间
public:
// 构造函数
Airplane(int capacity, int firstClassSeats, int economyClassSeats, string flightNumber, string departureTime, string arrivalTime)
: capacity(capacity), firstClassSeats(firstClassSeats), economyClassSeats(economyClassSeats),
seatStatus(capacity), flightNumber(flightNumber), departureTime(departureTime), arrivalTime(arrivalTime) {}
// 订票,返回票价,如果订票失败返回-1
double bookTicket(Passenger& passenger, bool isFirstClass) {
int seatNumber = -1;
int start = isFirstClass ? 0 : firstClassSeats; // 座位编号的起始值
int end = isFirstClass ? firstClassSeats : capacity; // 座位编号的结束值
for (int i = start; i < end; i++) {
if (seatStatus[i] == 0) {
seatNumber = i;
break;
}
}
// 如果头等舱满了,且客人不接受经济舱,返回-1
if (seatNumber == -1 && isFirstClass) {
cout << "First class is full. Do you accept economy class? Please input Y or N." << endl;
char answer;
cin >> answer;
if (answer == 'Y' || answer == 'y') {
return bookTicket(passenger, false);
} else {
cout << "Please book next flight." << endl;
return -1;
}
}
// 如果都满了,返回-1
if (seatNumber == -1) {
cout << "All seats are full. Please book next flight." << endl;
return -1;
}
// 订票成功,更新座位状态和旅客信息
seatStatus[seatNumber] = 1;
passenger.setSeatNumber(seatNumber);
double price = isFirstClass ? 1000 : 500; // 票价
if (passenger.getIsMember()) {
price *= passenger.getDiscount(); // 如果是会员,按折扣计算价格
}
passenger.addTravelRecord(flightNumber, departureTime); // 添加旅行记录
cout << "Seat number: " << seatNumber << ", price: " << price << ", flight number: " << flightNumber << endl;
return price;
}
// 获取载客量
int getCapacity() const {
return capacity;
}
// 获取头等舱座位数量
int getFirstClassSeats() const {
return firstClassSeats;
}
// 获取经济舱座位数量
int getEconomyClassSeats() const {
return economyClassSeats;
}
// 获取航班号
string getFlightNumber() const {
return flightNumber;
}
// 获取起飞时间
string getDepartureTime() const {
return departureTime;
}
// 获取到达时间
string getArrivalTime() const {
return arrivalTime;
}
};
int main() {
// 创建一个飞机对象
Airplane airplane(150, 30, 120, "MU1234", "2022-01-01 08:00:00", "2022-01-01 10:30:00");
// 输入是否会员
bool isMember;
cout << "Please input 1 for member customer and type 0 for non-member customer." << endl;
cin >> isMember;
// 输入头等舱或经济舱
bool isFirstClass;
cout << "Please type 1 for 'first class' and type 0 for 'Economy'." << endl;
cin >> isFirstClass;
// 创建一个旅客对象
Passenger passenger("John", isMember, 12345, 0.8);
// 订票
double price = airplane.bookTicket(passenger, isFirstClass);
if (price == -1) {
return 0;
}
// 打印订票信息
cout << "Name: " << passenger.getName() << ", seat number: " << passenger.getSeatNumber() << ", price: " << price << endl;
return 0;
}
```
阅读全文