program KassenSturz;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.Generics.Defaults,
System.Generics.Collections;
type
TWallet = TDictionary<Currency, UInt32>;
procedure MoveMoney(
const ASource: TWallet; AValue: Currency;
const ADest: TWallet);
var
Amount: Currency;
Values: TArray<Currency>;
Val: Currency;
begin
if not Assigned(ASource)
then
raise EArgumentNilException.Create('
ASource');
if not Assigned(ADest)
then
raise EArgumentNilException.Create('
ADest');
Amount := AValue;
Values := ASource.Keys.ToArray();
TArray.Sort<Currency>(Values, TComparer<Currency>.Construct(
function(
const Left, Right: Currency): Integer
begin
Result := -TComparer<Currency>.
Default.Compare(Left, Right);
end));
for Val
in Values
do
begin
while (Val <= Amount)
and (ASource[Val] > 0)
do
begin
ASource[Val] := ASource[Val] - 1;
if not ADest.ContainsKey(Val)
then
ADest.Add(Val, 1)
else
ADest[Val] := ADest[Val] + 1;
Amount := Amount - Val;
end;
end;
if Amount > 0
then
raise Exception.Create('
Fehlermeldung');
end;
procedure WriteWallet(
const Value: TWallet; IgnoreEmpty: Boolean = false);
var
Values: TArray<Currency>;
Val, Tot: Currency;
Cnt: Integer;
begin
Values := Value.Keys.ToArray();
TArray.Sort<Currency>(Values, TComparer<Currency>.Construct(
function(
const Left, Right: Currency): Integer
begin
Result := -TComparer<Currency>.
Default.Compare(Left, Right);
end));
Tot := 0;
for Val
in Values
do
begin
Cnt := Value[Val];
if IgnoreEmpty
and (Cnt = 0)
then
Continue;
Tot := Tot + Cnt * Val;
Writeln(
string.Format('
%0.2f x %d = %0.2f', [Val, Cnt, Val * Cnt]));
end;
Writeln('
======');
Writeln(
string.Format('
Total: %0.2f', [Tot]));
end;
procedure Main;
var
s, d: TWallet;
begin
s :=
nil;
d :=
nil;
try
s := TWallet.Create();
s.Add(500.00, 0);
s.Add(200.00, 1);
s.Add(100.00, 0);
s.Add(50.00, 0);
s.Add(20.00, 2);
s.Add(10.00, 0);
s.Add(5.00, 6);
s.Add(2.00, 9);
s.Add(1.00, 0);
s.Add(0.50, 10);
s.Add(0.20, 0);
s.Add(0.10, 0);
s.Add(0.05, 3);
s.Add(0.02, 0);
s.Add(0.01, 0);
d := TWallet.Create();
Writeln('
Source:');
WriteWallet(s, true);
Writeln('
Destination:');
WriteWallet(d, true);
MoveMoney(s, 25.10, d);
Writeln;
Writeln('
Source:');
WriteWallet(s, true);
Writeln('
Destination:');
WriteWallet(d, true);
finally
s.Free();
d.Free();
end;
end;
begin
try
{ TODO -oUser -cConsole Main : Code hier einfügen }
Main;
except
on E:
Exception do
Writeln(E.ClassName, '
: ', E.
Message);
end;
Readln;
end.