《重构--改善既有代码的设计》读书笔记之三:分解并重组statement() part2

来源:互联网 发布:淘宝手机助手2017 编辑:程序博客网 时间:2024/05/22 15:04

运用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 rentalresult += "\t" + each.getMovie().getTitle() + "\t" + boost::lexical_cast<string>(each.getCharge());}// add footer linesresult += "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 rentalresult += each.getMovie().getTitle() + ": " + boost::lexical_cast<string>(each.getCharge()) + "<BR>\n";}// add footer linesresult += "<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 informationvector<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 rentalsvector<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 15tempCust.addRental(tempRent);}customers.push_back(tempCust);}// test code//(1) print out information of all moviesconst 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 customersconst 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 Customerfor (int i = 0; i < numCustomers; ++i){Customer tempCust = customers[i];cout << tempCust.statement() << endl;}cout << endl << endl;// (4) test htmlStatement() in Customerfor (int i = 0; i < numCustomers; ++i){Customer tempCust = customers[i];cout << tempCust.htmlStatement() << endl;}return 0;}


原创粉丝点击