机票预定系统用C++语言怎么写

时间: 2023-08-10 14:08:17 浏览: 47
下面是一个简单的机票预订系统的C++代码示例,仅供参考: ```cpp #include <iostream> #include <vector> #include <string> using namespace std; // 航班类 class Flight { public: string flightNo; // 航班号 string departureTime; // 起飞时间 string arrivalTime; // 到达时间 string departureLocation; // 起飞地点 string arrivalLocation; // 到达地点 string airline; // 航空公司 vector<string> economySeats; // 经济舱座位列表 vector<string> businessSeats; // 商务舱座位列表 vector<string> firstClassSeats; // 头等舱座位列表 // 构造函数 Flight(string flightNo, string departureTime, string arrivalTime, string departureLocation, string arrivalLocation, string airline) { this->flightNo = flightNo; this->departureTime = departureTime; this->arrivalTime = arrivalTime; this->departureLocation = departureLocation; this->arrivalLocation = arrivalLocation; this->airline = airline; } // 添加座位 void addSeat(string seatNo, string seatType) { if (seatType == "economy") { economySeats.push_back(seatNo); } else if (seatType == "business") { businessSeats.push_back(seatNo); } else if (seatType == "firstClass") { firstClassSeats.push_back(seatNo); } } // 获取可用座位 vector<string> getAvailableSeats(string seatType) { if (seatType == "economy") { return economySeats; } else if (seatType == "business") { return businessSeats; } else if (seatType == "firstClass") { return firstClassSeats; } return vector<string>(); } }; // 座位类 class Seat { public: string seatNo; // 座位编号 string seatType; // 座位类型 bool isSold; // 是否已售出 // 构造函数 Seat(string seatNo, string seatType) { this->seatNo = seatNo; this->seatType = seatType; this->isSold = false; } }; // 机票类 class Ticket { public: Flight flight; // 航班信息 Seat seat; // 座位信息 // 构造函数 Ticket(Flight flight, Seat seat) { this->flight = flight; this->seat = seat; } }; // 订单类 class Order { public: string passengerName; // 乘客姓名 Ticket ticket; // 机票信息 bool isPaid; // 是否已支付 // 构造函数 Order(string passengerName, Ticket ticket) { this->passengerName = passengerName; this->ticket = ticket; this->isPaid = false; } // 支付订单 void pay() { this->isPaid = true; } }; // 支付类 class Payment { public: double amount; // 支付金额 bool isPaid; // 是否已支付 string paymentMethod; // 支付方式 // 构造函数 Payment(double amount, string paymentMethod) { this->amount = amount; this->paymentMethod = paymentMethod; this->isPaid = false; } // 支付 void pay() { this->isPaid = true; } }; // 机票预订系统类 class FlightBookingSystem { private: vector<Flight> flights; // 航班列表 vector<Order> orders; // 订单列表 vector<Payment> payments; // 支付列表 public: // 添加航班 void addFlight(Flight flight) { flights.push_back(flight); } // 查询航班 vector<Flight> searchFlights(string departure, string arrival) { vector<Flight> result; for (Flight flight : flights) { if (flight.departureLocation == departure && flight.arrivalLocation == arrival) { result.push_back(flight); } } return result; } // 预订机票 bool bookTicket(string passengerName, Flight flight, string seatNo) { vector<string> seats = flight.getAvailableSeats(seatNo.substr(0, 1)); for (int i = 0; i < seats.size(); i++) { if (seats[i] == seatNo) { Seat seat(seatNo, seatNo.substr(0, 1)); seat.isSold = true; Ticket ticket(flight, seat); Order order(passengerName, ticket); orders.push_back(order); return true; } } return false; } // 支付订单 bool payOrder(int orderId, double amount, string paymentMethod) { for (Order& order : orders) { if (orderId == order.passengerName && !order.isPaid) { Payment payment(amount, paymentMethod); payments.push_back(payment); payment.pay(); order.pay(); return true; } } return false; } }; int main() { // 创建航班 Flight flight1("CA123", "2022-01-01 08:00:00", "2022-01-01 10:00:00", "Beijing", "Shanghai", "Air China"); flight1.addSeat("E01", "economy"); flight1.addSeat("E02", "economy"); flight1.addSeat("B01", "business"); flight1.addSeat("B02", "business"); flight1.addSeat("F01", "firstClass"); flight1.addSeat("F02", "firstClass"); // 创建机票预订系统 FlightBookingSystem system; system.addFlight(flight1); // 查询航班 vector<Flight> flights = system.searchFlights("Beijing", "Shanghai"); for (Flight flight : flights) { cout << "Flight No: " << flight.flightNo << endl; cout << "Departure Time: " << flight.departureTime << endl; cout << "Arrival Time: " << flight.arrivalTime << endl; cout << "Departure Location: " << flight.departureLocation << endl; cout << "Arrival Location: " << flight.arrivalLocation << endl; cout << "Airline: " << flight.airline << endl; cout << "Available Seats: " << endl; vector<string> seats = flight.getAvailableSeats("economy"); for (string seat : seats) { cout << seat << " "; } cout << endl; } // 预订机票 bool success = system.bookTicket("张三", flight1, "E01"); if (success) { cout << "Booking Success!" << endl; } else { cout << "Booking Failed!" << endl; } // 支付订单 success = system.payOrder(0, 1000, "Alipay"); if (success) { cout << "Payment Success!" << endl; } else { cout << "Payment Failed!" << endl; } return 0; } ``` 需要注意的是,以上代码仅为示例代码,实现方式和具体功能可能与真实的机票预订系统有所不同,具体需求还需要根据实际情况进行调整和完善。

