Browse Source

* Fix from Dean Zobec, undoing some earlier workaround

git-svn-id: trunk@5263 -
michael 19 years ago
parent
commit
036207803b
1 changed files with 27 additions and 45 deletions
  1. 27 45
      fcl/fpcunit/exampletests/money.pp

+ 27 - 45
fcl/fpcunit/exampletests/money.pp

@@ -62,10 +62,8 @@ type
 
 
   TMoneyBag = class(TInterfacedObject, IMoney)
   TMoneyBag = class(TInterfacedObject, IMoney)
   private
   private
-    FMonies: TFPList;
-    function AddToMoniesList(const Item: IInterface): Integer;
-    function RemoveFromMoniesList(const Item: IInterface): Integer;
-    function FindMoney(aCurrencyUnit: string): ISingleCurrencyMoney;
+    FMonies: TInterfaceList;
+    function FindMoney(aCurrencyUnit: string): Integer;
     function Contains(m: ISingleCurrencyMoney): boolean;
     function Contains(m: ISingleCurrencyMoney): boolean;
   public
   public
     constructor Create;
     constructor Create;
@@ -109,45 +107,31 @@ begin
   c2 := c.multiply(i);
   c2 := c.multiply(i);
 end;
 end;
 
 
-function TMoneyBag.AddToMoniesList(const Item: IInterface): Integer;
-begin
-  Result := FMonies.Add(nil);
-  IInterface(FMonies.List^[Result]) := Item;
-end;
 
 
-function TMoneyBag.RemoveFromMoniesList(const Item: IInterface): Integer;
-begin
-  Result := FMonies.IndexOf(Pointer(Item));
-  if Result > -1 then
-  begin
-    IInterface(FMonies.List^[Result]) := nil;
-    FMonies.Delete(Result);
-  end;
-end;
-
-function TMoneyBag.FindMoney(aCurrencyUnit: string): ISingleCurrencyMoney;
+function TMoneyBag.FindMoney(aCurrencyUnit: string): Integer;
 var
 var
   i: Integer;
   i: Integer;
 begin
 begin
   for i := 0 to FMonies.Count - 1 do
   for i := 0 to FMonies.Count - 1 do
-    if ISingleCurrencyMoney(FMonies.items[i]).CurrencyUnit = aCurrencyUnit then
+    if (FMonies.items[i] as ISingleCurrencyMoney).CurrencyUnit = aCurrencyUnit then
     begin
     begin
-      Result := ISingleCurrencyMoney(FMonies.items[i]);
+      Result := i;
       Exit;
       Exit;
     end;
     end;
+  result := -1;
 end;
 end;
 
 
 function TMoneyBag.Contains(m: ISingleCurrencyMoney): boolean;
 function TMoneyBag.Contains(m: ISingleCurrencyMoney): boolean;
 var
 var
-  found: ISingleCurrencyMoney;
+  idx: integer;
 begin
 begin
-  found := FindMoney(m.CurrencyUnit);
-  if found = nil then
+  idx := FindMoney(m.CurrencyUnit);
+  if idx = -1 then
   begin
   begin
     Result := false;
     Result := false;
     Exit;
     Exit;
   end;
   end;
-  Result := (found.Amount = m.amount);
+  Result := ((FMonies[idx] as ISingleCurrencyMoney).Amount = m.amount);
 end;
 end;
 
 
 class function TMoneyBag.CreateWith(m1: IMoney; m2: IMoney): IMoney;
 class function TMoneyBag.CreateWith(m1: IMoney; m2: IMoney): IMoney;
@@ -162,15 +146,11 @@ end;
 
 
 constructor TMoneyBag.Create;
 constructor TMoneyBag.Create;
 begin
 begin
-  FMonies := TFPList.Create;
+  FMonies := TInterfaceList.Create;
 end;
 end;
 
 
 destructor TMoneyBag.Destroy;
 destructor TMoneyBag.Destroy;
-var
-  i: integer;
 begin
 begin
-  for i := 0 to FMonies.Count - 1 do
-    IInterface(FMonies.items[i])._release;
   FMonies.Free;
   FMonies.Free;
   inherited Destroy;
   inherited Destroy;
 end;
 end;
@@ -178,7 +158,7 @@ end;
 function TMoneyBag.Simplify: IMoney;
 function TMoneyBag.Simplify: IMoney;
 begin
 begin
   if FMonies.Count = 1 then
   if FMonies.Count = 1 then
