在我们的项目中,我们需要创建三个类:“客户”,“活动”和“广告”。每个客户都有一个广告系列,每个广告系列都有一个广告:
客户->广告系列->广告
当我们尝试向中添加广告实例时,问题就出现了 给定客户的广告系列。
当我们尝试将广告添加到给定的广告系列时,我们再也看不到它 实际添加广告。广告数量停滞不前。
我们都是C ++的新手,因此不确定如何通过引用来解决这个问题,我们认为这与问题有关。
客户类别:
#pragma once
#include <vector>
#include <cstdio>
#include <string>
#include "campaign.h"
using namespace std;
class Customer
{
string name;
int id;
vector<Campaign> campaigns;
public:
Customer(string name, int id)
{
this->name = name;
this->id = id;
}
string GetName()
{
return name;
}
void SetName(string name)
{
this->name = name;
}
int GetId()
{
return id;
}
void SetId(int id)
{
this->id = id;
}
bool AddCampaign(Campaign campaignObject)
{
campaigns.push_back(campaignObject);
return true;
}
bool CommitAdvertisement(Ad ad, int campaignID)
{
for (int i = 0; i < campaigns.size(); i++)
{
if (campaigns[i].GetId() == campaignID)
{
campaigns[i].CommitAdvertisement(ad);
return true;
}
}
return false;
}
bool hasActiveCampaigns()
{
for (Campaign i : campaigns)
{
if (i.IsActive())
{
return true;
}
}
return false;
}
vector<Campaign> GetAllCampaigns()
{
return campaigns;
}
vector<Ad> GetAllAdsForCampaign(int campaignID)
{
for (int i = 0; i < campaigns.size(); i++)
{
if (campaigns[i].GetId() == campaignID)
{
return campaigns[i].GetAllAds();
}
}
}
};
广告系列类别:
#pragma once
#include <ctime>
#include <string>
#include <vector>
#include <iostream>
#include <exception>
#include <ctime>
#include "Ad.h"
using namespace std;
class Campaign
{
string name;
int id;
time_t fromDateTime;
time_t toDateTime;
float campaignCost;
vector<Ad> ads;
public:
Campaign(time_t fromDateTime, time_t toDateTime, int id, string name, float campaignCost)
{
this->fromDateTime = fromDateTime;
this->toDateTime = toDateTime;
this->id = id;
this->name = name;
this->campaignCost = campaignCost;
}
time_t GetFromDateTime()
{
return fromDateTime;
}
void SetFromDateTime(time_t fromDateTime)
{
this->fromDateTime = fromDateTime;
}
time_t GetToDateTime()
{
return toDateTime;
}
void SetToDateTime(time_t toDateTime)
{
this->toDateTime = toDateTime;
}
int GetId()
{
return id;
}
void SetId(int id)
{
this->id = id;
}
string GetName()
{
return name;
}
void SetName(string name)
{
this->name = name;
}
float GetCampaignCost()
{
return campaignCost;
}
void SetCampaignCost(float campaignCost)
{
this->campaignCost = campaignCost;
}
bool IsActive()
{
time_t now = time(NULL);
if (fromDateTime <= now && toDateTime >= now)
{
return true;
}
return false;
}
bool CommitAdvertisement(Ad ad)
{
for (Ad i : ads)
{
if (i.GetId() == ad.GetId())
{
return false;
}
}
ads.push_back(ad);
return true;
}
bool DeleteAdvertisement(int id)
{
for (int i = 0; i < ads.size(); i++)
{
if (ads[i].GetId() == id)
{
ads.erase(ads.begin() + i);
return false;
}
}
return false;
}
vector<Ad> GetAllAds()
{
return ads;
}
};
广告类别
#pragma once
#include <string>
#include <vector>
#include <iostream>
#include <exception>
#include "AdType.h"
using namespace std;
class Ad
{
private:
string name;
string adText;
AdType adType;
int id;
public:
Ad(string name, string text, int id, AdType type = AdType::PLAINTEXT)
{
this->name = name;
this->adText = text;
this->id = id;
this->adType = type;
}
string GetName()
{
return name;
}
void SetName(string name)
{
this->name = name;
}
int GetId()
{
return id;
}
void SetId(int id)
{
this->id = id;
}
AdType GetType()
{
return adType;
}
void SetType(AdType type)
{
this->adType = type;
}
string GetText()
{
return adText;
}
void SetText(string adText)
{
this->adText = adText;
}
};
现在,我们已经成功地将广告系列添加到给定的客户,并且能够添加一个广告实例,但不能添加更多。
此代码段显示了此尝试:
// Create a customer
Customer *the_client = new Customer("Foobar Inc", 123);
// Create a campaign
Campaign *the_campaign = new Campaign(begin, end, 1234, "campaign_name", 123455.0f);
// Create an ad
Ad *the_first_ad = new Ad("First ad", "adtext", 12345656, AdType::PLAINTEXT);
// create another ad
Ad* the_second_ad = new Ad("Second ad", "adtext", 12345656, AdType::PLAINTEXT);
// add the campaign to the customer
the_client->AddCampaign(*the_campaign);
// Add the ad to the customers' list of ads
the_client->CommitAdvertisement(*the_first_ad, 1234);
// Print out how many ads there are in the given campaign
cout << "There are now " << the_client->GetAllAdsForCampaign(1234).size() << "ads int the campaign" << endl;
// Add the second ad to the customers' list of ads
the_client->CommitAdvertisement(*the_second_ad, 1234);
// Again - print out how many ads there are in the given campaign
cout << "There are now " << the_client->GetAllAdsForCampaign(1234).size() << "ads int the campaign" << endl;
很有可能我们完全错了,但是任何指导都值得欢迎。
答案 0 :(得分:1)
CommitAdvertisement
将返回false,如果已经存在具有相同ID的广告,则添加失败。
这就是您正在做的。第一个广告和第二个广告的ID均为12345656。因此,它可以按照您的要求进行操作。
增加the_second_ad
的ID。