概述
示例:影片出租店程序(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;
最后
以上就是追寻路人为你收集整理的《重构》 — Delphi示例:影片出租店程序(1、重构前)的全部内容,希望文章能够帮你解决《重构》 — Delphi示例:影片出租店程序(1、重构前)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复