用c++写一个游乐场管理系统,基本要求,能从文件里读出游乐场管理信息,并能将管理信息保存到文件。游乐场信息包括:游乐设施编号、游乐设备名称、管理人员、消费金额、消费日期。功能要求:增加和删除游乐场的消费记录,查询游乐场某个游乐项目某日的收入总额,查询游乐场某日的总收入,查询所有游乐设备的收入情况,并能根据某时段和某日的游乐设备收入高低排序
时间: 2024-02-22 19:58:28 浏览: 50
好的,下面是一个简单的游乐场管理系统的实现,包括游乐设施、游乐场和文件读写三个类。请注意,这只是一个简单的实现,还有很多细节需要完善和优化。
```
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
class AmusementFacility {
public:
AmusementFacility(int id, string name, string manager, double price, string date):
id_(id), name_(name), manager_(manager), price_(price), date_(date) {}
int GetId() const { return id_; }
string GetName() const { return name_; }
string GetManager() const { return manager_; }
double GetPrice() const { return price_; }
string GetDate() const { return date_; }
private:
int id_;
string name_;
string manager_;
double price_;
string date_;
};
class AmusementPark {
public:
AmusementPark() {}
void AddFacility(const AmusementFacility& facility) {
facilities_.push_back(facility);
}
void DeleteFacility(int id) {
for (auto it = facilities_.begin(); it != facilities_.end(); ++it) {
if (it->GetId() == id) {
facilities_.erase(it);
break;
}
}
}
double GetTotalIncome(string date) const {
double total_income = 0;
for (const auto& facility : facilities_) {
if (facility.GetDate() == date) {
total_income += facility.GetPrice();
}
}
return total_income;
}
double GetTotalIncome(string name, string date) const {
double total_income = 0;
for (const auto& facility : facilities_) {
if (facility.GetName() == name && facility.GetDate() == date) {
total_income += facility.GetPrice();
}
}
return total_income;
}
vector<pair<string, double>> GetFacilityIncome(string date_from, string date_to) const {
vector<pair<string, double>> facility_income;
for (const auto& facility : facilities_) {
if (facility.GetDate() >= date_from && facility.GetDate() <= date_to) {
bool found = false;
for (auto& p : facility_income) {
if (p.first == facility.GetName()) {
p.second += facility.GetPrice();
found = true;
break;
}
}
if (!found) {
facility_income.push_back(make_pair(facility.GetName(), facility.GetPrice()));
}
}
}
sort(facility_income.begin(), facility_income.end(), [](const pair<string, double>& p1, const pair<string, double>& p2) {
return p1.second > p2.second;
});
return facility_income;
}
private:
vector<AmusementFacility> facilities_;
};
class FileIO {
public:
static void ReadFromFile(string filename, AmusementPark& park) {
ifstream fin(filename);
if (!fin) {
cout << "Error opening file " << filename << endl;
return;
}
int id;
string name;
string manager;
double price;
string date;
while (fin >> id >> name >> manager >> price >> date) {
AmusementFacility facility(id, name, manager, price, date);
park.AddFacility(facility);
}
fin.close();
}
static void WriteToFile(string filename, const AmusementPark& park) {
ofstream fout(filename);
if (!fout) {
cout << "Error opening file " << filename << endl;
return;
}
for (const auto& facility : park) {
fout << facility.GetId() << " " << facility.GetName() << " " << facility.GetManager()
<< " " << facility.GetPrice() << " " << facility.GetDate() << endl;
}
fout.close();
}
};
int main() {
AmusementPark park;
FileIO::ReadFromFile("amusement_park.txt", park);
// Add a new facility
AmusementFacility new_facility(5, "Roller Coaster", "John", 50.0, "2021-07-01");
park.AddFacility(new_facility);
// Delete a facility
park.DeleteFacility(2);
// Get total income of a certain date
double total_income = park.GetTotalIncome("2021-06-30");
cout << "Total income on 2021-06-30: " << total_income << endl;
// Get total income of a certain facility on a certain date
double facility_income = park.GetTotalIncome("Ferris Wheel", "2021-06-30");
cout << "Income of Ferris Wheel on 2021-06-30: " << facility_income << endl;
// Get income of all facilities in a certain time period
vector<pair<string, double>> facility_income = park.GetFacilityIncome("2021-06-01", "2021-06-30");
cout << "Facility income from 2021-06-01 to 2021-06-30:" << endl;
for (const auto& p : facility_income) {
cout << p.first << ": " << p.second << endl;
}
FileIO::WriteToFile("amusement_park.txt", park);
return 0;
}
```
以上代码实现了从文件里读出游乐场管理信息,并能将管理信息保存到文件。游乐场信息包括:游乐设施编号、游乐设备名称、管理人员、消费金额、消费日期。同时实现了增加和删除游乐场的消费记录,查询游乐场某个游乐项目某日的收入总额,查询游乐场某日的总收入,查询所有游乐设备的收入情况,并能根据某时段和某日的游乐设备收入高低排序。
阅读全文