-    Result := IMoney(FMonies.items[0])
+    Result := FMonies.items[0] as IMoney
   else
   else
     Result := Self;
     Result := Self;
 end;
 end;
@@ -203,25 +183,27 @@ var
   i: integer;
   i: integer;
 begin
 begin
   for i := 0 to aBag.FMonies.Count - 1 do
   for i := 0 to aBag.FMonies.Count - 1 do
-    appendMoney(ISingleCurrencyMoney(aBag.FMonies.Items[i]));
+    appendMoney(aBag.FMonies.Items[i] as ISingleCurrencyMoney);
 end;
 end;
 
 
 procedure TMoneyBag.appendMoney(aMoney: ISingleCurrencyMoney);
 procedure TMoneyBag.appendMoney(aMoney: ISingleCurrencyMoney);
 var
 var
+  i: integer;
   old: IMoney;
   old: IMoney;
   sum: IMoney;
   sum: IMoney;
 begin
 begin
   if aMoney.isZero then Exit;
   if aMoney.isZero then Exit;
-  old := findMoney(aMoney.CurrencyUnit);
-  if old = nil then
+  i := FindMoney(aMoney.CurrencyUnit);
+  if i = -1 then
   begin
   begin
-    AddToMoniesList(aMoney);
+    FMonies.add(aMoney);
     Exit;
     Exit;
   end;
   end;
+  old := FMonies[i] as IMoney;
   sum := old.Add(aMoney);
   sum := old.Add(aMoney);
-  RemoveFromMoniesList(old);
+  FMonies.Delete(i);
   if sum.isZero then Exit;
   if sum.isZero then Exit;
-  AddToMoniesList(sum);
+  FMonies.Add(sum);
 end;
 end;
 
 
 function TMoneyBag.isZero: boolean;
 function TMoneyBag.isZero: boolean;
@@ -237,8 +219,8 @@ begin
   if factor <> 0 then
   if factor <> 0 then
     for i := 0 to FMonies.Count - 1 do
     for i := 0 to FMonies.Count - 1 do
     begin
     begin
-      TMoneyBag(Result._Self).appendMoney(ISingleCurrencyMoney(
-      ISingleCurrencyMoney(FMonies.items[i]).Multiply(factor)));
+      TMoneyBag(Result._Self).appendMoney(
+      (FMonies.items[i] as ISingleCurrencyMoney).Multiply(factor) as ISingleCurrencyMoney);
     end;
     end;
 end;
 end;
 
 
@@ -249,8 +231,8 @@ begin
   Result := TMoneyBag.Create;
   Result := TMoneyBag.Create;
   for i := 0 to FMonies.Count - 1 do
   for i := 0 to FMonies.Count - 1 do
   begin
   begin
-    TMoneyBag(Result._Self).appendMoney(ISingleCurrencyMoney(
-      ISingleCurrencyMoney(FMonies.items[i]).negate));
+    TMoneyBag(Result._Self).appendMoney(
+(FMonies.items[i] as ISingleCurrencyMoney).negate as ISingleCurrencyMoney);
   end;
   end;
 end;
 end;
 
 
@@ -270,7 +252,7 @@ var
 begin
 begin
   Result := '{';
   Result := '{';
   for i := 0 to FMonies.Count - 1 do
   for i := 0 to FMonies.Count - 1 do
-    Result := Result + ISingleCurrencyMoney(FMonies.items[i]).ToString;
+    Result := Result + (FMonies.items[i] as IMoney).ToString;
   Result := Result + '}';
   Result := Result + '}';
 end;
 end;
 
 
@@ -300,7 +282,7 @@ begin
     end;
     end;
     for i := 0 to FMonies.Count - 1 do
     for i := 0 to FMonies.Count - 1 do
     begin
     begin
-      ism := ISingleCurrencyMoney(FMonies.items[i]);
+      ism := FMonies.items[i] as ISingleCurrencyMoney;
       if not aMoneyBag.Contains(ism) then
       if not aMoneyBag.Contains(ism) then
       begin
       begin
         Result := false;
         Result := false;
@@ -400,7 +382,7 @@ begin
          Result := m.isZero;
          Result := m.isZero;
     if m._Self.ClassType = TMoney  then
     if m._Self.ClassType = TMoney  then
     begin
     begin
-      ism := ISingleCurrencyMoney(m);
+      ism := m as ISingleCurrencyMoney;
        Result := (ism.Amount = Amount) and
        Result := (ism.Amount = Amount) and
           (ism.CurrencyUnit = CurrencyUnit)
           (ism.CurrencyUnit = CurrencyUnit)
     end
     end