影片出租店 重构方法(1)

 影片出租店是《重构:改善既有代码的设计》里面的第一个例子。使用了多种重构方法。
影片出租店:计算每一位顾客的消费金额并打印详单。
操作者告诉程序:顾客租了哪些影片,租期多长,程序便根据租赁时间和影片类型算出费用。影片分为三类:普通片,儿童片和新片。除了计算费用,还要为常客计算积分,积分会根据租片种类是否为新片而有不同。

重构之前的代码:

Refector_1.h

#ifndef REFECTOR_1_H
#define REFECTOR_1_H
#include <QString>
#include <QVector>

class Movie
{
public:
    Movie(QString title, int priceCode);
    const static int CHILDREN = 2;
    const static int REGULAR = 0;
    const static int NEW_RELEASE = 1;
    int GetPriceCode();
    void SetPriceCode(int priceCode);
    QString GetTitle();

private:
    QString m_Title;
    int     m_PriceCode;
};



class Rental
{
public:
    Rental(Movie *movie, int daysRented);
    int GetDaysRented();
    Movie* GetMovie();
private:
    Movie* m_Movie;
    int m_DaysRented;


};

class Customer
{
public:
    Customer(QString name);
    void AddRental(Rental *rental);
    QString GetName();
    QVector<Rental *> GetRentals();
    QString Statement();
private:
    QString m_Name;
    QVector<Rental *> m_Rentals;

};

#endif // REFECTOR_1_H
Refector_1.cpp

#include "Refector_1.h"
#include <QDebug>


Movie::Movie(QString title, int priceCode)
{
    m_Title = title;
    m_PriceCode = priceCode;
}

int Movie::GetPriceCode()
{
    return m_PriceCode;
}

void Movie::SetPriceCode(int priceCode)
{
    m_PriceCode = priceCode;
}

QString Movie::GetTitle()
{
    return m_Title;
}

Rental::Rental(Movie *movie, int daysRented)
{
    m_Movie =movie;
    m_DaysRented = daysRented;
}

int Rental::GetDaysRented()
{
    return m_DaysRented;
}

Movie *Rental::GetMovie()
{
    return m_Movie;
}

Customer::Customer(QString name)
{
    m_Name = name;
}

void Customer::AddRental(Rental *rental)
{
    m_Rentals.push_back(rental);
}

QString Customer::GetName()
{
    return  m_Name;
}

QVector<Rental *> Customer::GetRentals()
{
    return  m_Rentals;
}

QString Customer::Statement()
{
    double TotalAmout = 0;
    int FrequentRenterPoints = 0;
    QString result = "Rental Record for " + GetName() + "\n";
    for (Rental *rental : GetRentals())
    {
        double ThisAmount = 0;
        switch (rental->GetMovie()->GetPriceCode())
        {
        case Movie::REGULAR:
            ThisAmount += 2;
            if (rental->GetDaysRented() > 2)
            {
                ThisAmount += (rental->GetDaysRented() - 2)*1.5;
            }
            break;
        case Movie::NEW_RELEASE:
            ThisAmount += rental->GetDaysRented() * 3;
            break;
        case Movie::CHILDREN:
            ThisAmount += 1.5;
            if (rental->GetDaysRented() > 3)
            {
                ThisAmount += (rental->GetDaysRented() - 3)*1.5;
            }
            break;
        }
        FrequentRenterPoints++;
        if (Movie::NEW_RELEASE == (rental->GetMovie()->GetPriceCode()) &&
                rental->GetDaysRented() > 1 )
        {
            FrequentRenterPoints++;
        }
        result += "\t" + rental->GetMovie()->GetTitle() + "\t" + QString("%1").arg(ThisAmount) + "\n";
        TotalAmout += ThisAmount;
    }

    result += "Amount owed is " + QString("%1").arg(TotalAmout) + "\n";
    result += "You earned " + QString("%1").arg(FrequentRenterPoints) + " frequent renter points";
    qDebug() << qPrintable(result) ;
    return  result;
}

main.cpp

    Movie movie1("Movie1", 0);
    Movie movie2("Movie2", 1);
    Movie movie3("Movie3", 2);
    Movie movie4("Movie4", 0);
    Movie movie5("Movie5", 1);
    Movie movie6("Movie6", 2);
    Movie movie7("Movie7", 0);
    Rental rental1(&movie1, 1);
    Rental rental2(&movie2, 2);
    Rental rental3(&movie3, 3);
    Rental rental4(&movie4, 4);
    Rental rental5(&movie5, 5);
    Rental rental6(&movie6, 6);
    Rental rental7(&movie7, 7);
    Customer ctmer("tester");
    ctmer.AddRental(&rental1);
    ctmer.AddRental(&rental2);
    ctmer.AddRental(&rental3);
    ctmer.AddRental(&rental4);
    ctmer.AddRental(&rental5);
    ctmer.AddRental(&rental6);
    ctmer.AddRental(&rental7);
    ctmer.Statement();

执行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

giantmfc123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值