Changeset 3 for trunk/Car.pas
- Timestamp:
- Jun 2, 2023, 10:06:55 PM (18 months ago)
- File:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/Car.pas
r2 r3 1 unit UCar;1 unit Car; 2 2 3 3 interface … … 5 5 uses 6 6 Classes, SysUtils, Generics.Collections, Generics.Defaults, DateUtils, 7 UPrefixMultiplier;7 PrefixMultiplier; 8 8 9 9 type … … 47 47 Distance: Double; 48 48 Consumption: Double; 49 Odometer: Integer; 49 50 function GetEmission: Double; 50 51 function GetFuel: Double; … … 66 67 function GetPrice: Double; 67 68 function GetPriceWithFuel: Double; 69 function GetAverageOdometer: Integer; 68 70 end; 69 71 … … 94 96 end; 95 97 98 { TSummaryYear } 99 96 100 TSummaryYear = class 97 101 Year: Integer; … … 100 104 Distance: Double; 101 105 RentCount: Integer; 102 CompanyCount: Integer; 106 Companies: TCompanies; 107 constructor Create; 108 destructor Destroy; override; 103 109 end; 104 110 … … 118 124 EngineTypeUnit: array[TEngineType] of string = ('', 'l', 'l', 'Wh', 119 125 'g', 'g'); 126 DistanceUnits: array[TEngineType] of Integer = (1, 100, 100, 1, 100, 100); 127 128 function Explode(Separator: string; Data: string): TStringArray; 129 var 130 Index: Integer; 131 begin 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; 145 end; 120 146 121 147 function Trim(const S: string; What: Char): string; overload; … … 132 158 end; 133 159 160 { TSummaryYear } 161 162 constructor TSummaryYear.Create; 163 begin 164 Companies := TCompanies.Create(False); 165 end; 166 167 destructor TSummaryYear.Destroy; 168 begin 169 FreeAndNil(Companies); 170 inherited; 171 end; 172 134 173 { TSummaryYears } 135 174 … … 201 240 Result := Result + Rents[I].PriceWithFuel; 202 241 end; 242 end; 243 244 function TCompany.GetAverageOdometer: Integer; 245 var 246 I: Integer; 247 Count: Integer; 248 begin 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; 203 258 end; 204 259 … … 219 274 function TRent.GetFuel: Double; 220 275 begin 221 Result := Distance * Consumption;276 Result := Distance / DistanceUnits[Car.EngineType] * Consumption; 222 277 end; 223 278 … … 316 371 ValueDouble: Double; 317 372 ValueDate: TDateTime; 318 Years: TSummaryYear; 373 Parts: TStringArray; 374 Part: string; 375 Odometer: Integer; 376 InBetween: string; 377 LeftIndex: Integer; 378 RightIndex: Integer; 319 379 const 320 380 DistanceText = '* Vzdálenost:'; … … 331 391 DateText = '* Datum:'; 332 392 BrandText = '* Značka:'; 393 TableLineStartText = '| '; 394 TableLineCellSeparator = '||'; 333 395 begin 334 396 Lines := TStringList.Create; … … 346 408 if Line.StartsWith(DistanceText) then begin 347 409 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; 350 419 if Pos(DistanceUnit, Line) > 0 then 351 420 Line := Trim(Copy(Line, 1, Pos(DistanceUnit, Line) - 1)); … … 430 499 Company.Rents.Add(Rent); 431 500 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; 432 510 end else begin 433 511 // Line := Trim(Line); … … 487 565 SummaryYear.PriceWithFuel := SummaryYear.PriceWithFuel + PriceWithFuel; 488 566 Inc(SummaryYear.RentCount); 567 if SummaryYear.Companies.IndexOf(Company) = -1 then 568 SummaryYear.Companies.Add(Company); 489 569 end; 490 570 … … 509 589 FloatToStr(PriceWithFuel) + ' || ' + 510 590 FloatToStr(Distance) + ' || ' + IntToStr(RentCount) + ' || ' + 511 IntToStr(Compan yCount));591 IntToStr(Companies.Count)); 512 592 if I < SummaryYears.Count - 1 then AddLine('|-'); 513 593 end; … … 517 597 AddLine('==Auta=='); 518 598 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'); 520 600 AddLine('|-'); 521 601 for I := 0 to Cars.Count - 1 do 522 with Cars[I]do begin602 with TCar(Cars[I]) do begin 523 603 AddLine('| ' + Name + ' || ' + IntToStr(Rents.Count) + ' || ' + 524 FloatToStr(Round(GetConsumption * 100) / 100) );604 FloatToStr(Round(GetConsumption * 100) / 100) + ' || ' + EngineTypeText[EngineType]); 525 605 if I < Cars.Count - 1 then AddLine('|-'); 526 606 end; … … 530 610 AddLine('==Půjčovny=='); 531 611 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]'); 533 613 AddLine('|-'); 534 614 for I := 0 to Companies.Count - 1 do 535 615 with TCompany(Companies[I]) do begin 536 616 AddLine('| ' + Name + ' || ' + IntToStr(Rents.Count) + ' || ' + 537 FloatToStr(GetPrice) + ' || ' + FloatToStr(GetPriceWithFuel)); 617 FloatToStr(GetPrice) + ' || ' + FloatToStr(GetPriceWithFuel) + ' || ' + 618 IntToStr(GetAverageOdometer)); 538 619 if I < Companies.Count - 1 then AddLine('|-'); 539 620 end; … … 543 624 AddLine('==Zápůjčky=='); 544 625 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'); 546 627 AddLine('|-'); 547 628 for I := 0 to Rents.Count - 1 do … … 552 633 CompanyName + ' || ' + FloatToStr(Price) + ' || ' + 553 634 FloatToStr(PriceWithFuel) + ' || ' + 554 FloatToStr( Consumption) + ' || ' + FloatToStr(Distance) + ' || ' +635 FloatToStr(Distance) + ' || ' + FloatToStr(Consumption) + ' || ' + 555 636 EngineTypeText[Car.EngineType] + ' || ' + FloatToStr(GetFuel)); 556 637 if I < Rents.Count - 1 then AddLine('|-');
Note:
See TracChangeset
for help on using the changeset viewer.