《重构》 — Delphi示例:影片出租店程序(1、重构前)

示例:影片出租店程序(1、重构前)
代码:
unit uMovie;

interface

uses
    SysUtils,Contnrs;

const
    REGULAR = 0;
    NEW_RELEASE = 1;
    CHILDRENS = 2;

type
    TEnumeration = class
    private
        FList: TObjectList;
        FIndex: integer;
    public
        constructor Create(const AList: TObjectList);
        //---
        function HasMoreElements: boolean;
        function NextElement: TObject;
    end;

    //--影片
    TMovie = class
    private
        FTitle: string; //--名称
        FPriceCode: integer; //--价格(代号)
        function GetPriceCode: integer;
        function GetTitle: string;
        procedure SetPriceCode(const Value: integer);
    public
        constructor Create(const ATitle: string; APriceCode: integer);
        //---
        property Title: string read GetTitle;
        property PriceCode: integer read GetPriceCode write SetPriceCode;
    end;

    //--租赁
    TRental = class
    private
        FMovie: TMovie;
        FDaysRented: integer; //--租期
        function GetDaysRented: integer;
        function GetMovie: TMovie;
    public
        constructor Create(const AMovie: TMovie; ADaysRented: integer);
        //---
        property Movie: TMovie read GetMovie;
        property DaysRented: integer read GetDaysRented;
    end;

    //--顾客
    TCustomer = class
    private
        FName: string;
        FRentals: TObjectList;
        function GetName: string;
    public
        constructor Create(const AName: string);
        destructor Destroy; override;
        //---
        procedure AddRental(arg: TRental);
        function Statement: string; //--统计报表
        //---
        property Name: string read GetName;
    end;

implementation

constructor TMovie.Create(const ATitle: string; APriceCode: integer);
begin
    FTitle := ATitle;
    FPriceCode := APriceCode;
end;

function TMovie.GetPriceCode: integer;
begin
    Result := FPriceCode;
end;

function TMovie.GetTitle: string;
begin
    Result := FTitle;
end;

procedure TMovie.SetPriceCode(const Value: integer);
begin
    FPriceCode := Value;
end;

constructor TRental.Create(const AMovie: TMovie; ADaysRented: integer);
begin
    FMovie := AMovie;
    FDaysRented := ADaysRented;
end;

function TRental.GetDaysRented: integer;
begin
    Result := FDaysRented;
end;

function TRental.GetMovie: TMovie;
begin
    Result := FMovie;
end;

constructor TCustomer.Create(const AName: string);
begin
    FName := AName;
    FRentals := TObjectList.Create;
end;

destructor TCustomer.Destroy;
begin
    FRentals.Free;
    //---
    inherited;
end;

procedure TCustomer.AddRental(arg: TRental);
begin
    FRentals.Add(arg);
end;

function TCustomer.GetName: string;
begin
    Result := FName;
end;

function TCustomer.Statement: string;
var
    totalAmount: double; //--总消费金额
    frequentRenterPoints: integer; //--常客积点
    Rentals: TEnumeration;
    thisAmount: double;
    each: TRental;
begin
    Result := 'Rental Record for ' + self.Name + #13#10;
    //---
    totalAmount := 0;
    frequentRenterPoints := 0;
    Rentals := TEnumeration.Create(FRentals);
    while Rentals.HasMoreElements do
    begin
        thisAmount := 0;
        each := TRental(Rentals.NextElement); //--取得一笔租借记录
        //---
        case each.Movie.PriceCode of //--取得影片出租价格
            REGULAR: //--普通片
                begin
                    thisAmount := thisAmount + 2;
                    if each.DaysRented > 2 then
                        thisAmount := thisAmount + (each.DaysRented - 2) * 1.5;
                end;
            NEW_RELEASE: //--新片
                begin
                    thisAmount := thisAmount + each.DaysRented * 3;
                end;
            CHILDRENS: //--儿童片
                begin
                    thisAmount := thisAmount + 1.5;
                    if each.DaysRented > 3 then
                        thisAmount := thisAmount + (each.DaysRented - 3) * 1.5;
                end;
        end;
        //---累加常客积点
        frequentRenterPoints := frequentRenterPoints + 1;
        if (each.Movie.PriceCode = NEW_RELEASE) and (each.DaysRented > 1) then
            frequentRenterPoints := frequentRenterPoints + 1;
        //---显示此笔租借数据
        Result := Result + each.Movie.Title + ' ' + FloatToStr(thisAmount) + #13#10;
        totalAmount := totalAmount + thisAmount;
end;
    Rentals.Free;
    //---结尾打印
    Result := Result + 'Amount owed is ' + FloatToStr(totalAmount) + #13#10;
    Result := Result + 'You earned ' + IntToStr(frequentRenterPoints) + ' frequent renter points';
end;

constructor TEnumeration.Create(const AList: TObjectList);
begin
    FList := AList;
    FIndex := 0;
end;

function TEnumeration.HasMoreElements: boolean;
begin
    Result := FIndex < FList.Count;
end;

function TEnumeration.NextElement: TObject;
begin
    Result := FList[FIndex];
    FIndex := FIndex + 1;
end;

end.

procedure TForm1.Button1Click(Sender: TObject);
var
    ACustomer: TCustomer;
    AMovie1,AMovie2,AMovie3: TMovie;
begin
    AMovie1 := TMovie.Create('aaa',REGULAR);
    AMovie2 := TMovie.Create('bbb',NEW_RELEASE);
    AMovie3 := TMovie.Create('ccc',CHILDRENS);
    try
        ACustomer := TCustomer.Create('ZhangSan');
        try
            with ACustomer do
            begin
                AddRental(TRental.Create(AMovie1,1));
                AddRental(TRental.Create(AMovie2,1));
                AddRental(TRental.Create(AMovie3,1));
            end;
            ShowMessage(ACustomer.Statement);
        finally
            ACustomer.Free;
        end;
    finally
        AMovie1.Free;
        AMovie2.Free;
        AMovie3.Free;
    end;
end;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值