Language Delphi
(OO Simple version (not using Interfaces)
| Date: | 09/04/05 |
| Author: | Luis Carlos F. Dias |
| URL: | n/a |
| Comments: | 0 |
| Info: | n/a |
| Score: |
//------------------ File Singers.pas ------------------
unit Singers;
interface
uses
SysUtils ;
type
TAbstractSinger = class
private
FLine: String ;
FCanSing: boolean ;
function GetLine: String ; virtual ;
procedure SetLine(aLine: String) ; virtual ;
public
property CanSing: boolean read FCanSing write FCanSing ;
property Line: String read GetLine write SetLine ;
function Sing: String ; virtual ; abstract ;
end ;
TBottlesSinger = class(TAbstractSinger)
private
FCounter: integer ;
function EvalS: String ;
procedure SetCounter(aValue: integer) ;
procedure PrepareLine ;
property Counter: integer read FCounter write SetCounter;
public
constructor Create ;
function Sing: String ; override ;
end ;
const
CRLF: String = '' + #13#10 ;
implementation
{ TAbstractSinger }
function TAbstractSinger.GetLine: String;
begin
result := FLine ;
end;
procedure TAbstractSinger.SetLine(aLine: String);
begin
FLine := aLine ;
end;
{ TBottlesSinger }
constructor TBottlesSinger.Create;
begin
inherited;
FCounter := 99 ;
CanSing := true ;
end;
function TBottlesSinger.EvalS: String;
var
res : string ;
begin
res := 's' ;
if Counter = 1 then
res := '' ;
result := res ;
end;
procedure TBottlesSinger.PrepareLine;
var
lineForOne, lineForMore, lineForNone: String ;
begin
lineForMore := inttostr(Counter) +
' bottles of beer on the wall, ' + inttostr(Counter) +
' bottles of beer.' + CRLF +
'Take one down and pass it around, ' + inttostr(Counter-1) +
' bottle' + EvalS + ' of beer on the wall.' + CRLF ;
lineForOne := '1 bottle of beer on the wall, 1 bottle of beer.' + CRLF +
'Take one down and pass it around,' +
' no more bottles of beer on the wall.' + CRLF ;
lineForNone := 'No more bottle of beer on the wall,' +
' no more bottles of beer.' + CRLF +
'Go to the store and buy some more,' +
' 99 bottles of beer on the wall.' + CRLF ;
case Counter of
1 : begin
Line := lineForOne ;
Counter := Counter-1 ;
end ;
0 : begin
Line := lineForNone ;
Counter := 99 ;
CanSing := false ;
end ;
else
begin
Line := lineForMore ;
Counter := Counter-1 ;
end ;
end ; {case}
end;
procedure TBottlesSinger.SetCounter(aValue: integer);
begin
FCounter := aValue ;
end;
function TBottlesSinger.Sing: String;
begin
PrepareLine ;
result := Line ;
end;
end. // end of file Singers.pas
//--------------- File Bottles.dpr ----------------------
program Bottles;
{$APPTYPE CONSOLE}
uses
SysUtils,
Singers in 'Singers.pas';
var
Singer: TAbstractSinger ;
begin
Singer := TBottlesSinger.Create ;
While Singer.CanSing do
WriteLn(Singer.Sing) ;
FreeAndNil(Singer) ;
end. // end of file Bottles.dpr
Download Source | Write Comment
Alternative Versions
| Version | Author | Date | Comments | Rate |
|---|---|---|---|---|
| OO version - Delphi 7 | Luis Carlos F. Dias | 09/10/05 | 9 | |
| Recursive version | Juan Carlos Molinos | 01/12/06 | 2 | |
| standard version | Anonymous | 04/20/05 | 4 | |
| NonVCL version | Philipp Winterberg | 04/20/05 | 0 | |
| OO version (with interface) | Luis Carlos F. Dias | 09/05/05 | 0 |
Download Source | Write Comment
Add Comment
Please provide a value for the fields Name,
Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.
Please don't post large portions of code here! Use the form to submit new examples or updates instead!
Comments