{Unit which declares Classes ProductSpecification and the Promotion
Hierarchy.}
unit Products;
interface
uses BasicTypes;
type
{====== Promotion and its Subtypes =================}
Promotion = class
private
StartTimeAndDate,
EndTimeAndDate : TDateTime ;
public
constructor Create (Start, Finish : TDateTime) ;
function ToString : String; virtual ;
end ; {Promotion}
MForN = class (Promotion)
private
M, N : Integer ;
public
constructor Create (Start, Finish : TDateTime;
InitM, InitN : Integer) ;
function ToString : String; override ;
end ; {MForN}
Reduction = class (Promotion)
private
PercentageOff : Integer ;
public
constructor Create (Start, Finish : TDateTime;
PercentOff : Integer) ;
function ToString : String; override ;
end ; {Reduction}
{========== ProductSpecification ====================}
ProductSpecification = class
private
Description : String ;
Price : Money ;
Code : BarCode ;
ThePromotion : Promotion ;
public
function ToString : String ;
function HasPromotion : Boolean ;
function GetPromotion : Promotion ;
constructor Create (d : String; p : Money; c : BarCode ) ;
procedure SetPromotion (p : Promotion) ;
end ;{ProductSpecification}
{==================================================================}
implementation
uses SysUtils ;
{Promotion}
constructor Promotion.Create (Start, Finish : TDateTime) ;
begin
StartTimeAndDate := Start ;
EndTimeAndDate := Finish ;
end ;
function Promotion.ToString : String ;
begin
Result := FloatToStr(StartTimeAndDate) +
FloatToStr(EndTimeAndDate) ;
end ; {ToString}
{MForN}
constructor MForN.Create (Start, Finish : TDateTime;
InitM, InitN : Integer) ;
begin
inherited Create (Start, Finish) ;
M := InitM ;
N := InitN ;
end ;
function MForN.ToString : String ;
begin
Result := inherited ToString + ' ' + IntToStr (M) +
' for the price of ' + IntToStr (N) ;
end ; {ToString}
{Reduction}
constructor Reduction.Create (Start, Finish : TDateTime;
PercentOff : Integer) ;
begin
inherited Create (Start, Finish) ;
PercentageOff := PercentOff ;
end ; {Create}
function Reduction.ToString : String;
begin
Result := inherited ToString + 'Reduction = '
+ IntToStr (PercentageOff) ;
end ; {ToString}
{============ ProductSpecification ===================}
constructor ProductSpecification.Create (d : String;
p : Money; c : BarCode);
begin
Description := d ;
Price := p ;
Code := c ;
end ;
function ProductSpecification.ToString : String ;
var
Temp : String ;
begin
Temp := Description + ' ' + Price.ToString ;
if ThePromotion <> nil then
Result := Temp + ' ' + ThePromotion.ToString
{Promotion.ToString is dynamically bound}
else
Result := Temp ;
end ;
function ProductSpecification.HasPromotion : Boolean ;
begin
Result := (ThePromotion <> nil) ;
end ;
function ProductSpecification.GetPromotion : Promotion ;
begin
Result := ThePromotion ;
end ;
procedure ProductSpecification.SetPromotion (p : Promotion) ;
begin
ThePromotion := p ;
end ;
end.
{======================================================================}
{Unit defining Class Inventory}
unit Invent;
interface
uses Products ;
const
Size = 100 ;
type
Inventory = class
private
HowMany : Integer ;
Cursor : Integer ;
Store : array [1 .. 100] of ProductSpecification ;
public
constructor Create ;
procedure addItem (p : ProductSpecification) ;
{precondition: not isFull}
function IsFull : Boolean ;
procedure Reset ;
function getItem : ProductSpecification ;
{precondition: isMore}
function isMore : Boolean ;
end ;{Inventory}
var
TheInventory : Inventory ;
implementation
constructor Inventory.Create ;
begin
inherited Create ;
HowMany := 0 ;
end ; {Create}
procedure Inventory.AddItem (p : ProductSpecification) ;
{precondition: not isFull}
begin
HowMany := HowMany + 1 ;
Store [HowMany] := p ;
end ; {AddItem}
function Inventory.isFull : Boolean ;
begin
Result := (HowMany = Size);
end ; {isFull}
procedure Inventory.Reset ;
begin
Cursor := 1 ;
end ; {Reset}
function Inventory.isMore : Boolean ;
begin
Result := (Cursor <= HowMany) ;
end ; {isMore}
function Inventory.getItem : ProductSpecification ;
begin
Result := Store [Cursor] ;
Cursor := Cursor + 1 ;end ; {getItem}
end.