运用Replace Temp with Query把statement()中的totalAmount 和 frequentRenterPoints临时变量去掉:
用Customer类的getTotalCharge()取代totalAmount
用Customer类的getTotalFrequentRenterPoints()取代frequentRenterPoints
最后,我们添加htmlStatement()以及测试代码
完成后的customer类及测试代码如下:
customer.h
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include <string>
#include <vector>
#include "rental.h"
using std::string;
using std::vector;
class Customer
{
public:
Customer(string name);
~Customer();
void addRental(Rental arg);
vector<Rental>& getRentals();
string getName();
string statement();
string htmlStatement();
double getTotalCharge();
int getTotalFrequentRenterPoints();
private:
string _name;
vector<Rental> _rentals;
};
#endif //CUSTOMER_H
customer.cpp:
#include "customer.h"
#include "movie.h"
#include <boost/lexical_cast.hpp>
Customer::Customer(string name)
:_name(name)
{
_rentals = vector<Rental>();
}
Customer::~Customer()
{
_rentals.clear();
}
void Customer::addRental(Rental arg)
{
_rentals.push_back(arg);
}
vector<Rental>& Customer::getRentals()
{
return _rentals;
}
string Customer::getName()
{
return _name;
}
string Customer::statement()
{
string result = "Rental Record for " + getName() + "\n";
vector<Rental>::iterator iterVec = _rentals.begin();
for (; iterVec != _rentals.end(); ++iterVec)
{
Rental each = *iterVec;
// show figures for this rental
result += "\t" + each.getMovie().getTitle() + "\t" + boost::lexical_cast<string>(each.getCharge());
}
// add footer lines
result += "Amount owed is " + boost::lexical_cast<string>(getTotalCharge()) + "\n";
result += "You earned " + boost::lexical_cast<string>(getTotalFrequentRenterPoints()) + "frequent renter points";
return result;
}
string Customer::htmlStatement()
{
string result = "<H1>Rentals for<EM>" + getName() + "</EM></H1><P>\n";
vector<Rental>::iterator iterVec = _rentals.begin();
for (; iterVec != _rentals.end(); ++iterVec)
{
Rental each = *iterVec;
// show figures for this rental
result += each.getMovie().getTitle() + ": " + boost::lexical_cast<string>(each.getCharge()) + "<BR>\n";
}
// add footer lines
result += "<P>You owe <EM>" + boost::lexical_cast<string>(getTotalCharge()) + "</EM><P>\n";
result += "On this rental you earned <EM>" + boost::lexical_cast<string>(getTotalFrequentRenterPoints()) + "</EM> frequent renter points<P>";
return result;
}
double Customer::getTotalCharge()
{
double result = 0;
vector<Rental>::iterator iterVec = _rentals.begin();
for (; iterVec != _rentals.end(); ++iterVec)
{
Rental each = *iterVec;
result += each.getCharge();
}
return result;
}
int Customer::getTotalFrequentRenterPoints()
{
int result = 0;
vector<Rental>::iterator iterVec = _rentals.begin();
for (; iterVec != _rentals.end(); ++iterVec)
{
Rental each = *iterVec;
result += each.getFrequentRenterPoints();
}
return result;
}
test code:
#include "customer.h"
#include "movie.h"
#include "rental.h"
#include <boost/lexical_cast.hpp>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
// set up movie information
vector<Movie> movies;
for (int i = 0; i < 30; ++i)
{
Movie tempMovie("Movie" + boost::lexical_cast<string>(i+1), i+1);
movies.push_back(tempMovie);
}
// set up rental information on top of movies
// set up customer information on top of rentals
vector<Customer> customers;
for (int i = 0; i < 5; ++i)
{
Customer tempCust("customer" + boost::lexical_cast<string>(i+1));
for (int j = 6 * i; j < 6 * i + 6; ++j)
{
Movie tempMovie = movies[j];
Rental tempRent(tempMovie, i+1); // set max rental days to 15
tempCust.addRental(tempRent);
}
customers.push_back(tempCust);
}
// test code
//(1) print out information of all movies
const vector<Movie>::size_type numMovies = movies.size();
for (int i = 0; i < numMovies; ++i)
{
cout << "the Title of the " << i + 1 << " th movie and its price is : (" << movies[i].getTitle() << ", " << movies[i].getPriceCode() << ")" << std::endl;
}
cout << endl;
// (2) print out information of all customers
const vector<Customer>::size_type numCustomers = customers.size();
for (int i = 0; i < numCustomers; ++i)
{
Customer tempCust = customers[i];
cout << "the " << i+1 << " th customer '" << tempCust.getName() << "' has rented these movies:" << endl;
const vector<Rental>::size_type numRentals = tempCust.getRentals().size();
for (int j = 0; j < numRentals; ++j)
{
cout << " " << "( " << tempCust.getRentals()[j].getMovie().getTitle() << ", " << tempCust.getRentals()[j].getDaysRented() << ")" << endl;
}
}
cout << endl;
// (3) test statement() in Customer
for (int i = 0; i < numCustomers; ++i)
{
Customer tempCust = customers[i];
cout << tempCust.statement() << endl;
}
cout << endl << endl;
// (4) test htmlStatement() in Customer
for (int i = 0; i < numCustomers; ++i)
{
Customer tempCust = customers[i];
cout << tempCust.htmlStatement() << endl;
}
return 0;
}