用c++写一个游乐园系统,游客需要注册,登录,充值后买票,票的种类为单人票,团购票和月票,买完票后提示顾客余额,并计算游乐场一天的营业额
时间: 2023-10-21 11:07:16 浏览: 97
好的,这是一个比较大的项目,需要涉及到多个方面的知识,我会尽力回答。首先,我们需要设计一下数据结构和类的关系:
1. 游客类 Tourist:
- 属性:账号、密码、余额
- 方法:注册、登录、充值、购买票
2. 票类 Ticket:
- 属性:种类、价格
- 方法:无
3. 游乐场类 Playground:
- 属性:单人票价格、团购票价格、月票价格、营业额
- 方法:无
接下来,我们可以根据这个设计,开始编写代码。首先是 Tourist 类:
```cpp
#include <iostream>
#include <string>
#include <vector>
#include "Ticket.h" // 票类的头文件
using namespace std;
class Tourist {
private:
string account; // 账号
string password; // 密码
double balance; // 余额
vector<Ticket*> tickets; // 已购票的列表
public:
Tourist(string account, string password) {
this->account = account;
this->password = password;
this->balance = 0.0;
}
// 注册
void registerAccount() {
cout << "请输入账号:";
cin >> account;
cout << "请输入密码:";
cin >> password;
cout << "注册成功!" << endl;
}
// 登录
bool login() {
string inputAccount, inputPassword;
cout << "请输入账号:";
cin >> inputAccount;
cout << "请输入密码:";
cin >> inputPassword;
if (inputAccount == account && inputPassword == password) {
cout << "登录成功!" << endl;
return true;
}
else {
cout << "账号或密码错误,请重新输入!" << endl;
return false;
}
}
// 充值
void recharge() {
double amount;
cout << "请输入充值金额:";
cin >> amount;
balance += amount;
cout << "充值成功,当前余额为:" << balance << endl;
}
// 购买票
void buyTicket(int type, Playground& playground) {
double price;
if (type == 1) {
price = playground.getSingleTicketPrice();
}
else if (type == 2) {
price = playground.getGroupTicketPrice();
}
else if (type == 3) {
price = playground.getMonthTicketPrice();
}
else {
cout << "无效的票种!" << endl;
return;
}
if (balance < price) {
cout << "余额不足,请充值后购买!" << endl;
return;
}
balance -= price;
Ticket* ticket = new Ticket(type, price); // 创建新的票对象
tickets.push_back(ticket); // 添加到已购票列表中
playground.addRevenue(price); // 增加游乐场的营业额
cout << "购票成功,当前余额为:" << balance << endl;
}
};
```
接下来是 Ticket 类:
```cpp
#include <iostream>
using namespace std;
class Ticket {
private:
int type; // 票种(1:单人票,2:团购票,3:月票)
double price; // 价格
public:
Ticket(int type, double price) {
this->type = type;
this->price = price;
}
int getType() {
return type;
}
double getPrice() {
return price;
}
};
```
最后是 Playground 类:
```cpp
#include <iostream>
using namespace std;
class Playground {
private:
double singleTicketPrice; // 单人票价格
double groupTicketPrice; // 团购票价格
double monthTicketPrice; // 月票价格
double revenue; // 营业额
public:
Playground(double singleTicketPrice, double groupTicketPrice, double monthTicketPrice) {
this->singleTicketPrice = singleTicketPrice;
this->groupTicketPrice = groupTicketPrice;
this->monthTicketPrice = monthTicketPrice;
this->revenue = 0.0;
}
double getSingleTicketPrice() {
return singleTicketPrice;
}
double getGroupTicketPrice() {
return groupTicketPrice;
}
double getMonthTicketPrice() {
return monthTicketPrice;
}
void addRevenue(double amount) {
revenue += amount;
}
double getRevenue() {
return revenue;
}
};
```
最后,我们可以在 main 函数中测试一下:
```cpp
int main() {
Playground playground(50.0, 150.0, 1000.0); // 创建游乐场对象
Tourist tourist("test", "123456"); // 创建游客对象
tourist.registerAccount(); // 注册账号
tourist.login(); // 登录
tourist.recharge(); // 充值
tourist.buyTicket(1, playground); // 购买单人票
tourist.buyTicket(2, playground); // 购买团购票
tourist.buyTicket(3, playground); // 购买月票
cout << "游乐场今日营业额为:" << playground.getRevenue() << endl; // 输出营业额
return 0;
}
```
当然,这只是一个简单的实现,还有很多细节需要考虑,比如输入的合法性、错误处理、数据持久化等等。
阅读全文