为了更好地理解《重构》一书中的思想,将其代码用C++形式表现出来并结合书本内容逐步重构。
movie.h:
#ifndef MOVIE_H
#define MOVIE_H
#include <string>
using std::string;
class Movie
{
public:
Movie(string title = "empty", int priceCode = 0);
int getPriceCode();
void setPriceCode(int arg);
string getTitle();
public:
static const int REGULAR;
static const int NEW_RELEASE;
static const int CHILDRENS;
private:
string _title;
int _priceCode;
};
#endif //MOVIE_H
movie.cpp:
#include "movie.h"
const int Movie::REGULAR = 0;
const int Movie::NEW_RELEASE = 1;
const int Movie::CHILDRENS = 2;
Movie::Movie(string title , int priceCode)
{
_title = title;
_priceCode = priceCode;
}
int Movie::getPriceCode()
{
return _priceCode;
}
void Movie::setPriceCode(int arg)
{
_priceCode = arg;
}
string Movie::getTitle()
{
return _title;
}
rental.h:
#ifndef RENTAL_H
#define RENTAL_H
#include "movie.h"
class Rental
{
public:
Rental(Movie movie, int daysRented);
int getDaysRented();
Movie getMovie();
private:
Movie _movie;
int _daysRented;
};
#endif //RENTAL_H
rental.cpp:
#include "rental.h"
Rental::Rental(Movie movie, int daysRented)
{
_movie = movie;
_daysRented = daysRented;
}
int Rental::getDaysRented()
{
return _daysRented;
}
Movie Rental::getMovie()
{
return _movie;
}
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();
private:
string _name;
vector<Rental> _rentals;
};
#endif //CUSTOMER_H
customer.cpp:
#include "customer.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()
{
double totalAmount = 0;
int frequentRenterPoints = 0;
string result = "Rental Record for " + getName() + "\n";
vector<Rental>::iterator iterVec = _rentals.begin();
for (; iterVec != _rentals.end(); ++iterVec)
{
double thisAmout = 0;
Rental each = *iterVec;
// determine amounts for each line
switch(each.getMovie().getPriceCode())
{
//case Movie::REGULAR:
case 0:
thisAmout += 2;
if (each.getDaysRented() > 2)
{
thisAmout += (each.getDaysRented() - 2) * 1.5;
}
break;
//case Movie::NEW_RELEASE:
case 1:
thisAmout += each.getDaysRented() * 3;
break;
//case Movie::CHILDRENS:
case 2:
thisAmout += 1.5;
if (each.getDaysRented() > 3)
{
thisAmout += (each.getDaysRented() - 3) * 1.5;
}
break;
}
// add frequent renter points
++frequentRenterPoints;
// add bonus for a two day new release rental
if ((each.getMovie().getPriceCode() == Movie::NEW_RELEASE) && each.getDaysRented() > 1)
{
++frequentRenterPoints;
}
// show figures for this rental
result += "\t" + each.getMovie().getTitle() + "\t" + boost::lexical_cast<string>(thisAmout);
}
// add footer lines
result += "Amount owed is " + boost::lexical_cast<string>(totalAmount) + "\n";
result += "You earned " + boost::lexical_cast<string>(frequentRenterPoints) + "frequent renter points";
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;
}
return 0;
}