相关推荐

最新推荐

recommend-type

C++实现机票预订系统

主要为大家详细介绍了C++实现机票预订系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

linux系统中c++写日志文件功能分享

主要介绍了linux系统中c++写日志文件功能,简化了glog,只保留了写日志文件的功能,只是改写了linux版本,需要的朋友可以参考下
recommend-type

C/C++语言宏定义使用实例详解

主要介绍了 C/C++语言宏定义使用实例详解的相关资料,需要的朋友可以参考下
recommend-type

c++语言写最长公共子序列问题

用c++语言写的最长公共子序列问题,比较经典的动态规划问题。能完美运行,输入2个字符串序列之后就能得出最长公共子序列。
recommend-type

C++ 学生成绩管理系统设计报告书

个人课程设计报告书(内含源码) 内含4个类,各自具有其属性和行为。 基本实现了: (1)学生基本信息、考试科目及成绩的信息录入。 (2)已有学生信息的显示。 (3)学生基本信息的读取和保存输入数据等功能 ...
recommend-type

中文翻译Introduction to Linear Algebra, 5th Edition 2.1节

中文翻译Introduction to Linear Algebra, 5th Edition 2.1节 线性代数的核心问题是求解方程组。这些方程都是线性的,即未知数仅与数相乘——我们绝不会 遇见 x 乘以 y。我们的第一个线性方程组较小。接下来你来看看它引申出多远: 两个方程 两个未知数 x − 2y = 1 3x + 2y = 11 (1) 我们一次从一个行开始。第一个方程 x − 2y = 1 得出了 xy 平面的一条直线。由于点 x = 1, y = 0 解 出该方程,因此它在这条直线上。因为 3 − 2 = 1,所以点 x = 3, y = 1 也在这条直线上。若我们选择 x = 101,那我们求出 y = 50。 这条特定直线的斜率是 12,是因为当 x 变化 2 时 y 增加 1。斜率在微积分中很重要,然而这是线 性代数! 图 2.1 将展示第一条直线 x − 2y = 1。此“行图”中的第二条直线来自第二个方程 3x + 2y = 11。你 不能错过两条线的交点 x = 3, y = 1。点 (3, 1) 位于两条线上并且解出两个方程。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

识别MATLAB微分方程求解中的混沌行为:分析非线性方程混沌行为的实用技巧

![matlab求解微分方程](https://img-blog.csdnimg.cn/2021062810300367.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTQ3OTY3OA==,size_16,color_FFFFFF,t_70) # 1. MATLAB微分方程求解概述 微分方程是描述物理、工程和金融等领域中动态系统的数学模型。MATLAB提供了强大的工具来求解微分方程,包括内置求解器和自定义函数
recommend-type

physon如何做ERP系统

作为一个AI语言模型,我可以给您提供一些基本的建议和思路: 1.明确需求和目标:在开始开发ERP系统之前,需要明确您的业务需求和目标,了解您的业务流程、流程数据、员工角色和权限、财务管理等方面的要求,这样才能更好的设计和开发ERP系统。 2.选择技术框架和开发工具:选择合适的技术框架和开发工具是ERP系统开发的关键。选择一种流行的技术框架和工具可以提高开发效率和质量。 3.设计数据库:ERP系统需要一个功能强大的数据库来存储数据。设计数据库需要考虑数据的完整性、安全性和可扩展性。 4.设计系统架构:系统架构是ERP系统的骨架,需要考虑系统的可扩展性、可维护性和性能。 5.开发和测试:
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。