ssd5 网络拍卖 实验五
Bid.h
View Code
#ifndef BID_H
#define BID_H
#include <iostream>
#include <string>
#include "Date.h"
using namespace std;
class Bid;
istream &operator>>(istream &stream, Bid &b);
class Bid {
private:
string email;
float amount;
int quantity;
Date date;
///////
int winbid;
public:
Bid(void);
Bid(const Bid &b);
Bid (string email, float amount, int quantity, Date date);
virtual string getEmail () const ;
virtual float getAmount () const;
virtual int getQuantity () const;
virtual Date getDate () const;
////////////
virtual int getWinbid() const;
////////////
virtual void setEmail(const string&);
virtual void setAmount(const float&);
virtual void setQuantity(const int&);
virtual void setDate(const Date&);
//////
virtual void setWinbid(const int&);
/////
virtual bool operator< (const Bid &rhs) const;
virtual bool operator== (const Bid &rhs) const;
};
#endif
Bid.cpp
View Code
#include "Bid.h"
Bid::Bid(void)
{
this->email="";
this->amount=0;
this->quantity=0;
this->date=Date();
}
Bid::Bid(const Bid&b)
{
this->email = b.getEmail();
this->amount= b.getAmount();
this->quantity=b.getQuantity();
this->date = b.getDate();
/////
this->winbid = b.getWinbid();
/////
}
Bid::Bid(string email,float amount, int quantity,Date date)
{
this->amount = amount;
this->email = email;
this->quantity = quantity;
this->date = date;
}
string Bid::getEmail()const
{
return this->email;
}
float Bid::getAmount()const
{
return this->amount;
}
int Bid::getQuantity()const
{
return this->quantity;
}
Date Bid::getDate()const
{
return this->date;
}
///////////
int Bid::getWinbid()const
{
return this->winbid;
}
void Bid::setWinbid(const int& w)
{
this->winbid = w;
}
///////////
void Bid::setEmail(const string&e)
{
this->email = e;
}
void Bid::setAmount(const float&a)
{
this->amount=a;
}
void Bid::setQuantity(const int&q)
{
this->quantity = q;
}
void Bid::setDate(const Date&d)
{
this->date = d;
}
bool Bid::operator <(const Bid&rhs)const
{
if(this->amount<rhs.getAmount())return true;
else return false;
}
bool Bid::operator ==(const Bid&rhs)const
{
if(this->amount==rhs.getAmount())return true;
else return false;
}
istream &operator>>(istream &stream,Bid &b)
{
int q;
float a;
string e;
Date d;
stream>>e>>a>>q>>d;
b.setAmount(a);
b.setDate(d);
b.setEmail(e);
b.setQuantity(q);
return stream;
}
Advertisement.h
View Code
#ifndef ADVERTISEMENT_H
#define ADVERTISEMENT_H
#include <queue>
#include <iostream>
#include <string>
#include "Date.h"
#include "Bid.h"
using namespace std;
class Advertisement;
istream &operator>>(istream &stream, Advertisement &a);
class Advertisement {
private:
int number;
int quantity;
string title;
string seller_email;
string body;
Date start;
Date close;
priority_queue<Bid> bids;
public:
Advertisement(void);
Advertisement(const Advertisement &a);
Advertisement (string title, string seller_email, string body,
Date start, Date close, int quantity);
virtual void setStart (const Date &start);
virtual void setClose (const Date &close);
virtual void setTitle (string title);
virtual void setBody (string body);
virtual void setNumber (int number);
virtual void setEmail (string email);
virtual void setQuantity (int quantity);
virtual Date getStart () const;
virtual Date getClose () const;
virtual string getTitle() const;
virtual string getBody() const;
virtual string getEmail() const;
virtual int getNumber () const;
virtual int getQuantity() const;
virtual bool operator==(const Advertisement&) const;
virtual priority_queue<Bid>& getBids(void);
virtual vector<Bid> getTopDutchBids (void) const;
};
#endif
Advertisement.cpp
View Code
#include <iostream>
#include "Advertisement.h"
using namespace std;
Advertisement::Advertisement()
{
this->number = 0;
this->quantity=0;
this->seller_email=this->title=this->body="";
this->start=Date();
this->close=Date();
}
Advertisement::Advertisement(const Advertisement &a)
{
this->quantity=a.getQuantity();
this->title = a.getTitle();
this->seller_email=a.getEmail();
this->body=a.getBody();
this->start=a.getStart();
this->close=a.getClose();
this->number = a.getNumber();
}
Advertisement::Advertisement(std::string title, std::string seller_email, std::string body, Date start, Date close, int quantity)
{
this->quantity=quantity;
this->title = title;
this->seller_email=seller_email;
this->body=body;
this->start=start;
this->close=close;
this->number = 0;
}
void Advertisement::setStart(const Date &start)
{
this->start = start;
}
void Advertisement::setClose(const Date &close)
{
this->close = close;
}
void Advertisement::setBody(std::string body)
{
this->body = body;
}
void Advertisement::setEmail(std::string email)
{
this->seller_email = email;
}
void Advertisement::setNumber(int number)
{
this->number = number;
}
void Advertisement::setQuantity(int quantity)
{
this->quantity = quantity;
}
void Advertisement::setTitle(std::string title)
{
this->title = title;
}
string Advertisement::getBody() const
{
return this->body;
}
string Advertisement::getEmail() const
{
return this->seller_email;
}
string Advertisement::getTitle() const
{
return this->title;
}
Date Advertisement::getStart() const
{
return this->start;
}
Date Advertisement::getClose() const
{
return this->close;
}
int Advertisement::getNumber()const
{
return this->number;
}
int Advertisement::getQuantity()const
{
return this->quantity;
}
bool Advertisement::operator==(const Advertisement& a)const
{
if(this->number==a.getNumber())
return true;
else return false;
}
istream &operator>>(istream &stream, Advertisement &a)
{
string title,email,body;
int quantity;
Date start,close;
stream>>title>>email>>quantity>>start>>close>>body;
a.setTitle(title);
a.setBody(body);
a.setEmail(email);
a.setStart(start);
a.setClose(close);
a.setQuantity(quantity);
return stream;
}
priority_queue<Bid>& Advertisement::getBids(void)
{
return this->bids;
}
vector<Bid> Advertisement::getTopDutchBids(void) const
{
priority_queue<Bid> pb = this->bids;
vector<Bid> b;
Bid bid;
int quantity = this->quantity;
while(!pb.empty()&&quantity>0)
{
bid = pb.top();
quantity -=bid.getQuantity();
if(quantity<0)
{
bid.setWinbid(quantity+bid.getQuantity());
b.push_back(bid);
quantity = 0;
}
else
{
bid.setWinbid(bid.getQuantity());
b.push_back(bid);
pb.pop();
}
}
return b;
}
Client.h
View Code
#ifndef CLIENT_H
#define CLIENT_H
#include <string>
#include <vector>
#include "Date.h"
using namespace std;
class Client;
istream &operator>>(istream &stream, Client &c);
class Client {
private:
string fname;
string lname;
string email;
string passwd;
vector<int> offerings;
vector<int> bids;
public:
Client(void);
Client(Client const &c);
Client (string &fname, string &lname, string &email, string &passwd);
virtual void setFname(const string&);
virtual void setLname(const string&);
virtual void setEmail(const string&);
virtual void setPasswd(const string&);
virtual string getFname () const;
virtual string getLname () const;
virtual string getEmail () const;
virtual string getPasswd () const;
virtual vector<int>::iterator beginOfferings();
virtual vector<int>::iterator endOfferings();
virtual vector<int>::iterator beginBids();
virtual vector<int>::iterator endBids();
virtual void addBid (int item);
virtual void addOffering (int item);
virtual bool verifyPasswd(string passwd);
};
#endif
Client.cpp
View Code
#include <iostream>
#include <string.h>
#include "Client.h"
using namespace std;
Client::Client()
{
this->fname = "";
this->email = "";
this->lname = "";
this->passwd= "";
}
Client::Client(const Client &c)
{
this->fname = c.getFname();
this->lname = c.getLname();
this->email = c.getEmail();
this->passwd = c.getPasswd();
}
Client::Client(std::string &fname, std::string &lname, std::string &email, std::string &passwd)
{
this->fname = fname;
this->lname = lname;
this->email = email;
this->passwd = passwd;
}
void Client::setEmail(const std::string &email)
{
this->email = email;
}
void Client::setFname(const std::string &fname)
{
this->fname = fname;
}
void Client::setPasswd(const std::string &passwd)
{
this->passwd = passwd;
}
void Client::setLname(const std::string &lname)
{
this->lname = lname;
}
string Client::getFname() const
{
return this->fname;
}
string Client::getLname() const
{
return this->lname;
}
string Client::getEmail() const
{
return this->email;
}
string Client::getPasswd() const
{
return this->passwd;
}
bool Client::verifyPasswd(std::string passwd)
{
if(this->passwd == passwd)return true;
else return false;
}
istream &operator>>(istream &stream, Client &c)
{
string fname,lname,email,passwd;
stream>>fname>>lname>>email>>passwd;
c.setFname(fname);
c.setLname(lname);
c.setEmail(email);
c.setPasswd(passwd);
return stream;
}
/////////////////
vector<int>::iterator Client::beginBids()
{
return this->bids.begin();
}
vector<int>::iterator Client::endBids()
{
return this->bids.end();
}
vector<int>::iterator Client::beginOfferings()
{
return this->offerings.begin();
}
vector<int>::iterator Client::endOfferings()
{
return this->offerings.end();
}
void Client::addBid(int item)
{
this->bids.push_back(item);
}
void Client::addOffering(int item)
{
this->offerings.push_back(item);
}
Function displayBidHistory
View Code
void displayBidHistory(ostringstream &oss, Advertisement* ad)
{
Client* client =NULL;
client = users[ad->getEmail()];
int bid_number = 0;
vector<Bid> bids = ad->getTopDutchBids();
oss << "<table border=0 width=100%><tr><td align=center width=15%>" << endl;
oss << "<tr>" << endl;
oss << "<B>" << "AD Title: </B>" << ad->getTitle() << "<br>" <<endl;
oss << "<B>" << "LastName: </B>" << client->getLname() << "<br>" <<endl;
oss << "<B>" << "FirstName: </B>" << client->getFname() << "<br>" <<endl;
oss << "<B>" << "Posted: </B>" << ad->getStart() << "<br>" <<endl;
oss << "<B>" << "Closes: </B>" << ad->getClose() << "<br>" <<endl;
oss << "<B>" << "Quantity: </B>" << ad->getQuantity() << "<br>" <<endl;
oss << "<B>" << "Total number of bids: </B>" <<ad->getBids().size() << "<br><br><br>" <<endl;
oss << "<B>" << "The highest bid list</B><br><br>" << endl;
for(vector<Bid>::iterator it = bids.begin(); it!=bids.end(); it++)
{
oss << "NO.<B>" << ++bid_number << "</B><br>"<<endl;
oss << "Bidder's email: <B>" << it->getEmail() << "</B><br>" <<endl;
oss << "The dollar amount of the bid: <B>" << it->getAmount() << "$</B><br>" << endl;
oss << "The number of items bid on: <B>" << it->getQuantity() << "</B><br>" << endl;
oss << "The number of items that the bid is currently winning: <B>" << it->getWinbid() << "</B><br>" <<endl;
oss << "Auction Date: <B>" << it->getDate() << "</B><br><br>" <<endl;
}
oss << "<br></tr></table>" <<endl;
}
posted on 2011-09-13 18:10 NewPanderKing 阅读(501) 评论(0) 编辑 收藏 举报