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

运用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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值