Changeset 4


Ignore:
Timestamp:
Sep 12, 2023, 10:52:25 PM (15 months ago)
Author:
chronos
Message:
  • Modified: Show cars distinguished by name and engine type.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Car.pas

    r3 r4  
    1717  TCar = class
    1818    Name: string;
     19    EngineType: TEngineType;
    1920    Rents: TRents;
    20     EngineType: TEngineType;
    2121    constructor Create;
    2222    destructor Destroy; override;
     
    2929    function AddNew(Name: string): TCar;
    3030    function SearchByName(Name: string): TCar;
     31    function SearchByNameEngine(Name: string; EngineType: TEngineType): TCar;
    3132  end;
    3233
     
    4849    Consumption: Double;
    4950    Odometer: Integer;
     51    procedure Assign(Source: TRent);
    5052    function GetEmission: Double;
    5153    function GetFuel: Double;
     
    8587    FOnLog: TLogEvent;
    8688    procedure Log(Text: string);
     89    procedure SaveAndFreeCar(var CarName: string; var EngineType: TEngineType;
     90      var CompanyName: string; var Distance: Double; var Odometer: Integer;
     91      var Price: double; var PriceWithFuel: double; var RentTime: TDateTime;
     92      var Consumption: double);
    8793  public
    8894    Cars: TCars;
     
    260266{ TRent }
    261267
     268procedure TRent.Assign(Source: TRent);
     269begin
     270  Time := Source.Time;
     271  Price := Source.Price;
     272  PriceWithFuel := Source.PriceWithFuel;
     273  Distance := Source.Distance;
     274  Consumption := Source.Consumption;
     275  Odometer := Source.Odometer;
     276end;
     277
    262278function TRent.GetEmission: Double;
    263279begin
     
    342358end;
    343359
     360function TCars.SearchByNameEngine(Name: string; EngineType: TEngineType): TCar;
     361var
     362  I: Integer;
     363begin
     364  I := 0;
     365  while (I < Count) and ((Items[I].Name <> Name) or
     366    (Items[I].EngineType <> EngineType)) do Inc(I);
     367  if I < Count then Result := Items[I]
     368    else Result := nil;
     369end;
     370
    344371{ TSummary }
    345372
     
    364391  Lines: TStringList;
    365392  Line: string;
    366   Car: TCar;
    367   Company: TCompany;
    368   Rent: TRent;
    369   Name: string;
    370393  CarComparer: TCarComparer;
    371394  ValueDouble: Double;
     
    374397  Part: string;
    375398  Odometer: Integer;
     399  ValueInteger: Integer;
    376400  InBetween: string;
    377401  LeftIndex: Integer;
    378402  RightIndex: Integer;
     403  CarName: string;
     404  EngineType: TEngineType;
     405  CompanyName: string;
     406  Distance: double;
     407  Price: double;
     408  PriceWithFuel: double;
     409  RentTime: TDateTime;
     410  Consumption: double;
    379411const
    380412  DistanceText = '* Vzdálenost:';
     
    399431    Line := Lines[I];
    400432    if Pos('==', Line) > 0 then begin
    401       Name := Trim(Line, '=');
    402       Car := Cars.SearchByName(Name);
    403       if not Assigned(Car) then Car := Cars.AddNew(Name);
    404       Rent := Rents.AddNew;
    405       Rent.Car := Car;
    406       Car.Rents.Add(Rent);
     433      SaveAndFreeCar(CarName, EngineType, CompanyName, Distance, Odometer, Price,
     434        PriceWithFuel, RentTime, Consumption);
     435      CarName := Trim(Line, '=');
    407436    end else
    408437    if Line.StartsWith(DistanceText) then begin
     
    413442        InBetween := Copy(Line, LeftIndex + 1, RightIndex - LeftIndex - 1);
    414443        if Pos('-', InBetween) > 0 then InBetween := Copy(InBetween, Pos('-', InBetween) + 1, MaxInt);
    415         if TryStrToInt(Trim(InBetween), Odometer) then
    416           Rent.Odometer := Odometer;
     444        if TryStrToInt(Trim(InBetween), ValueInteger) then
     445          Odometer := ValueInteger;
    417446        Line := Copy(Line, 1, LeftIndex - 1) + Copy(Line, RightIndex + 1, MaxInt);
    418447      end;
     
    420449        Line := Trim(Copy(Line, 1, Pos(DistanceUnit, Line) - 1));
    421450      if TryStrToFloat(Line, ValueDouble) then begin
    422         Rent.Distance := ValueDouble;
     451        Distance := ValueDouble;
    423452      end else begin
    424453        Log('Špatná vzdálenost ' + Line);
     
    426455    end else
    427456    if Line.StartsWith(BrandText) then begin
    428       Line := Copy(Line, Length(BrandText) + 1, MaxInt);
    429       if Pos('diesel', Line) > 0 then
    430         Car.EngineType := etDiesel;
     457      Line := LowerCase(Copy(Line, Length(BrandText) + 1, MaxInt));
     458      if Line.Contains('diesel') then EngineType := etDiesel
     459      else if Line.Contains('lpg') then EngineType := etLpg
     460      else if Line.Contains('kwh') then EngineType := etElectric;
    431461    end else
    432462    if Line.StartsWith(PriceText) then begin
     
    435465        Line := Trim(Copy(Line, 1, Pos(PriceUnit, Line) - 1));
    436466      if TryStrToFloat(Line, ValueDouble) then begin
    437         Rent.Price := ValueDouble;
     467        Price := ValueDouble;
    438468      end else begin
    439469        Log('Špatná cena ' + Line);
     
    445475        Line := Trim(Copy(Line, 1, Pos(PriceUnit, Line) - 1));
    446476      if TryStrToFloat(Line, ValueDouble) then begin
    447         Rent.PriceWithFuel := ValueDouble;
     477        PriceWithFuel := ValueDouble;
    448478      end else begin
    449479        Log('Špatná cena včetně paliva ' + Line);
     
    455485        Line := Trim(Copy(Line, 1, Pos(PriceUnit, Line) - 1));
    456486      if TryStrToFloat(Line, ValueDouble) then begin
    457         Rent.PriceWithFuel := ValueDouble;
     487        PriceWithFuel := ValueDouble;
    458488      end else begin
    459489        Log('Špatný cena včetně energie ' + Line);
     
    463493      Line := Trim(Copy(Line, Length(DateText) + 1, MaxInt));
    464494      if TryStrToDate(Line, ValueDate) then begin
    465         Rent.Time := ValueDate;
     495        RentTime := ValueDate;
    466496      end else begin
    467497        Log('Špatný datum ' + Line);
     
    472502      if Pos(ConsumptionUnit, Line) > 0 then begin
    473503        Line := Trim(Copy(Line, 1, Pos(ConsumptionUnit, Line) - 1));
    474         if Car.EngineType = etNone then
    475           Car.EngineType := etPetrol;
     504        if EngineType = etNone then
     505          EngineType := etPetrol;
    476506        if TryStrToFloat(Line, ValueDouble) then
    477           Rent.Consumption := ValueDouble;
     507          Consumption := ValueDouble;
    478508      end;
    479509      if Pos(ConsumptionUnitElectric, Line) > 0 then begin
    480510        Line := Trim(Copy(Line, 1, Pos(ConsumptionUnitElectric, Line) - 1));
    481         if Car.EngineType = etNone then
    482           Car.EngineType := etElectric;
     511        if EngineType = etNone then
     512          EngineType := etElectric;
    483513        if TryStrToFloat(Line, ValueDouble) then
    484           Rent.Consumption := ValueDouble;
     514          Consumption := ValueDouble;
    485515      end;
    486516      if Pos(ConsumptionUnitCng, Line) > 0 then begin
    487517        Line := Trim(Copy(Line, 1, Pos(ConsumptionUnitCng, Line) - 1));
    488         if Car.EngineType = etNone then
    489           Car.EngineType := etCng;
     518        if EngineType = etNone then
     519          EngineType := etCng;
    490520        if TryStrToFloat(Line, ValueDouble) then
    491           Rent.Consumption := ValueDouble * 1000;
     521          Consumption := ValueDouble * 1000;
    492522      end;
    493523    end else
    494524    if Line.StartsWith(CompanyText) then begin
    495       Line := Trim(Copy(Line, Length(CompanyText) + 1, MaxInt));
    496       Company := Companies.SearchByName(Line);
    497       if not Assigned(Company) then
    498         Company := Companies.AddNew(Line);
    499       Company.Rents.Add(Rent);
    500       Rent.Company := Company;
     525      CompanyName := Trim(Copy(Line, Length(CompanyText) + 1, MaxInt));
    501526    end else
    502527    if Line.StartsWith(TableLineStartText) then begin
     
    505530        Part := Parts[4];
    506531        Part := Trim(Part);
    507         if TryStrToInt(Part, Odometer) and (Rent.Odometer = 0) then
    508           Rent.Odometer := Odometer;
     532        if TryStrToInt(Part, Odometer) and (Odometer = 0) then
     533          Odometer := Odometer;
    509534      end;
    510535    end else begin
     
    512537    end;
    513538  end;
     539  SaveAndFreeCar(CarName, EngineType, CompanyName, Distance, Odometer, Price,
     540    PriceWithFuel, RentTime, Consumption);
    514541  Lines.Free;
    515542  CarComparer := TCarComparer.Create;
     
    648675end;
    649676
     677procedure TSummary.SaveAndFreeCar(var CarName: string;
     678  var EngineType: TEngineType; var CompanyName: string; var Distance: Double;
     679  var Odometer: Integer; var Price: double; var PriceWithFuel: double;
     680  var RentTime: TDateTime; var Consumption: double);
     681var
     682  Car: TCar;
     683  Rent: TRent;
     684  Company: TCompany;
     685begin
     686  if EngineType = etNone then EngineType := etPetrol;
     687  if CarName <> '' then begin
     688    Car := Cars.SearchByNameEngine(CarName, EngineType);
     689    if not Assigned(Car) then begin
     690      Car := Cars.AddNew(CarName);
     691      Car.EngineType := EngineType;
     692    end;
     693
     694    Rent := Rents.AddNew;
     695    Car.Rents.Add(Rent);
     696    Rent.Car := Car;
     697    Rent.Odometer := Odometer;
     698    Rent.Distance := Distance;
     699    Rent.Price := Price;
     700    Rent.PriceWithFuel := PriceWithFuel;
     701    Rent.Time := RentTime;
     702    Rent.Consumption := Consumption;
     703
     704    Company := Companies.SearchByName(CompanyName);
     705    if not Assigned(Company) then begin
     706      Company := Companies.AddNew(CompanyName);
     707    end;
     708    Company.Rents.Add(Rent);
     709    Rent.Company := Company;
     710  end;
     711
     712  CarName := '';
     713  EngineType := etNone;
     714  CompanyName := '';
     715  Odometer := 0;
     716  Distance := 0;
     717  Price := 0;
     718  PriceWithFuel := 0;
     719  RentTime := 0;
     720  Consumption := 0;
     721end;
     722
    650723end.
    651724
  • trunk/PrefixMultiplier.pas

    r3 r4  
    3131  (
    3232    (ShortText: 'y'; FullText: 'yocto'; Value: 1e-24),
    33           (ShortText: 'z'; FullText: 'zepto'; Value: 1e-21),
     33    (ShortText: 'z'; FullText: 'zepto'; Value: 1e-21),
    3434    (ShortText: 'a'; FullText: 'atto'; Value: 1e-18),
    3535    (ShortText: 'f'; FullText: 'femto'; Value: 1e-15),
Note: See TracChangeset for help on using the changeset viewer.