Changeset 3 for trunk


Ignore:
Timestamp:
Jun 2, 2023, 10:06:55 PM (18 months ago)
Author:
chronos
Message:
  • Modified: Removed U prefix from unit names.
  • Modified: Improved summary calculation.
  • Added: Parsing rent car initial odometer.
Location:
trunk
Files:
2 edited
4 moved

Legend:

Unmodified
Added
Removed
  • trunk/Car.pas

    r2 r3  
    1 unit UCar;
     1unit Car;
    22
    33interface
     
    55uses
    66  Classes, SysUtils, Generics.Collections, Generics.Defaults, DateUtils,
    7   UPrefixMultiplier;
     7  PrefixMultiplier;
    88
    99type
     
    4747    Distance: Double;
    4848    Consumption: Double;
     49    Odometer: Integer;
    4950    function GetEmission: Double;
    5051    function GetFuel: Double;
     
    6667    function GetPrice: Double;
    6768    function GetPriceWithFuel: Double;
     69    function GetAverageOdometer: Integer;
    6870  end;
    6971
     
    9496  end;
    9597
     98  { TSummaryYear }
     99
    96100  TSummaryYear = class
    97101    Year: Integer;
     
    100104    Distance: Double;
    101105    RentCount: Integer;
    102     CompanyCount: Integer;
     106    Companies: TCompanies;
     107    constructor Create;
     108    destructor Destroy; override;
    103109  end;
    104110
     
    118124  EngineTypeUnit: array[TEngineType] of string = ('', 'l', 'l', 'Wh',
    119125    'g', 'g');
     126  DistanceUnits: array[TEngineType] of Integer = (1, 100, 100, 1, 100, 100);
     127
     128function Explode(Separator: string; Data: string): TStringArray;
     129var
     130  Index: Integer;
     131begin
     132  Result := Default(TStringArray);
     133  repeat
     134    Index := Pos(Separator, Data);
     135    if Index > 0 then begin
     136      SetLength(Result, Length(Result) + 1);
     137      Result[High(Result)] := Copy(Data, 1, Index - 1);
     138      Delete(Data, 1, Index + Length(Separator) - 1);
     139    end else Break;
     140  until False;
     141  if Data <> '' then begin
     142    SetLength(Result, Length(Result) + 1);
     143    Result[High(Result)] := Data;
     144  end;
     145end;
    120146
    121147function Trim(const S: string; What: Char): string; overload;
     
    132158end;
    133159
     160{ TSummaryYear }
     161
     162constructor TSummaryYear.Create;
     163begin
     164  Companies := TCompanies.Create(False);
     165end;
     166
     167destructor TSummaryYear.Destroy;
     168begin
     169  FreeAndNil(Companies);
     170  inherited;
     171end;
     172
    134173{ TSummaryYears }
    135174
     
    201240    Result := Result + Rents[I].PriceWithFuel;
    202241  end;
     242end;
     243
     244function TCompany.GetAverageOdometer: Integer;
     245var
     246  I: Integer;
     247  Count: Integer;
     248begin
     249  Result := 0;
     250  Count := 0;
     251  for I := 0 to Rents.Count - 1 do
     252  if Rents[I].Odometer > 0 then begin
     253    Result := Result + Rents[I].Odometer;
     254    Inc(Count);
     255  end;
     256  if Count > 0 then
     257    Result := Result div Count;
    203258end;
    204259
     
    219274function TRent.GetFuel: Double;
    220275begin
    221   Result := Distance * Consumption;
     276  Result := Distance / DistanceUnits[Car.EngineType] * Consumption;
    222277end;
    223278
     
    316371  ValueDouble: Double;
    317372  ValueDate: TDateTime;
    318   Years: TSummaryYear;
     373  Parts: TStringArray;
     374  Part: string;
     375  Odometer: Integer;
     376  InBetween: string;
     377  LeftIndex: Integer;
     378  RightIndex: Integer;
    319379const
    320380  DistanceText = '* Vzdálenost:';
     
    331391  DateText = '* Datum:';
    332392  BrandText = '* Značka:';
     393  TableLineStartText = '| ';
     394  TableLineCellSeparator = '||';
    333395begin
    334396  Lines := TStringList.Create;
     
    346408    if Line.StartsWith(DistanceText) then begin
    347409      Line := Copy(Line, Length(DistanceText) + 1, MaxInt);
    348       if (Pos('(', Line) > 0) and (Pos(')', Line) > 0) then
    349         Line := Copy(Line, 1, Pos('(', Line) - 1) + Copy(Line, Pos(')', Line) + 1, MaxInt);
     410      LeftIndex := Pos('(', Line);
     411      RightIndex := Pos(')', Line);
     412      if (LeftIndex > 0) and (RightIndex > 0) then begin
     413        InBetween := Copy(Line, LeftIndex + 1, RightIndex - LeftIndex - 1);
     414        if Pos('-', InBetween) > 0 then InBetween := Copy(InBetween, Pos('-', InBetween) + 1, MaxInt);
     415        if TryStrToInt(Trim(InBetween), Odometer) then
     416          Rent.Odometer := Odometer;
     417        Line := Copy(Line, 1, LeftIndex - 1) + Copy(Line, RightIndex + 1, MaxInt);
     418      end;
    350419      if Pos(DistanceUnit, Line) > 0 then
    351420        Line := Trim(Copy(Line, 1, Pos(DistanceUnit, Line) - 1));
     
    430499      Company.Rents.Add(Rent);
    431500      Rent.Company := Company;
     501    end else
     502    if Line.StartsWith(TableLineStartText) then begin
     503      Parts := Explode(TableLineCellSeparator, Line);
     504      if Length(Parts) > 4 then begin
     505        Part := Parts[4];
     506        Part := Trim(Part);
     507        if TryStrToInt(Part, Odometer) and (Rent.Odometer = 0) then
     508          Rent.Odometer := Odometer;
     509      end;
    432510    end else begin
    433511//      Line := Trim(Line);
     
    487565    SummaryYear.PriceWithFuel := SummaryYear.PriceWithFuel + PriceWithFuel;
    488566    Inc(SummaryYear.RentCount);
     567    if SummaryYear.Companies.IndexOf(Company) = -1 then
     568      SummaryYear.Companies.Add(Company);
    489569  end;
    490570
     
    509589      FloatToStr(PriceWithFuel) + ' || ' +
    510590      FloatToStr(Distance) + ' || ' + IntToStr(RentCount) + ' || ' +
    511       IntToStr(CompanyCount));
     591      IntToStr(Companies.Count));
    512592    if I < SummaryYears.Count - 1 then AddLine('|-');
    513593  end;
     
    517597  AddLine('==Auta==');
    518598  AddLine('{| class="wikitable sortable"');
    519   AddLine('! Jméno !! Půjčení !! Průměrná spotřeba');
     599  AddLine('! Jméno !! Půjčení !! Průměrná spotřeba !! Typ paliva');
    520600  AddLine('|-');
    521601  for I := 0 to Cars.Count - 1 do
    522   with Cars[I] do begin
     602  with TCar(Cars[I]) do begin
    523603    AddLine('| ' + Name + ' || ' + IntToStr(Rents.Count) + ' || ' +
    524       FloatToStr(Round(GetConsumption * 100) / 100));
     604      FloatToStr(Round(GetConsumption * 100) / 100) + ' || ' + EngineTypeText[EngineType]);
    525605    if I < Cars.Count - 1 then AddLine('|-');
    526606  end;
     
    530610  AddLine('==Půjčovny==');
    531611  AddLine('{| class="wikitable sortable"');
    532   AddLine('! Jméno !! Půjčení !! Celkem cena [Kč] !! Celkem cena s palivem [Kč]');
     612  AddLine('! Jméno !! Půjčení !! Celkem cena [Kč] !! Celkem cena s palivem [Kč] !! Průměrně odometr [Km]');
    533613  AddLine('|-');
    534614  for I := 0 to Companies.Count - 1 do
    535615  with TCompany(Companies[I]) do begin
    536616    AddLine('| ' + Name + ' || ' + IntToStr(Rents.Count) + ' || ' +
    537       FloatToStr(GetPrice) + ' || ' + FloatToStr(GetPriceWithFuel));
     617      FloatToStr(GetPrice) + ' || ' + FloatToStr(GetPriceWithFuel) + ' || ' +
     618      IntToStr(GetAverageOdometer));
    538619    if I < Companies.Count - 1 then AddLine('|-');
    539620  end;
     
    543624  AddLine('==Zápůjčky==');
    544625  AddLine('{| class="wikitable sortable"');
    545   AddLine('! Datum !! Auto !! Půjčovna !! Cena celkem [Kč] !! Celkem cena s palivem [Kč] !! Spotřeba [l/100 km] !! Vzdálenost [km] !! Typ paliva || Energie');
     626  AddLine('! Datum !! Auto !! Půjčovna !! Cena celkem [Kč] !! Celkem cena s palivem [Kč] !! Vzdálenost [km] !! Spotřeba !! Typ paliva || Energie');
    546627  AddLine('|-');
    547628  for I := 0 to Rents.Count - 1 do
     
    552633      CompanyName + ' || ' + FloatToStr(Price) + ' || ' +
    553634      FloatToStr(PriceWithFuel) + ' || ' +
    554       FloatToStr(Consumption) + ' || ' + FloatToStr(Distance) + ' || ' +
     635      FloatToStr(Distance) + ' || ' + FloatToStr(Consumption) + ' || ' +
    555636      EngineTypeText[Car.EngineType] + ' || ' + FloatToStr(GetFuel));
    556637    if I < Rents.Count - 1 then AddLine('|-');
  • trunk/CarRentalSummary.lpi

    r1 r3  
    1515    </General>
    1616    <BuildModes>
    17       <Item Name="Default" Default="True"/>
     17      <Item Name="Debug" Default="True"/>
     18      <Item Name="Release">
     19        <CompilerOptions>
     20          <Version Value="11"/>
     21          <Target>
     22            <Filename Value="CarRentalSummary"/>
     23          </Target>
     24          <SearchPaths>
     25            <IncludeFiles Value="$(ProjOutDir)"/>
     26            <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
     27          </SearchPaths>
     28          <Parsing>
     29            <SyntaxOptions>
     30              <SyntaxMode Value="Delphi"/>
     31              <CStyleOperator Value="False"/>
     32              <AllowLabel Value="False"/>
     33              <CPPInline Value="False"/>
     34            </SyntaxOptions>
     35          </Parsing>
     36          <CodeGeneration>
     37            <SmartLinkUnit Value="True"/>
     38            <Optimizations>
     39              <OptimizationLevel Value="3"/>
     40            </Optimizations>
     41          </CodeGeneration>
     42          <Linking>
     43            <Debugging>
     44              <GenerateDebugInfo Value="False"/>
     45              <DebugInfoType Value="dsDwarf2Set"/>
     46            </Debugging>
     47            <LinkSmart Value="True"/>
     48            <Options>
     49              <Win32>
     50                <GraphicApplication Value="True"/>
     51              </Win32>
     52            </Options>
     53          </Linking>
     54        </CompilerOptions>
     55      </Item>
    1856    </BuildModes>
    1957    <PublishOptions>
     
    3573      </Unit>
    3674      <Unit>
    37         <Filename Value="UFormMain.pas"/>
     75        <Filename Value="FormMain.pas"/>
    3876        <IsPartOfProject Value="True"/>
    3977        <ComponentName Value="FormMain"/>
     
    4280      </Unit>
    4381      <Unit>
    44         <Filename Value="UCar.pas"/>
     82        <Filename Value="Car.pas"/>
    4583        <IsPartOfProject Value="True"/>
    4684      </Unit>
    4785      <Unit>
    48         <Filename Value="UPrefixMultiplier.pas"/>
     86        <Filename Value="PrefixMultiplier.pas"/>
    4987        <IsPartOfProject Value="True"/>
    5088      </Unit>
     
    63101      <SyntaxOptions>
    64102        <SyntaxMode Value="Delphi"/>
     103        <CStyleOperator Value="False"/>
     104        <IncludeAssertionCode Value="True"/>
     105        <AllowLabel Value="False"/>
     106        <CPPInline Value="False"/>
    65107      </SyntaxOptions>
    66108    </Parsing>
     109    <CodeGeneration>
     110      <Checks>
     111        <IOChecks Value="True"/>
     112        <RangeChecks Value="True"/>
     113        <OverflowChecks Value="True"/>
     114        <StackChecks Value="True"/>
     115      </Checks>
     116      <VerifyObjMethodCallValidity Value="True"/>
     117    </CodeGeneration>
    67118    <Linking>
    68119      <Debugging>
    69120        <DebugInfoType Value="dsDwarf2Set"/>
     121        <UseHeaptrc Value="True"/>
    70122      </Debugging>
    71123      <Options>
  • trunk/CarRentalSummary.lpr

    r1 r3  
    1111  {$ENDIF}
    1212  Interfaces, // this includes the LCL widgetset
    13   Forms, UFormMain, UCar, UPrefixMultiplier
     13  Forms, FormMain, Car, PrefixMultiplier
    1414  { you can add units after this };
    1515
     
    2020  Application.Scaled:=True;
    2121  Application.Initialize;
    22   Application.CreateForm(TFormMain, FormMain);
     22  Application.CreateForm(TFormMain, FormMain.FormMain);
    2323  Application.Run;
    2424end.
  • trunk/FormMain.lfm

    r2 r3  
    1111  OnDestroy = FormDestroy
    1212  OnShow = FormShow
    13   LCLVersion = '2.2.4.0'
     13  LCLVersion = '2.2.6.0'
    1414  WindowState = wsMaximized
    1515  object LabelSummary: TLabel
  • trunk/FormMain.pas

    r2 r3  
    1 unit UFormMain;
     1unit FormMain;
    22
    33interface
     
    55uses
    66  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ComCtrls,
    7   UCar;
     7  Car;
    88
    99type
  • trunk/PrefixMultiplier.pas

    r2 r3  
    1 unit UPrefixMultiplier;
     1unit PrefixMultiplier;
    22
    33// Date: 2010-06-01
Note: See TracChangeset for help on using the changeset viewer.