用C++语言做一个航空客运订票系统(需要完成数据持久化保存,使用vector存储数据。使用QT窗体实现)(1)航班管理。每条航班所涉及的信息有:终点站名、航班号、飞机型号、飞行日期、成员定额、余票量。(2)乘客管理。有关订票的乘客信息,包括姓名、订票量、舱位等级。(3)主要操作功能a)查询航班。根据终点站名输出下列信息:航班号、飞机型号、飞行日期、余票量。b)承办订票业务。根据乘客要求查询航班的余票额,票数量充足的情况下办理订票c)承办退票业务。旅客输入自己的个人姓名、航班号办理退票业务。
要能自己会改,主要涉及到数据管理、持久化、窗体设计等多个方面。以下是一个简单的实现步骤和大致的代码架构,仅供参考和借鉴。
代码示例(类声明部分):
class Flight {
private:
string dest; //终点站
string flightNo; //航班号
string planeType; //飞机型号
string date; //飞行日期
int quota; //成员定额
int tickets; //余票量
public:
Flight(string dest, string flightNo, string planeType, string date, int quota, int tickets);
void setDest(string dest);
void setFlightNo(string flightNo);
void setPlaneType(string planeType);
void setDate(string date);
void setQuota(int quota);
void setTickets(int tickets);
string getDest();
string getFlightNo();
string getPlaneType();
string getDate();
int getQuota();
int getTickets();
};
class Passenger {
private:
string name; //姓名
int tickets; //订票量
int level; //舱位等级
public:
Passenger(string name, int tickets, int level);
void setName(string name);
void setTickets(int tickets);
void setLevel(int level);
string getName();
int getTickets();
int getLevel();
};
代码示例:
#include <QtWidgets/QMainWindow>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QProgressBar>
#include <QComboBox>
#include <QTableView>
#include <QStandardItemModel>
#include <QMessageBox>
#include <QString>
#include <QVBoxLayout>
#include <QStringList>
#include <QDateTime>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
protected:
void createMenuBar();
void createWidgets();
void createLayout();
void createConnections();
private slots:
void queryFlights();
void addPassenger();
void cancelTicket();
private:
QMenuBar* menuBar;
QPushButton* queryButton;
QPushButton* addButton;
QPushButton* cancelButton;
QLabel* destLabel;
QLabel* nameLabel;
QLabel* flightLabel;
QLabel* flightLabel2;
QLineEdit* destEdit;
QLineEdit* nameEdit;
QComboBox* flightEdit;
QTableView* resultView;
QStandardItemModel* resultModel;
vector<Flight> flights;
vector<Passenger> passengers;
};
代码示例:
void MainWindow::queryFlights()
{
QString dest = destEdit->text();
if (dest == "") {
QMessageBox::warning(this, "Error", "Destination cannot be empty!");
return;
}
resultModel->clear();
resultModel->setHorizontalHeaderLabels(QStringList() << "Flight No." << "Plane Type" << "Date" << "Quota" << "Tickets Left");
for (int i = 0; i < flights.size(); i++) {
if (flights[i].getDest() == dest.toStdString()) {
QList<QStandardItem*> items;
items << new QStandardItem(QString::fromStdString(flights[i].getFlightNo()));
items << new QStandardItem(QString::fromStdString(flights[i].getPlaneType()));
items << new QStandardItem(QString::fromStdString(flights[i].getDate()));
items << new QStandardItem(QString::number(flights[i].getQuota()));
items << new QStandardItem(QString::number(flights[i].getTickets()));
resultModel->appendRow(items);
}
}
}
void MainWindow::addPassenger()
{
QString name = nameEdit->text();
QString flight = flightEdit->currentText();
int index = -1;
for (int i = 0; i < flights.size(); i++) {
if (flights[i].getFlightNo() == flight.toStdString()) {
index
(续)
...
break;
}
}
if (index == -1) {
QMessageBox::warning(this, "Error", "Cannot find the flight!");
return;
}
int tickets = passengers.size();
int quota = flights[index].getQuota();
if (tickets >= quota) {
QMessageBox::warning(this, "Error", "Tickets are sold out!");
return;
}
Passenger p(name.toStdString(), 1, 1);
passengers.push_back(p);
flights[index].setTickets(quota - (int)passengers.size());
QMessageBox::information(this, "Success", "Book a ticket successfully!");
}
void MainWindow::cancelTicket()
{
QString name = nameEdit->text();
QString flight = flightEdit->currentText();
int index = -1;
for (int i = 0; i < flights.size(); i++) {
if (flights[i].getFlightNo() == flight.toStdString()) {
index = i;
break;
}
}
if (index == -1) {
QMessageBox::warning(this, "Error", "Cannot find the flight!");
return;
}
for (int i = 0; i < passengers.size(); i++) {
if (passengers[i].getName() == name.toStdString() && passengers[i].getTickets() == 1) {
passengers.erase(passengers.begin() + i);
int quota = flights[index].getQuota();
flights[index].setTickets(quota + 1);
QMessageBox::information(this, "Success", "Cancel the ticket successfully!");
return;
}
}
QMessageBox::warning(this, "Error", "No matching ticket to cancel!");
}
代码示例:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
createMenuBar();
createWidgets();
createLayout();
createConnections();
}
void MainWindow::createMenuBar()
{
menuBar = new QMenuBar(this);
QMenu* fileMenu = new QMenu("File");
QAction* saveAction = fileMenu->addAction("Save");
QAction* loadAction = fileMenu->addAction("Load");
menuBar->addMenu(fileMenu);
setMenuBar(menuBar);
}
void MainWindow::createWidgets()
{
queryButton = new QPushButton("Query", this);
addButton = new QPushButton("Add", this);
cancelButton = new QPushButton("Cancel", this);
destLabel = new QLabel("Destination:", this);
nameLabel = new QLabel("Name:", this);
flightLabel = new QLabel("Flight:", this);
flightLabel2 = new QLabel("Flight:", this);
destEdit = new QLineEdit(this);
nameEdit = new QLineEdit(this);
flightEdit = new QComboBox(this);
resultView = new QTableView(this);
resultModel = new QStandardItemModel(this);
for (int i = 0; i < flights.size(); i++) {
flightEdit->addItem(QString::fromStdString(flights[i].getFlightNo()));
}
}
void MainWindow::createLayout()
{
QVBoxLayout* mainLayout = new QVBoxLayout
(续)
...
inputLayout->addWidget(destLabel);
inputLayout->addWidget(destEdit);
inputLayout->addWidget(nameLabel);
inputLayout->addWidget(nameEdit);
inputLayout->addWidget(flightLabel);
inputLayout->addWidget(flightEdit);
mainLayout->addLayout(inputLayout);
QHBoxLayout* buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(queryButton);
buttonLayout->addWidget(addButton);
buttonLayout->addWidget(cancelButton);
mainLayout->addLayout(buttonLayout);
mainLayout->addWidget(flightLabel2);
mainLayout->addWidget(resultView);
setLayout(mainLayout);
}
void MainWindow::createConnections()
{
connect(queryButton, SIGNAL(clicked(bool)), this, SLOT(queryFlights()));
connect(addButton, SIGNAL(clicked(bool)), this, SLOT(addPassenger()));
connect(cancelButton, SIGNAL(clicked(bool)), this, SLOT(cancelTicket()));
}
MainWindow::~MainWindow()
{
//save data to file before exit
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
以上是基于C++和QT界面实现航空客运订票系统的一个简单实现。由于本项目较为复杂,实际实现过程可能会因具体需求而有所变化。