Changeset 3 for trunk/Car.pas


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.
File:
1 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('|-');
Note: See TracChangeset for help on using the changeset viewer.